[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 *) (** apply a map function for escape sequences *)
let escape_map map_fun s = let escape_map map_fun s =
let len = String.length s in let needs_escape = String.exists ~f:(fun c -> Option.is_some (map_fun c)) s in
let buf = Buffer.create len in if needs_escape then
for i = 0 to len - 1 do let len = String.length s in
let c = String.unsafe_get s i in let buf = Buffer.create len in
match map_fun c with None -> Buffer.add_char buf c | Some s' -> Buffer.add_string buf s' for i = 0 to len - 1 do
done ; let c = String.unsafe_get s i in
Buffer.contents buf 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 escape_csv s =
let map = function let map = function

Loading…
Cancel
Save