[utils] only make copies of strings that need to be escaped

Reviewed By: martinoluca

Differential Revision: D5752049

fbshipit-source-id: 0354965
master
Sam Blackshear 7 years ago committed by Facebook Github Bot
parent c42431115f
commit 9ab89025a8

@ -14,13 +14,17 @@ open! IStd
(** apply a map function for escape sequences *)
let escape_map map_fun s =
let len = String.length s in
let buf = Buffer.create len in
for i = 0 to len - 1 do
let c = String.unsafe_get s i in
match map_fun c with None -> Buffer.add_char buf c | Some s' -> Buffer.add_string buf s'
done ;
Buffer.contents buf
let needs_escape = String.exists ~f:(fun c -> Option.is_some (map_fun c)) s in
if needs_escape then
let len = String.length s in
let buf = Buffer.create len in
for i = 0 to len - 1 do
let c = String.unsafe_get s i in
match map_fun c with None -> Buffer.add_char buf c | Some s' -> Buffer.add_string buf s'
done ;
Buffer.contents buf
else (* not escaping anything, so don't waste memory on a copy of the string *)
s
let escape_csv s =
let map = function

Loading…
Cancel
Save