Skip to content
Browse files

[fix] json,escaping: follow json specification and faster

  • Loading branch information...
1 parent 15e6452 commit 225c6317cf0b627e55841f0bba1871677bd1065e @OpaOnWindowsNow OpaOnWindowsNow committed Dec 13, 2011
Showing with 17 additions and 9 deletions.
  1. +17 −9 stdlib/core/string.opa
View
26 stdlib/core/string.opa
@@ -321,15 +321,23 @@ String =
* Very slow: replace or improve
**/
escape_non_utf8_special(v) =
- rpl_list = [
- ( "\\" , "\\\\"),
- ( "\n" , "\\n" ),
- ( "\r" , "\\r" ),
- ( "\t" , "\\t" ),
- ( "\000" , "\\000" ),
- ( "\"" , "\\\"" )
- ]
- List.foldl((pat, rpl), str -> String.replace(pat, rpl, str) , rpl_list, v)
+ /* see json.org */
+ transform =
+ | "\"" -> "\\\""
+ | "\\" -> "\\\\"
+/* | "\/" -> "\\\/"
+ | "\b" -> "\\n"
+ | "\f" -> "\\f" include in fallback*/
+ | "\n" -> "\\n"
+ | "\r" -> "\\r"
+ | "\t" -> "\\t"
+ | c ->
+ i = Cactutf.look(c,0)
+ if i < 32 then
+ if i < 10 then "\\u000{i}"
+ else "\\u00{i}"
+ else c
+ String.map(transform,v)
/**
* {2 Additional functions}

0 comments on commit 225c631

Please sign in to comment.
Something went wrong with that request. Please try again.