Escape sequences in Ocaml

Is there a standard way in Ocaml to convert from (for example) a\n(a three byte string:) 0x61,0x5c,0x6eto a double byte string 0x61,0x0a:?

My Ocaml program can get strings with escaped characters, like their "unescape"?

+3
source share
2 answers

Good solution from http://caml.inria.fr/mantis/view.php?id=3888 :

let unescape s = Scanf.sscanf ("\"" ^ s ^ "\"") "%S%!" (fun u -> u)

+2
source

If you are using OCaml 4.0.0 or later, you can use Scanf.unescaped.

Example:

# open Scanf
# Scanf.unescaped "a\\n";;
- : bytes = "a\n"      
# Scanf.unescaped "\\n\\t\\\\";;
- : bytes = "\n\t\\"

Interface: ( via OCaml docs )

val unescaped : string -> string

escape-, OCaml, . escape-, , String.escaped.

4.00.0

+1

All Articles