From e7b50987708729b9c3d0c8c0acb58e801142e6b9 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 3 Dec 2009 11:20:13 -0500 Subject: Escaping UTF-8 in MonoOpt --- src/mono_opt.sml | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'src/mono_opt.sml') diff --git a/src/mono_opt.sml b/src/mono_opt.sml index d4441ac7..3a5b4f4c 100644 --- a/src/mono_opt.sml +++ b/src/mono_opt.sml @@ -61,14 +61,37 @@ val urlifyFloat = attrifyFloat val htmlifyInt = attrifyInt val htmlifyFloat = attrifyFloat -val htmlifyString = String.translate (fn ch => case ch of - #"<" => "<" - | #"&" => "&" - | _ => - if Char.isPrint ch orelse Char.isSpace ch then - str ch - else - "&#" ^ Int.toString (ord ch) ^ ";") + +fun htmlifyString s = + let + fun hs (pos, acc) = + if pos >= size s then + String.concat (rev acc) + else + case String.sub (s, pos) of + #"<" => hs (pos+1, "<" :: acc) + | #"&" => hs (pos+1, "&" :: acc) + | ch => + let + val n = ord ch + fun isCont k = pos + k < size s + andalso ord (String.sub (s, pos + k)) div 64 = 2 + fun unicode k = hs (pos+k+1, String.substring (s, pos, k+1) :: acc) + in + if Char.isPrint ch orelse Char.isSpace ch then + hs (pos+1, str ch :: acc) + else if n div 32 = 6 andalso isCont 1 then + unicode 1 + else if n div 16 = 14 andalso isCont 1 andalso isCont 2 then + unicode 2 + else if n div 8 = 30 andalso isCont 1 andalso isCont 2 andalso isCont 3 then + unicode 3 + else + hs (pos+1, "&#" ^ Int.toString (ord ch) ^ ";" :: acc) + end + in + hs (0, []) + end fun hexIt ch = let -- cgit v1.2.3