File: string.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (112 lines) | stat: -rw-r--r-- 3,361 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(* Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 * Copyright (C) 1997-2000 NEC Research Institute.
 *
 * MLton is released under a HPND-style license.
 * See the file MLton-LICENSE for details.
 *)

signature STRING_ARG =
   sig
      structure Char: CHAR_EXTRA
      structure CharVector: EQTYPE_MONO_VECTOR_EXTRA
      sharing type Char.char   = CharVector.elem
      sharing type Char.string = CharVector.vector
   end

functor StringFn(Arg : STRING_ARG) 
        :> STRING_EXTRA
             where type char   = Arg.CharVector.elem
             where type string = Arg.CharVector.vector 
             where type array  = Arg.CharVector.array =
   struct
      open Arg
      open CharVector
      structure CharVectorSlice = MonoVectorSlice
      
      type char = elem
      type string = vector
      
      val new = vector
      fun str c = new (1, c)
      
      val maxSize = maxLen
      val size = length
      val op ^ = append
      val implode = fromList
      val explode = toList
      
      fun extract (s, start, len) = 
         CharVectorSlice.vector (CharVectorSlice.slice (s, start, len))
      fun substring (s, start, len) = extract (s, start, SOME len)

      val toLower = translate (str o Char.toLower)

      local
         fun make f = f (op = : char * char -> bool)
      in
        val isPrefix = make isPrefix
        val isSubstring = make isSubvector
        val isSuffix = make isSuffix
      end
      val compare = collate Char.compare
      local
         structure S = StringComparisons (type t = string
                                          val compare = compare)
      in
         open S
      end
      
      fun Stranslate f = String.fromPoly o Vector.translate f o toPoly

      val toString = Stranslate Char.toString
      val toCString = Stranslate Char.toCString

      val scan =
         fn reader =>
         let
            fun loop (state, cs) =
               case Char.scan reader state of
                  NONE => SOME (implode (rev cs),
                                Char.formatSequences reader state)
                | SOME (c, state) => loop (state, c :: cs)
         in
            fn state =>
            case reader state of
               NONE => SOME (implode [], state)
             | SOME _ =>
               case Char.scan reader state of
                  SOME (c, state) => loop (state, [c])
                | NONE =>
                  case Char.formatSequencesOpt reader state of
                     SOME ((), state) => SOME (implode [], state)
                   | NONE => NONE
         end

      val fromString = StringCvt.scanString scan

      fun scanString scanChar reader =
         fn state =>
         Option.map (fn (cs, state) => (implode cs, state))
         (Reader.list (scanChar reader) state)

      val fromCString = StringCvt.scanString (scanString Char.scanC)

      val null = str (Char.chr 0)
      fun nullTerm s = s ^ null
   end

structure StringArg : STRING_ARG =
   struct
      structure Char = Char
      structure CharVector = CharVector
   end

structure WideStringArg : STRING_ARG =
   struct
      structure Char = WideChar
      structure CharVector = WideCharVector
   end

structure String : STRING_EXTRA = StringFn(StringArg)
structure WideString : STRING_EXTRA = StringFn(WideStringArg)