Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / basis-library / text / string.sml
CommitLineData
7f918cf1
CE
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 BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9signature STRING_ARG =
10 sig
11 structure Char: CHAR_EXTRA
12 structure CharVector: EQTYPE_MONO_VECTOR_EXTRA
13 sharing type Char.char = CharVector.elem
14 sharing type Char.string = CharVector.vector
15 end
16
17functor StringFn(Arg : STRING_ARG)
18 :> STRING_EXTRA
19 where type char = Arg.CharVector.elem
20 where type string = Arg.CharVector.vector
21 where type array = Arg.CharVector.array =
22 struct
23 open Arg
24 open CharVector
25 structure CharVectorSlice = MonoVectorSlice
26
27 type char = elem
28 type string = vector
29
30 val new = vector
31 fun str c = new (1, c)
32
33 val maxSize = maxLen
34 val size = length
35 val op ^ = append
36 val implode = fromList
37 val explode = toList
38
39 fun extract (s, start, len) =
40 CharVectorSlice.vector (CharVectorSlice.slice (s, start, len))
41 fun substring (s, start, len) = extract (s, start, SOME len)
42
43 val toLower = translate (str o Char.toLower)
44
45 local
46 fun make f = f (op = : char * char -> bool)
47 in
48 val isPrefix = make isPrefix
49 val isSubstring = make isSubvector
50 val isSuffix = make isSuffix
51 end
52 val compare = collate Char.compare
53 local
54 structure S = StringComparisons (type t = string
55 val compare = compare)
56 in
57 open S
58 end
59
60 fun Stranslate f = String.fromPoly o Vector.translate f o toPoly
61
62 val toString = Stranslate Char.toString
63 val toCString = Stranslate Char.toCString
64
65 val scan =
66 fn reader =>
67 let
68 fun loop (state, cs) =
69 case Char.scan reader state of
70 NONE => SOME (implode (rev cs),
71 Char.formatSequences reader state)
72 | SOME (c, state) => loop (state, c :: cs)
73 in
74 fn state => loop (state, [])
75 end
76
77 val fromString = StringCvt.scanString scan
78
79 fun scanString scanChar reader =
80 fn state =>
81 Option.map (fn (cs, state) => (implode cs, state))
82 (Reader.list (scanChar reader) state)
83
84 val fromCString = StringCvt.scanString (scanString Char.scanC)
85
86 val null = str (Char.chr 0)
87 fun nullTerm s = s ^ null
88 end
89
90structure StringArg : STRING_ARG =
91 struct
92 structure Char = Char
93 structure CharVector = CharVector
94 end
95
96structure WideStringArg : STRING_ARG =
97 struct
98 structure Char = WideChar
99 structure CharVector = WideCharVector
100 end
101
102structure String : STRING_EXTRA = StringFn(StringArg)
103structure WideString : STRING_EXTRA = StringFn(WideStringArg)