| 1 |
(* Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh |
| 2 |
* Jagannathan, and Stephen Weeks. |
| 3 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 4 |
* |
| 5 |
* MLton is released under a HPND-style license. |
| 6 |
* See the file MLton-LICENSE for details. |
| 7 |
*) |
| 8 |
|
| 9 |
signature CHAR_ARG = |
| 10 |
sig |
| 11 |
structure PreChar : PRE_CHAR |
| 12 |
structure CharVector: EQTYPE_MONO_VECTOR_EXTRA |
| 13 |
structure CharArray: MONO_ARRAY_EXTRA |
| 14 |
sharing type PreChar.char = CharVector.elem = CharArray.elem |
| 15 |
sharing type PreChar.string = CharVector.vector = CharArray.vector |
| 16 |
end |
| 17 |
|
| 18 |
functor CharFn(Arg : CHAR_ARG) |
| 19 |
:> CHAR_EXTRA |
| 20 |
where type char = Arg.PreChar.char |
| 21 |
where type string = Arg.PreChar.string = |
| 22 |
struct |
| 23 |
open Arg.PreChar |
| 24 |
|
| 25 |
type string = Arg.CharVector.vector |
| 26 |
val maxOrd: int = numChars - 1 |
| 27 |
|
| 28 |
val fromString = Arg.CharVector.fromPoly o |
| 29 |
Vector.map (fn x => fromChar x) o |
| 30 |
String.toPoly |
| 31 |
|
| 32 |
fun succ c = |
| 33 |
if Primitive.Controls.safe |
| 34 |
andalso c = maxChar |
| 35 |
then raise Chr |
| 36 |
else chrUnsafe (Int.+ (ord c, 1)) |
| 37 |
|
| 38 |
fun pred c = |
| 39 |
if Primitive.Controls.safe |
| 40 |
andalso c = minChar |
| 41 |
then raise Chr |
| 42 |
else chrUnsafe (Int.- (ord c, 1)) |
| 43 |
|
| 44 |
fun chrOpt c = |
| 45 |
if Primitive.Controls.safe |
| 46 |
andalso Int.gtu (c, maxOrd) |
| 47 |
then NONE |
| 48 |
else SOME (chrUnsafe c) |
| 49 |
|
| 50 |
fun chr c = |
| 51 |
case chrOpt c of |
| 52 |
NONE => raise Chr |
| 53 |
| SOME c => c |
| 54 |
|
| 55 |
(* To implement character classes, we cannot use lookup tables on the |
| 56 |
* order of the number of characters. We don't want to scan the string |
| 57 |
* each time, so instead we'll sort it and use binary search. |
| 58 |
*) |
| 59 |
fun contains s = |
| 60 |
let |
| 61 |
val a = Array.tabulate (Arg.CharVector.length s, |
| 62 |
fn i => Arg.CharVector.sub (s, i)) |
| 63 |
val () = Heap.heapSort (a, op <) |
| 64 |
in |
| 65 |
fn c => |
| 66 |
let |
| 67 |
val x = Heap.binarySearch (a, fn d => d < c) |
| 68 |
in |
| 69 |
if x = Array.length a then false else |
| 70 |
Array.sub (a, x) = c |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
fun notContains s = not o contains s |
| 75 |
|
| 76 |
val c = fromChar |
| 77 |
val ( la, lA, lf, lF, lz, lZ, l0, l9, lSPACE,lBANG, lTIL, lTAB, lCR, lDEL) = |
| 78 |
(c#"a", c#"A", c#"f", c#"F", c#"z", c#"Z", c#"0", c#"9", c#" ", c#"!", c#"~", c#"\t", c#"\r", c#"\127") |
| 79 |
|
| 80 |
(* Range comparisons don't need tables! It's faster to just compare. *) |
| 81 |
fun isLower c = la <= c andalso c <= lz |
| 82 |
fun isUpper c = c <= lZ andalso lA <= c (* More discriminating first! *) |
| 83 |
fun isDigit c = c <= l9 andalso l0 <= c (* More discriminating first! *) |
| 84 |
fun isGraph c = lBANG <= c andalso c <= lTIL |
| 85 |
fun isPrint c = lSPACE <= c andalso c <= lTIL |
| 86 |
fun isCntrl c = c < lSPACE orelse c = lDEL |
| 87 |
fun isAscii c = c <= lDEL |
| 88 |
|
| 89 |
(* These take advantage of ASCII ordering to minimize comparisons. *) |
| 90 |
fun isAlpha c = if la <= c then c <= lz else lA <= c andalso c <= lZ |
| 91 |
fun isAlphaNum c = |
| 92 |
if lA <= c then |
| 93 |
if la <= c then c <= lz else c <= lZ |
| 94 |
else |
| 95 |
l0 <= c andalso c <= l9 |
| 96 |
fun isHexDigit c = |
| 97 |
if lA <= c then |
| 98 |
if la <= c then c <= lf else c <= lF |
| 99 |
else |
| 100 |
l0 <= c andalso c <= l9 |
| 101 |
fun isSpace c = if lCR < c then c = lSPACE else lTAB <= c |
| 102 |
fun isPunct c = isGraph c andalso not (isAlphaNum c) |
| 103 |
|
| 104 |
local |
| 105 |
fun make (test, diff) c = |
| 106 |
if test c then chrUnsafe (Int.+! (ord c, diff)) else c |
| 107 |
val diff = Int.- (ord lA, ord la) |
| 108 |
in |
| 109 |
val toLower = make (isUpper, Int.~ diff) |
| 110 |
val toUpper = make (isLower, diff) |
| 111 |
end |
| 112 |
|
| 113 |
fun control reader state = |
| 114 |
case reader state of |
| 115 |
NONE => NONE |
| 116 |
| SOME (c, state) => |
| 117 |
if Char.<= (#"@", c) andalso Char.<= (c, #"_") |
| 118 |
then SOME (chr (Int.-! (Char.ord c, Char.ord #"@")), state) |
| 119 |
else NONE |
| 120 |
|
| 121 |
fun formatChar reader state = |
| 122 |
case reader state of |
| 123 |
NONE => NONE |
| 124 |
| SOME (c, state) => |
| 125 |
if StringCvt.isSpace c |
| 126 |
then SOME ((), state) |
| 127 |
else NONE |
| 128 |
|
| 129 |
fun formatChars reader = |
| 130 |
let |
| 131 |
fun loop state = |
| 132 |
case formatChar reader state of |
| 133 |
NONE => state |
| 134 |
| SOME ((), state) => loop state |
| 135 |
in |
| 136 |
loop |
| 137 |
end |
| 138 |
|
| 139 |
val 'a formatSequencesOpt: (Char.char, 'a) StringCvt.reader |
| 140 |
-> (unit, 'a) StringCvt.reader = |
| 141 |
fn reader => |
| 142 |
let |
| 143 |
fun loop state = |
| 144 |
case reader state of |
| 145 |
SOME (#"\\", state1) => |
| 146 |
(case formatChar reader state1 of |
| 147 |
NONE => NONE |
| 148 |
| SOME ((), state2) => |
| 149 |
let |
| 150 |
val state3 = formatChars reader state2 |
| 151 |
in |
| 152 |
case reader state3 of |
| 153 |
SOME (#"\\", state4) => |
| 154 |
(case loop state4 of |
| 155 |
state5 as SOME _ => state5 |
| 156 |
| NONE => SOME ((), state4)) |
| 157 |
| _ => NONE |
| 158 |
end) |
| 159 |
| _ => NONE |
| 160 |
in |
| 161 |
loop |
| 162 |
end |
| 163 |
|
| 164 |
fun formatSequences reader state = |
| 165 |
case formatSequencesOpt reader state of |
| 166 |
NONE => state |
| 167 |
| SOME ((), state) => state |
| 168 |
|
| 169 |
fun 'a scan (reader: (Char.char, 'a) StringCvt.reader) |
| 170 |
: (char, 'a) StringCvt.reader = |
| 171 |
let |
| 172 |
val escape : (char, 'a) StringCvt.reader = |
| 173 |
fn state => |
| 174 |
case reader state of |
| 175 |
NONE => NONE |
| 176 |
| SOME (c, state') => |
| 177 |
let |
| 178 |
fun yes c = SOME (fromChar c, state') |
| 179 |
in |
| 180 |
case c of |
| 181 |
#"a" => yes #"\a" |
| 182 |
| #"b" => yes #"\b" |
| 183 |
| #"t" => yes #"\t" |
| 184 |
| #"n" => yes #"\n" |
| 185 |
| #"v" => yes #"\v" |
| 186 |
| #"f" => yes #"\f" |
| 187 |
| #"r" => yes #"\r" |
| 188 |
| #"\\" => yes #"\\" |
| 189 |
| #"\"" => yes #"\"" |
| 190 |
| #"^" => control reader state' |
| 191 |
| #"u" => |
| 192 |
Reader.mapOpt chrOpt |
| 193 |
(StringCvt.digitsExact (StringCvt.HEX, 4) reader) |
| 194 |
state' |
| 195 |
| #"U" => |
| 196 |
Reader.mapOpt chrOpt |
| 197 |
(StringCvt.digitsExact (StringCvt.HEX, 8) reader) |
| 198 |
state' |
| 199 |
| _ => (* 3 decimal digits *) |
| 200 |
Reader.mapOpt chrOpt |
| 201 |
(StringCvt.digitsExact (StringCvt.DEC, 3) |
| 202 |
reader) |
| 203 |
state |
| 204 |
end |
| 205 |
val main: (char, 'a) StringCvt.reader = |
| 206 |
fn state => |
| 207 |
let |
| 208 |
val state = formatSequences reader state |
| 209 |
in |
| 210 |
case reader state of |
| 211 |
NONE => NONE |
| 212 |
| SOME (c, state) => |
| 213 |
(* isPrint doesn't exist. yuck: *) |
| 214 |
if Char.>= (c, #" ") andalso Char.<= (c, #"~") |
| 215 |
then |
| 216 |
case c of |
| 217 |
#"\\" => escape state |
| 218 |
| #"\"" => NONE |
| 219 |
| _ => SOME (fromChar c, formatSequences reader state) |
| 220 |
else NONE |
| 221 |
end |
| 222 |
in |
| 223 |
main |
| 224 |
end |
| 225 |
|
| 226 |
val fromString = StringCvt.scanString scan |
| 227 |
|
| 228 |
fun 'a scanC (reader: (Char.char, 'a) StringCvt.reader) |
| 229 |
: (char, 'a) StringCvt.reader = |
| 230 |
let |
| 231 |
val rec escape = |
| 232 |
fn state => |
| 233 |
case reader state of |
| 234 |
NONE => NONE |
| 235 |
| SOME (c, state') => |
| 236 |
let fun yes c = SOME (fromChar c, state') |
| 237 |
in case c of |
| 238 |
#"a" => yes #"\a" |
| 239 |
| #"b" => yes #"\b" |
| 240 |
| #"t" => yes #"\t" |
| 241 |
| #"n" => yes #"\n" |
| 242 |
| #"v" => yes #"\v" |
| 243 |
| #"f" => yes #"\f" |
| 244 |
| #"r" => yes #"\r" |
| 245 |
| #"?" => yes #"?" |
| 246 |
| #"\\" => yes #"\\" |
| 247 |
| #"\"" => yes #"\"" |
| 248 |
| #"'" => yes #"'" |
| 249 |
| #"^" => control reader state' |
| 250 |
| #"x" => |
| 251 |
Reader.mapOpt chrOpt |
| 252 |
(StringCvt.digits StringCvt.HEX reader) |
| 253 |
state' |
| 254 |
| #"u" => |
| 255 |
Reader.mapOpt chrOpt |
| 256 |
(StringCvt.digitsExact (StringCvt.HEX, 4) reader) |
| 257 |
state' |
| 258 |
| #"U" => |
| 259 |
Reader.mapOpt chrOpt |
| 260 |
(StringCvt.digitsExact (StringCvt.HEX, 8) reader) |
| 261 |
state' |
| 262 |
| _ => |
| 263 |
Reader.mapOpt chrOpt |
| 264 |
(StringCvt.digitsPlus (StringCvt.OCT, 3) reader) |
| 265 |
state |
| 266 |
end |
| 267 |
and main = |
| 268 |
fn NONE => NONE |
| 269 |
| SOME (c, state) => |
| 270 |
(* yuck. isPrint is not defined yet: *) |
| 271 |
if Char.>= (c, #" ") andalso Char.<= (c, #"~") |
| 272 |
then |
| 273 |
case c of |
| 274 |
#"\\" => escape state |
| 275 |
| _ => SOME (fromChar c, state) |
| 276 |
else NONE |
| 277 |
in |
| 278 |
main o reader |
| 279 |
end |
| 280 |
|
| 281 |
val fromCString = StringCvt.scanString scanC |
| 282 |
|
| 283 |
fun padLeft (s: String.string, n: int): String.string = |
| 284 |
let |
| 285 |
val m = String.size s |
| 286 |
val diff = Int.-! (n, m) |
| 287 |
in if Int.> (diff, 0) |
| 288 |
then String.concat [String.new (diff, #"0"), s] |
| 289 |
else if diff = 0 |
| 290 |
then s |
| 291 |
else raise Fail "padLeft" |
| 292 |
end |
| 293 |
|
| 294 |
fun unicodeEscape ord = |
| 295 |
if Int.< (ord, 65536) |
| 296 |
then String.concat |
| 297 |
["\\u", padLeft (Int.fmt StringCvt.HEX ord, 4)] |
| 298 |
else String.concat |
| 299 |
["\\U", padLeft (Int.fmt StringCvt.HEX ord, 8)] |
| 300 |
|
| 301 |
fun toString c = |
| 302 |
let |
| 303 |
val ord = ord c |
| 304 |
in |
| 305 |
if isPrint c |
| 306 |
then |
| 307 |
case ord of |
| 308 |
92 (* #"\\" *) => "\\\\" |
| 309 |
| 34 (* #"\"" *) => "\\\"" |
| 310 |
| _ => String.new (1, Char.chrUnsafe ord) |
| 311 |
(* ^^^^ safe b/c isPrint < 128 *) |
| 312 |
else |
| 313 |
case ord of |
| 314 |
7 (* #"\a" *) => "\\a" |
| 315 |
| 8 (* #"\b" *) => "\\b" |
| 316 |
| 9 (* #"\t" *) => "\\t" |
| 317 |
| 10 (* #"\n" *) => "\\n" |
| 318 |
| 11 (* #"\v" *) => "\\v" |
| 319 |
| 12 (* #"\f" *) => "\\f" |
| 320 |
| 13 (* #"\r" *) => "\\r" |
| 321 |
| _ => |
| 322 |
if Int.< (ord, 32) |
| 323 |
then String.concat |
| 324 |
["\\^", String.new |
| 325 |
(1, Char.chrUnsafe |
| 326 |
(Int.+! (ord, 64 (* #"@" *) )))] |
| 327 |
else if Int.< (ord, 256) |
| 328 |
then String.concat |
| 329 |
["\\", padLeft (Int.fmt StringCvt.DEC ord, 3)] |
| 330 |
else unicodeEscape ord |
| 331 |
end |
| 332 |
|
| 333 |
fun toCString c = |
| 334 |
let |
| 335 |
val ord = ord c |
| 336 |
in |
| 337 |
if isPrint c |
| 338 |
then |
| 339 |
case ord of |
| 340 |
92 (* #"\\" *) => "\\\\" |
| 341 |
| 34 (* #"\"" *) => "\\\"" |
| 342 |
| 63 (* #"?" *) => "\\?" |
| 343 |
| 39 (* #"'" *) => "\\'" |
| 344 |
| _ => String.new (1, Char.chrUnsafe ord) |
| 345 |
else |
| 346 |
case ord of |
| 347 |
7 (* #"\a" *) => "\\a" |
| 348 |
| 8 (* #"\b" *) => "\\b" |
| 349 |
| 9 (* #"\t" *) => "\\t" |
| 350 |
| 10 (* #"\n" *) => "\\n" |
| 351 |
| 11 (* #"\v" *) => "\\v" |
| 352 |
| 12 (* #"\f" *) => "\\f" |
| 353 |
| 13 (* #"\r" *) => "\\r" |
| 354 |
| _ => |
| 355 |
if Int.< (ord, 256) |
| 356 |
then String.concat |
| 357 |
["\\", padLeft (Int.fmt StringCvt.OCT ord, 3)] |
| 358 |
else unicodeEscape ord |
| 359 |
end |
| 360 |
end |
| 361 |
|
| 362 |
structure CharArg : CHAR_ARG = |
| 363 |
struct |
| 364 |
structure PreChar = Char |
| 365 |
structure CharVector = CharVector |
| 366 |
structure CharArray = CharArray |
| 367 |
end |
| 368 |
|
| 369 |
structure WideCharArg : CHAR_ARG = |
| 370 |
struct |
| 371 |
structure PreChar = WideChar |
| 372 |
structure CharVector = WideCharVector |
| 373 |
structure CharArray = WideCharArray |
| 374 |
end |
| 375 |
|
| 376 |
structure Char : CHAR_EXTRA = CharFn(CharArg) |
| 377 |
structure WideChar : CHAR_EXTRA = CharFn(WideCharArg) |