[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,6 +14,8 @@ 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 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 len = String.length s in
let buf = Buffer.create len in let buf = Buffer.create len in
for i = 0 to len - 1 do for i = 0 to len - 1 do
@ -21,6 +23,8 @@ let escape_map map_fun s =
match map_fun c with None -> Buffer.add_char buf c | Some s' -> Buffer.add_string buf s' match map_fun c with None -> Buffer.add_char buf c | Some s' -> Buffer.add_string buf s'
done ; done ;
Buffer.contents buf 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