Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / word8.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 1999-2005 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(* Use fromLargeWord instead of fromLarge so we can compile this code
10 * with older MLtons that don't have fromLarge.
11 *)
12functor FixWord (W: PERVASIVE_WORD) =
13 struct
14 type t = W.word
15
16 local
17 structure LargeWord = Pervasive.LargeWord
18 structure Word = Pervasive.Word
19 structure Word8 = Pervasive.Word8
20 in
21 fun format (w, f) = W.fmt f w
22 val fromChar = W.fromLargeWord o Word8.toLargeWord o Byte.charToByte
23 val fromIntInf = W.fromLargeInt
24 val fromLarge = W.fromLargeWord o LargeWord.toLargeWord
25 val fromWord = W.fromLargeWord o Word.toLargeWord
26 val layout = Layout.str o W.toString
27 fun nthBitIsSet (w: t, n: int): bool =
28 W.fromInt 1 = W.andb (W.fromInt 1, W.>> (w, Word.fromInt n))
29 val toChar = Byte.byteToChar o Word8.fromLargeWord o W.toLargeWord
30 val toIntInf = W.toLargeInt
31 val toIntInfX = W.toLargeIntX
32 val toLarge = LargeWord.fromLargeWord o W.toLargeWord
33 val toLargeX = LargeWord.fromLargeWord o W.toLargeWordX
34 val toWord = Word.fromLargeWord o W.toLargeWord
35 val toWordX = Word.fromLargeWord o W.toLargeWordX
36 end
37 end
38
39structure Word8:
40 sig
41 include WORD
42
43 val stringToVector: string -> t vector
44 val vectorToString: t vector -> string
45 end =
46 struct
47 open Pervasive.Word8
48 structure Z = FixWord (Pervasive.Word8)
49 open Z
50
51 val equals: t * t -> bool = op =
52
53 fun vectorToString v =
54 CharVector.tabulate (Pervasive.Vector.length v, fn i =>
55 toChar (Pervasive.Vector.sub (v, i)))
56
57 fun stringToVector s =
58 Pervasive.Vector.tabulate (Pervasive.String.size s, fn i =>
59 fromChar (Pervasive.String.sub (s, i)))
60 end