diff --git a/infer/src/backend/escape.ml b/infer/src/backend/escape.ml new file mode 100644 index 000000000..f1a1130d7 --- /dev/null +++ b/infer/src/backend/escape.ml @@ -0,0 +1,58 @@ +(* + * Copyright (c) 2016 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + *) + +(** Escape a string for use in a CSV or XML file: replace reserved + characters with escape sequences *) + +(** 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 escape_csv s = + let map = function + | '"' -> Some "\"\"" + | c when Char.code c > 127 -> Some "?" (* non-ascii character: escape *) + | _ -> None in + escape_map map s + +let escape_xml s = + let map = function + | '"' -> (* on next line to avoid bad indentation *) + Some """ + | '>' -> Some ">" + | '<' -> Some "<" + | '&' -> Some "&" + | '%' -> Some "%" + | c when Char.code c > 127 -> (* non-ascii character: escape *) + Some ("&#" ^ string_of_int (Char.code c) ^ ";") + | _ -> None in + escape_map map s + +let escape_dotty s = + let map = function + | '"' -> Some "\\\"" + | '\\' -> Some "\\\\" + | _ -> None in + escape_map map s + +let escape_path s = + let map = function + | c -> + if Char.escaped c = Filename.dir_sep + then Some "_" + else None in + escape_map map s diff --git a/infer/src/backend/escape.mli b/infer/src/backend/escape.mli new file mode 100644 index 000000000..1f574702e --- /dev/null +++ b/infer/src/backend/escape.mli @@ -0,0 +1,26 @@ +(* + * Copyright (c) 2016 - present Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + *) + +(** Escape a string for use in a CSV or XML file: replace reserved + characters with escape sequences *) + +(** escape a string specifying the per character escaping function *) +val escape_map : (char -> string option) -> string -> string + +(** escape a string to be used in a dotty file *) +val escape_dotty : string -> string + +(** escape a string to be used in a csv file *) +val escape_csv : string -> string + +(** escape a path replacing the directory separator with an underscore *) +val escape_path : string -> string + +(** escape a string to be used in an xml file *) +val escape_xml : string -> string diff --git a/infer/src/backend/utils.ml b/infer/src/backend/utils.ml index fd9600b4b..446920ff5 100644 --- a/infer/src/backend/utils.ml +++ b/infer/src/backend/utils.ml @@ -859,58 +859,6 @@ module Arg = struct align dlist end - -(** Escape a string for use in a CSV or XML file: replace reserved characters with escape sequences *) -module Escape = struct - (** 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 escape_csv s = - let map = function - | '"' -> Some "\"\"" - | c when Char.code c > 127 -> Some "?" (* non-ascii character: escape *) - | _ -> None in - escape_map map s - - let escape_xml s = - let map = function - | '"' -> (* on next line to avoid bad indentation *) - Some """ - | '>' -> Some ">" - | '<' -> Some "<" - | '&' -> Some "&" - | '%' -> Some "%" - | c when Char.code c > 127 -> (* non-ascii character: escape *) - Some ("&#" ^ string_of_int (Char.code c) ^ ";") - | _ -> None in - escape_map map s - - let escape_dotty s = - let map = function - | '"' -> Some "\\\"" - | '\\' -> Some "\\\\" - | _ -> None in - escape_map map s - - let escape_path s = - let map_csv = function - | c -> - if (Char.escaped c) = Filename.dir_sep - then Some "_" - else None in - escape_map map_csv s -end - - (** flags for a procedure *) type proc_flags = (string, string) Hashtbl.t diff --git a/infer/src/backend/utils.mli b/infer/src/backend/utils.mli index 95019d1b7..823a0fdeb 100644 --- a/infer/src/backend/utils.mli +++ b/infer/src/backend/utils.mli @@ -317,17 +317,6 @@ val base_arg_desc : arg_list (** Reserved command-line arguments *) val reserved_arg_desc : arg_list -(** Escape a string for use in a CSV or XML file: replace reserved characters with escape sequences *) -module Escape : sig - (** escape a string specifying the per character escaping function *) - val escape_map : (char -> string option) -> string -> string - val escape_dotty : string -> string (** escape a string to be used in a dotty file *) - val escape_csv : string -> string (** escape a string to be used in a csv file *) - val escape_path : string -> string (** escape a path replacing the directory separator with an underscore *) - val escape_xml : string -> string (** escape a string to be used in an xml file *) -end - - (** flags for a procedure *) type proc_flags = (string, string) Hashtbl.t