move utils.Escape to its own file

Summary:public
Refactoring to make utils.ml more manageable in size.

Reviewed By: cristianoc

Differential Revision: D3058341

fb-gh-sync-id: 7696299
shipit-source-id: 7696299
master
Jules Villard 9 years ago committed by Facebook Github Bot 1
parent 8913e38dbd
commit 4384870b44

@ -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 "&lt;"
| '&' -> Some "&amp;"
| '%' -> Some "&#37;"
| 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

@ -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

@ -859,58 +859,6 @@ module Arg = struct
align dlist align dlist
end 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 "&quot;"
| '>' -> Some "&gt;"
| '<' -> Some "&lt;"
| '&' -> Some "&amp;"
| '%' -> Some "&#37;"
| 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 *) (** flags for a procedure *)
type proc_flags = (string, string) Hashtbl.t type proc_flags = (string, string) Hashtbl.t

@ -317,17 +317,6 @@ val base_arg_desc : arg_list
(** Reserved command-line arguments *) (** Reserved command-line arguments *)
val reserved_arg_desc : arg_list 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 *) (** flags for a procedure *)
type proc_flags = (string, string) Hashtbl.t type proc_flags = (string, string) Hashtbl.t

Loading…
Cancel
Save