Mercurial > urweb
comparison src/mono_opt.sml @ 1054:b06a2a65e670
UTF-8 in dynamic escaping
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 03 Dec 2009 11:50:51 -0500 |
parents | 4eb1c4a1b057 |
children | 03a81e26e5fe |
comparison
equal
deleted
inserted
replaced
1053:4eb1c4a1b057 | 1054:b06a2a65e670 |
---|---|
43 if n < 0.0 then | 43 if n < 0.0 then |
44 "-" ^ Real.toString (Real.~ n) | 44 "-" ^ Real.toString (Real.~ n) |
45 else | 45 else |
46 Real.toString n | 46 Real.toString n |
47 | 47 |
48 fun attrifyChar ch = | 48 fun attrifyString s = |
49 case ch of | |
50 #"\"" => """ | |
51 | #"&" => "&" | |
52 | ch => if Char.isPrint ch then | |
53 str ch | |
54 else | |
55 "&#" ^ Int.toString (ord ch) ^ ";" | |
56 | |
57 val attrifyString = String.translate attrifyChar | |
58 | |
59 val urlifyInt = attrifyInt | |
60 val urlifyFloat = attrifyFloat | |
61 | |
62 val htmlifyInt = attrifyInt | |
63 val htmlifyFloat = attrifyFloat | |
64 | |
65 fun htmlifyString s = | |
66 let | 49 let |
67 fun hs (pos, acc) = | 50 fun hs (pos, acc) = |
68 if pos >= size s then | 51 if pos >= size s then |
69 String.concat (rev acc) | 52 String.concat (rev acc) |
70 else | 53 else |
71 case String.sub (s, pos) of | 54 case String.sub (s, pos) of |
72 #"<" => hs (pos+1, "<" :: acc) | 55 #"\"" => hs (pos+1, """ :: acc) |
73 | #"&" => hs (pos+1, "&" :: acc) | 56 | #"&" => hs (pos+1, "&" :: acc) |
74 | ch => | 57 | ch => |
75 let | 58 let |
76 val n = ord ch | 59 val n = ord ch |
77 fun isCont k = pos + k < size s | 60 fun isCont k = pos + k < size s |
91 end | 74 end |
92 in | 75 in |
93 hs (0, []) | 76 hs (0, []) |
94 end | 77 end |
95 | 78 |
79 fun attrifyChar ch = | |
80 case ch of | |
81 #"\"" => """ | |
82 | #"&" => "&" | |
83 | ch => if Char.isPrint ch then | |
84 str ch | |
85 else | |
86 "&#" ^ Int.toString (ord ch) ^ ";" | |
87 | |
88 val urlifyInt = attrifyInt | |
89 val urlifyFloat = attrifyFloat | |
90 | |
91 val htmlifyInt = attrifyInt | |
92 val htmlifyFloat = attrifyFloat | |
93 | |
94 fun htmlifyString s = | |
95 let | |
96 fun hs (pos, acc) = | |
97 if pos >= size s then | |
98 String.concat (rev acc) | |
99 else | |
100 case String.sub (s, pos) of | |
101 #"<" => hs (pos+1, "<" :: acc) | |
102 | #"&" => hs (pos+1, "&" :: acc) | |
103 | ch => | |
104 let | |
105 val n = ord ch | |
106 fun isCont k = pos + k < size s | |
107 andalso ord (String.sub (s, pos + k)) div 64 = 2 | |
108 fun unicode k = hs (pos+k+1, String.substring (s, pos, k+1) :: acc) | |
109 in | |
110 if Char.isPrint ch then | |
111 hs (pos+1, str ch :: acc) | |
112 else if n div 32 = 6 andalso isCont 1 then | |
113 unicode 1 | |
114 else if n div 16 = 14 andalso isCont 1 andalso isCont 2 then | |
115 unicode 2 | |
116 else if n div 8 = 30 andalso isCont 1 andalso isCont 2 andalso isCont 3 then | |
117 unicode 3 | |
118 else | |
119 hs (pos+1, "&#" ^ Int.toString (ord ch) ^ ";" :: acc) | |
120 end | |
121 in | |
122 hs (0, []) | |
123 end | |
124 | |
96 fun hexIt ch = | 125 fun hexIt ch = |
97 let | 126 let |
98 val s = Int.fmt StringCvt.HEX (ord ch) | 127 val s = Int.fmt StringCvt.HEX (ord ch) |
99 in | 128 in |
100 case size s of | 129 case size s of |