Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 2009 Matthew Fluet. |
2 | * Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh | |
3 | * Jagannathan, and Stephen Weeks. | |
4 | * | |
5 | * MLton is released under a BSD-style license. | |
6 | * See the file MLton-LICENSE for details. | |
7 | *) | |
8 | ||
9 | structure Char0 : CHAR0 = | |
10 | struct | |
11 | ||
12 | structure Array = Pervasive.Array | |
13 | structure Int = Pervasive.Int | |
14 | structure String = Pervasive.String | |
15 | open Pervasive.Char | |
16 | ||
17 | type t = char | |
18 | ||
19 | val dash = #"-" | |
20 | val dquote = #"\"" | |
21 | val newline = #"\n" | |
22 | val space = #" " | |
23 | val toInt = ord | |
24 | val fromInt = chr | |
25 | val escapeSML = toString | |
26 | val escapeC = toCString | |
27 | val toString = String.str | |
28 | val equals: t * t -> bool = op = | |
29 | val toWord8 = Byte.charToByte | |
30 | val fromWord8 = Byte.byteToChar | |
31 | ||
32 | val {max, min, compare, ...} = Relation0.lessEqual {< = op <, equals = equals} | |
33 | ||
34 | fun digitToInt (c: t): int option = | |
35 | if isDigit c | |
36 | then SOME (ord c - ord #"0") | |
37 | else NONE | |
38 | ||
39 | fun fromDigit (d: int): t = | |
40 | if let open Int in 0 <= d andalso d < 10 end | |
41 | then chr (d + ord #"0") | |
42 | else Error.bug "Char0.fromDigit" | |
43 | ||
44 | fun output (c, out) = TextIO.output (out, toString c) | |
45 | ||
46 | val numChars = ord maxChar + 1 | |
47 | ||
48 | fun memoize (f: t -> 'a): t -> 'a = | |
49 | let val a = Array.tabulate (numChars, f o chr) | |
50 | in fn c => Array.sub (a, ord c) | |
51 | end | |
52 | ||
53 | fun toHexDigit (c: t): int = | |
54 | if #"0" <= c andalso c <= #"9" | |
55 | then ord c - ord #"0" | |
56 | else if #"a" <= c andalso c <= #"f" | |
57 | then ord c - ord #"a" + 10 | |
58 | else if #"A" <= c andalso c <= #"F" | |
59 | then ord c - ord #"A" + 10 | |
60 | else Error.bug "Char0.charToHexDigit" | |
61 | ||
62 | fun fromHexDigit (n: int): char = String.sub ("0123456789ABCDEF", n) | |
63 | ||
64 | end |