comparison src/mono_opt.sml @ 1053:4eb1c4a1b057

Escaping UTF-8 in MonoOpt
author Adam Chlipala <adamc@hcoop.net>
date Thu, 03 Dec 2009 11:20:13 -0500
parents 93415bcf54c0
children b06a2a65e670
comparison
equal deleted inserted replaced
1048:38411c2cd363 1053:4eb1c4a1b057
59 val urlifyInt = attrifyInt 59 val urlifyInt = attrifyInt
60 val urlifyFloat = attrifyFloat 60 val urlifyFloat = attrifyFloat
61 61
62 val htmlifyInt = attrifyInt 62 val htmlifyInt = attrifyInt
63 val htmlifyFloat = attrifyFloat 63 val htmlifyFloat = attrifyFloat
64 val htmlifyString = String.translate (fn ch => case ch of 64
65 #"<" => "&lt;" 65 fun htmlifyString s =
66 | #"&" => "&amp;" 66 let
67 | _ => 67 fun hs (pos, acc) =
68 if Char.isPrint ch orelse Char.isSpace ch then 68 if pos >= size s then
69 str ch 69 String.concat (rev acc)
70 else 70 else
71 "&#" ^ Int.toString (ord ch) ^ ";") 71 case String.sub (s, pos) of
72 #"<" => hs (pos+1, "&lt;" :: acc)
73 | #"&" => hs (pos+1, "&amp;" :: acc)
74 | ch =>
75 let
76 val n = ord ch
77 fun isCont k = pos + k < size s
78 andalso ord (String.sub (s, pos + k)) div 64 = 2
79 fun unicode k = hs (pos+k+1, String.substring (s, pos, k+1) :: acc)
80 in
81 if Char.isPrint ch orelse Char.isSpace ch then
82 hs (pos+1, str ch :: acc)
83 else if n div 32 = 6 andalso isCont 1 then
84 unicode 1
85 else if n div 16 = 14 andalso isCont 1 andalso isCont 2 then
86 unicode 2
87 else if n div 8 = 30 andalso isCont 1 andalso isCont 2 andalso isCont 3 then
88 unicode 3
89 else
90 hs (pos+1, "&#" ^ Int.toString (ord ch) ^ ";" :: acc)
91 end
92 in
93 hs (0, [])
94 end
72 95
73 fun hexIt ch = 96 fun hexIt ch =
74 let 97 let
75 val s = Int.fmt StringCvt.HEX (ord ch) 98 val s = Int.fmt StringCvt.HEX (ord ch)
76 in 99 in