Import Upstream version 20180207
[hcoop/debian/mlton.git] / mlton / atoms / char-size.fun
1 (* Copyright (C) 2004-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8 functor CharSize (S: CHAR_SIZE_STRUCTS): CHAR_SIZE =
9 struct
10
11 open S
12
13 datatype t = C8 | C16 | C32
14
15 val all = [C8, C16, C32]
16
17 fun bits s =
18 Bits.fromInt
19 (case s of
20 C8 => 8
21 | C16 => 16
22 | C32 => 32)
23
24 val equals = op =
25
26 fun fromBits b =
27 case Bits.toInt b of
28 8 => C8
29 | 16 => C16
30 | 32 => C32
31 | _ => Error.bug "CharSize.frombits"
32
33 val memoize =
34 fn f =>
35 let
36 val c8 = f C8
37 val c16 = f C16
38 val c32 = f C32
39 in
40 fn C8 => c8
41 | C16 => c16
42 | C32 => c32
43 end
44
45 val cardinality = memoize (fn s => IntInf.pow (2, Bits.toInt (bits s)))
46
47 fun isInRange (s, i) = 0 <= i andalso i < cardinality s
48
49 end