Module String.Escaping
Operations for escaping and unescaping strings, with parameterized escape and escapeworthy characters. Escaping/unescaping using this module is more efficient than using Pcre. Benchmark code can be found in core/benchmarks/string_escaping.ml.
val escape_gen_exn : escapeworthy_map:(char * char) list -> escape_char:char -> (string -> string) Staged.tescape_gen_exn escapeworthy_map escape_charreturns a function that will escape a stringsas follows: if(c1,c2)is inescapeworthy_map, then all occurrences ofc1are replaced byescape_charconcatenated toc2.Raises an exception if
escapeworthy_mapis not one-to-one. Ifescape_charis not inescapeworthy_map, then it will be escaped to itself.
val escape_gen : escapeworthy_map:(char * char) list -> escape_char:char -> (string -> string) Or_error.tval escape : escapeworthy:char list -> escape_char:char -> (string -> string) Staged.tescape ~escapeworthy ~escape_char sisescape_gen_exn ~escapeworthy_map:(List.zip_exn escapeworthy escapeworthy) ~escape_charDuplicates and
escape_charwill be removed fromescapeworthy. So, no exception will be raised
val unescape_gen_exn : escapeworthy_map:(char * char) list -> escape_char:char -> (string -> string) Staged.tunescape_gen_exnis the inverse operation ofescape_gen_exn. That is,let escape = Staged.unstage (escape_gen_exn ~escapeworthy_map ~escape_char) in let unescape = Staged.unstage (unescape_gen_exn ~escapeworthy_map ~escape_char) in assert (s = unescape (escape s))always succeed when ~escapeworthy_map is not causing exceptions.
val unescape_gen : escapeworthy_map:(char * char) list -> escape_char:char -> (string -> string) Or_error.tval unescape : escape_char:char -> (string -> string) Staged.tunescape ~escape_charis defined asunescape_gen_exn ~map:[] ~escape_char
val is_char_escaping : string -> escape_char:char -> int -> boolAny char in an escaped string is either escaping, escaped, or literal. For example, for escaped string
"0_a0__0"withescape_charas'_', pos 1 and 4 are escaping, 2 and 5 are escaped, and the rest are literal.is_char_escaping s ~escape_char posreturns true if the char atposis escaping, false otherwise.
val is_char_escaped : string -> escape_char:char -> int -> boolis_char_escaped s ~escape_char posreturns true if the char atposis escaped, false otherwise.
val is_char_literal : string -> escape_char:char -> int -> boolis_char_literal s ~escape_char posreturns true if the char atposis not escaped or escaping.
val index : string -> escape_char:char -> char -> int optionindex s ~escape_char charfinds the first literal (not escaped) instance ofcharin s starting from 0.
val index_exn : string -> escape_char:char -> char -> intval rindex : string -> escape_char:char -> char -> int optionrindex s ~escape_char charfinds the first literal (not escaped) instance ofcharinsstarting from the end ofsand proceeding towards 0.
val rindex_exn : string -> escape_char:char -> char -> intval index_from : string -> escape_char:char -> int -> char -> int optionindex_from s ~escape_char pos charfinds the first literal (not escaped) instance ofcharinsstarting fromposand proceeding towards the end ofs.
val index_from_exn : string -> escape_char:char -> int -> char -> intval rindex_from : string -> escape_char:char -> int -> char -> int optionrindex_from s ~escape_char pos charfinds the first literal (not escaped) instance ofcharinsstarting fromposand towards 0.
val rindex_from_exn : string -> escape_char:char -> int -> char -> intval split : string -> on:char -> escape_char:char -> string listsplit s ~escape_char ~onreturns a list of substrings ofsthat are separated by literal versions ofon. Consecutiveoncharacters will cause multiple empty strings in the result. Splitting the empty string returns a list of the empty string, not the empty list.E.g.,
split ~escape_char:'_' ~on:',' "foo,bar_,baz" = ["foo"; "bar_,baz"].
val split_on_chars : string -> on:char list -> escape_char:char -> string listsplit_on_chars s ~onreturns a list of all substrings ofsthat are separated by one of the literal chars fromon.onare not grouped. So a grouping ofonin the source string will produce multiple empty string splits in the result.E.g.,
split_on_chars ~escape_char:'_' ~on:[',';'|'] "foo_|bar,baz|0" -> ["foo_|bar"; "baz"; "0"].
val lsplit2 : string -> on:char -> escape_char:char -> (string * string) optionlsplit2 s ~on ~escape_charsplits s into a pair on the first literal instance ofon(meaning the first unescaped instance) starting from the left.
val lsplit2_exn : string -> on:char -> escape_char:char -> string * stringval rsplit2 : string -> on:char -> escape_char:char -> (string * string) optionrsplit2 s ~on ~escape_charsplitssinto a pair on the first literal instance ofon(meaning the first unescaped instance) starting from the right.
val rsplit2_exn : string -> on:char -> escape_char:char -> string * stringval lstrip_literal : ?drop:(char -> bool) -> t -> escape_char:char -> tThese are the same as
lstrip,rstrip, andstripfor generic strings, except that they only drop literal characters -- they do not drop characters that are escaping or escaped. This makes sense if you're trying to get rid of junk whitespace (for example), because escaped whitespace seems more likely to be deliberate and not junk.