From 16b177329cac77136b236dfb3645e4be4e3df297 Mon Sep 17 00:00:00 2001 From: Chouser Date: Wed, 28 Jan 2015 08:27:32 -0500 Subject: Ocaml: fix string escaping and printing --- ocaml/printer.ml | 6 +++++- ocaml/reader.ml | 13 ++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ocaml/printer.ml b/ocaml/printer.ml index 8e71376..fe025af 100644 --- a/ocaml/printer.ml +++ b/ocaml/printer.ml @@ -19,7 +19,11 @@ let rec pr_str mal_obj print_readably = | T.Bool false -> "false" | T.String s -> if r - then "\"" ^ (Str.global_replace (Str.regexp "\\([\"\\]\\)") "\\\\\\1" s) ^ "\"" + then "\"" ^ (Reader.gsub (Str.regexp "\\([\"\\\n]\\)") + (function + | "\n" -> "\\n" + | x -> "\\" ^ x) + s) ^ "\"" else s | T.List { T.value = xs } -> "(" ^ (String.concat " " (List.map (fun s -> pr_str s r) xs)) ^ ")" diff --git a/ocaml/reader.ml b/ocaml/reader.ml index 9754444..96404aa 100644 --- a/ocaml/reader.ml +++ b/ocaml/reader.ml @@ -6,6 +6,11 @@ let find_re re str = (List.filter (function | Str.Delim x -> true | Str.Text x -> false) (Str.full_split re str)) ;; +let gsub re f str = + String.concat + "" (List.map (function | Str.Delim x -> f x | Str.Text x -> x) + (Str.full_split re str)) + let token_re = (Str.regexp "~@\\|[][{}()'`~^@]\\|\"\\(\\\\.\\|[^\"]\\)*\"\\|;.*\\|[^][ \n{}('\"`,;)]*") type reader = { @@ -26,9 +31,11 @@ let read_atom token = | _ -> match token.[0] with | '0'..'9' -> T.Int (int_of_string token) - | '"' -> T.String (Str.global_replace (Str.regexp "\\\\\\(.\\)") - "\\1" - (String.sub token 1 ((String.length token) - 2))) + | '"' -> T.String (gsub (Str.regexp "\\\\.") + (function + | "\\n" -> "\n" + | x -> String.sub x 1 1) + (String.sub token 1 ((String.length token) - 2))) | ':' -> T.Keyword (Str.replace_first (Str.regexp "^:") "" token) | _ -> Types.symbol token -- cgit v1.2.3