| 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 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 |
|
| 17 |
functor 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 => |
| 75 |
case reader state of |
| 76 |
NONE => SOME (implode [], state) |
| 77 |
| SOME _ => |
| 78 |
case Char.scan reader state of |
| 79 |
SOME (c, state) => loop (state, [c]) |
| 80 |
| NONE => |
| 81 |
case Char.formatSequencesOpt reader state of |
| 82 |
SOME ((), state) => SOME (implode [], state) |
| 83 |
| NONE => NONE |
| 84 |
end |
| 85 |
|
| 86 |
val fromString = StringCvt.scanString scan |
| 87 |
|
| 88 |
fun scanString scanChar reader = |
| 89 |
fn state => |
| 90 |
Option.map (fn (cs, state) => (implode cs, state)) |
| 91 |
(Reader.list (scanChar reader) state) |
| 92 |
|
| 93 |
val fromCString = StringCvt.scanString (scanString Char.scanC) |
| 94 |
|
| 95 |
val null = str (Char.chr 0) |
| 96 |
fun nullTerm s = s ^ null |
| 97 |
end |
| 98 |
|
| 99 |
structure StringArg : STRING_ARG = |
| 100 |
struct |
| 101 |
structure Char = Char |
| 102 |
structure CharVector = CharVector |
| 103 |
end |
| 104 |
|
| 105 |
structure WideStringArg : STRING_ARG = |
| 106 |
struct |
| 107 |
structure Char = WideChar |
| 108 |
structure CharVector = WideCharVector |
| 109 |
end |
| 110 |
|
| 111 |
structure String : STRING_EXTRA = StringFn(StringArg) |
| 112 |
structure WideString : STRING_EXTRA = StringFn(WideStringArg) |