| 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 | |
| 9 | (* The :> is to hide the type substring. We must add the where's to make char |
| 10 | * and string the same as the toplevel types. |
| 11 | *) |
| 12 | functor SubstringFn(Arg : STRING_ARG) |
| 13 | :> SUBSTRING_EXTRA |
| 14 | where type char = Arg.CharVector.MonoVectorSlice.elem |
| 15 | where type string = Arg.CharVector.MonoVectorSlice.vector |
| 16 | where type substring = Arg.CharVector.MonoVectorSlice.slice = |
| 17 | struct |
| 18 | open Arg |
| 19 | open CharVector.MonoVectorSlice |
| 20 | |
| 21 | type char = elem |
| 22 | type string = vector |
| 23 | type substring = slice |
| 24 | |
| 25 | val size = length |
| 26 | val extract = slice |
| 27 | fun substring (s, start, len) = extract (s, start, SOME len) |
| 28 | val string = vector |
| 29 | val getc = getItem |
| 30 | fun first ss = Option.map #1 (getItem ss) |
| 31 | val slice = subslice |
| 32 | val explode = toList |
| 33 | local |
| 34 | fun make f = f (op = : char * char -> bool) |
| 35 | in |
| 36 | val isPrefix = make isPrefix |
| 37 | val isSubstring = make isSubvector |
| 38 | val isSuffix = make isSuffix |
| 39 | val position = make position |
| 40 | end |
| 41 | val compare = collate Char.compare |
| 42 | |
| 43 | (* |
| 44 | type cs = int |
| 45 | |
| 46 | fun reader (T {str, start, size}): (char, cs) Reader.reader = |
| 47 | fn i => if i >= size |
| 48 | then NONE |
| 49 | else SOME (String.sub (str, start +? i), i + 1) |
| 50 | |
| 51 | fun 'a scanSubstring |
| 52 | (f: (char, cs) Reader.reader -> ('a, int) Reader.reader) |
| 53 | (ss: substring): 'a option = |
| 54 | case f (reader ss) 0 of |
| 55 | NONE => NONE |
| 56 | | SOME (a, _) => SOME a |
| 57 | *) |
| 58 | end |
| 59 | |
| 60 | structure Substring = SubstringFn(StringArg) |
| 61 | structure WideSubstring = SubstringFn(WideStringArg) |