comparison src/mono_opt.sml @ 1059:03a81e26e5fe

Move to simpler HTML escaping
author Adam Chlipala <adamc@hcoop.net>
date Tue, 08 Dec 2009 08:48:29 -0500
parents b06a2a65e670
children 217eb87dde31
comparison
equal deleted inserted replaced
1058:86b831978b8d 1059:03a81e26e5fe
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 attrifyString s =
49 let
50 fun hs (pos, acc) =
51 if pos >= size s then
52 String.concat (rev acc)
53 else
54 case String.sub (s, pos) of
55 #"\"" => hs (pos+1, "&quot;" :: acc)
56 | #"&" => hs (pos+1, "&amp;" :: acc)
57 | ch =>
58 let
59 val n = ord ch
60 fun isCont k = pos + k < size s
61 andalso ord (String.sub (s, pos + k)) div 64 = 2
62 fun unicode k = hs (pos+k+1, String.substring (s, pos, k+1) :: acc)
63 in
64 if Char.isPrint ch orelse Char.isSpace ch then
65 hs (pos+1, str ch :: acc)
66 else if n div 32 = 6 andalso isCont 1 then
67 unicode 1
68 else if n div 16 = 14 andalso isCont 1 andalso isCont 2 then
69 unicode 2
70 else if n div 8 = 30 andalso isCont 1 andalso isCont 2 andalso isCont 3 then
71 unicode 3
72 else
73 hs (pos+1, "&#" ^ Int.toString (ord ch) ^ ";" :: acc)
74 end
75 in
76 hs (0, [])
77 end
78
79 fun attrifyChar ch = 48 fun attrifyChar ch =
80 case ch of 49 case ch of
81 #"\"" => "&quot;" 50 #"\"" => "&quot;"
82 | #"&" => "&amp;" 51 | #"&" => "&amp;"
83 | ch => if Char.isPrint ch then 52 | ch => str ch
84 str ch 53
85 else 54 val attrifyString = String.translate attrifyChar
86 "&#" ^ Int.toString (ord ch) ^ ";" 55
87 56
88 val urlifyInt = attrifyInt 57 val urlifyInt = attrifyInt
89 val urlifyFloat = attrifyFloat 58 val urlifyFloat = attrifyFloat
90 59
91 val htmlifyInt = attrifyInt 60 val htmlifyInt = attrifyInt
92 val htmlifyFloat = attrifyFloat 61 val htmlifyFloat = attrifyFloat
93 62
94 fun htmlifyString s = 63 val htmlifyString = String.translate (fn #"<" => "&lt;"
95 let 64 | #"&" => "&amp;"
96 fun hs (pos, acc) = 65 | ch => str ch)
97 if pos >= size s then
98 String.concat (rev acc)
99 else
100 case String.sub (s, pos) of
101 #"<" => hs (pos+1, "&lt;" :: acc)
102 | #"&" => hs (pos+1, "&amp;" :: 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 66
125 fun hexIt ch = 67 fun hexIt ch =
126 let 68 let
127 val s = Int.fmt StringCvt.HEX (ord ch) 69 val s = Int.fmt StringCvt.HEX (ord ch)
128 in 70 in