ocaml-core / base / core / benchmarks / string_escaping.ml

open Core.Std

module Bench = Core_extended.Bench

let () =
  let module E = String.Escaping in
  let escape =
    Staged.unstage (E.escape ~escapeworthy:['x';'y';'z'] ~escape_char:'\\')
  let unescape =
    Staged.unstage (E.unescape ~escape_char:'\\')
  let rex = Pcre.regexp "[xyz\\\\]" in
  let unrex = Pcre.regexp "\\\\." in
  let escape_pcre s =
    Pcre.substitute ~rex ~subst:(fun s -> "\\"^s) s
  let unescape_pcre s =
    Pcre.substitute ~rex:unrex ~subst:(fun s -> Char.to_string s.[1]) s
  let strings =
    [ "aaa"
    ; "xyz"
    ; "aaaaaaaaa"
    ; "aaxaaxaax"
    ; "abcde\\abcde\\abcde\\abcde\\abcde\\abcde\\"
    ; "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    ; "aaaaaaaaaaaaaaaaaa\\aaaaaaaaaaaaaaaaaaaaaaa"
    ; "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  Bench.bench (List.concat_map strings ~f:(fun str ->
    let go name escape unescape =
      Bench.Test.create ~name:(sprintf "%s-%s" name str)
        (fun () -> assert (unescape (escape str) = str))
    [ go "String.Escaping" escape unescape
    ; go "Pcre" escape_pcre unescape_pcre
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.