File: extString.ml

package info (click to toggle)
extlib 1.7.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 632 kB
  • sloc: ml: 6,980; makefile: 128; sh: 42; ansic: 31
file content (331 lines) | stat: -rw-r--r-- 8,073 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(*
 * ExtString - Additional functions for string manipulations.
 * Copyright (C) 2003 Nicolas Cannasse
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

exception Invalid_string

open ExtBytes

module String = struct

include String

#if OCAML < 402
let init len f =
  let s = Bytes.create len in
  for i = 0 to len - 1 do
    Bytes.unsafe_set s i (f i)
  done;
        (* 's' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string s
#endif

let starts_with str p =
  if length str < length p then 
    false
  else
    let rec loop str p i =
      if i = length p then true else
      if unsafe_get str i <> unsafe_get p i then false
      else loop str p (i+1)
    in
    loop str p 0

let ends_with s e =
  if length s < length e then
    false
  else
    let rec loop s e i =
      if i = length e then true else
      if unsafe_get s (length s - length e + i) <> unsafe_get e i then false
      else loop s e (i+1)
    in
    loop s e 0

let find_from str pos sub =
  let sublen = length sub in
  if sublen = 0 then
    0
  else
    let found = ref 0 in
    let len = length str in
    try
      for i = pos to len - sublen do
        let j = ref 0 in
        while unsafe_get str (i + !j) = unsafe_get sub !j do
          incr j;
          if !j = sublen then begin found := i; raise Exit; end;
        done;
      done;
      raise Invalid_string
    with
      Exit -> !found

let find str sub = find_from str 0 sub

let exists str sub =
  try
    ignore(find str sub);
    true
  with
    Invalid_string -> false

let strip ?(chars=" \t\r\n") s =
  let p = ref 0 in
  let l = length s in
  while !p < l && contains chars (unsafe_get s !p) do
    incr p;
  done;
  let p = !p in
  let l = ref (l - 1) in
  while !l >= p && contains chars (unsafe_get s !l) do
    decr l;
  done;
  sub s p (!l - p + 1)

#if OCAML < 400
let trim s = strip ~chars:" \t\r\n\012" s
#endif

let split str sep =
  let p = find str sep in
  let len = length sep in
  let slen = length str in
  sub str 0 p, sub str (p + len) (slen - p - len)

let nsplit str sep =
  if str = "" then []
  else if sep = "" then raise Invalid_string
  else
    let rec loop acc pos =
      if pos > String.length str then
        List.rev acc
      else
        let i = try find_from str pos sep with Invalid_string -> String.length str in
        loop (String.sub str pos (i - pos) :: acc) (i + String.length sep)
    in
    loop [] 0

let join = concat

let slice =
  let clip max x = if x > max then max else if x < 0 then 0 else x in
  fun ?(first=0) ?(last=Sys.max_string_length) s ->
    let len = String.length s in
    let i = if first = 0 then 0 else clip len (if first < 0 then len + first else first) in
    let j = if last = Sys.max_string_length then len else clip len (if last < 0 then len + last else last) in
    if i>=j || i=len then
      make 0 ' '
    else
      sub s i (j-i)

let lchop s =
  if s = "" then "" else sub s 1 (length s - 1)

let rchop s =
  if s = "" then "" else sub s 0 (length s - 1)

let of_int = string_of_int

let of_float = string_of_float

let of_char = make 1

let to_int s =
  try
    int_of_string s
  with
    _ -> raise Invalid_string

let to_float s =
  try
    float_of_string s
  with
    _ -> raise Invalid_string

let enum s =
  let l = length s in
  let rec make i =
    Enum.make 
    ~next:(fun () ->
      if !i = l then
        raise Enum.No_more_elements
      else
        let p = !i in
        incr i;
        unsafe_get s p
      )
    ~count:(fun () -> l - !i)
    ~clone:(fun () -> make (ref !i))
  in
  make (ref 0)

let of_enum e =
  let l = Enum.count e in
  let s = Bytes.create l in
  let i = ref 0 in
  Enum.iter (fun c -> Bytes.unsafe_set s !i c; incr i) e;
        (* 's' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string s

#if OCAML < 400
let map f s =
  let len = length s in
  let sc = Bytes.create len in
  for i = 0 to len - 1 do
    Bytes.unsafe_set sc i (f (unsafe_get s i))
  done;
        (* 'sc' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string sc
#endif

#if OCAML < 402
let mapi f s =
  let len = length s in
  let sc = Bytes.create len in
  for i = 0 to len - 1 do
    Bytes.unsafe_set sc i (f i (unsafe_get s i))
  done;
        (* 'sc' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string sc
#endif

#if OCAML < 400
let iteri f s =
  for i = 0 to length s - 1 do
    let () = f i (unsafe_get s i) in ()
  done
#endif

let fold_left =
  let rec loop str f i n result =
    if i = n then result
    else
      loop str f (i + 1) n (f result (String.unsafe_get str i))
  in
  fun f init str -> loop str f 0 (String.length str) init

let fold_right =
  let rec loop str f i result =
    if i = 0 then result
    else
      let i' = i - 1 in
      loop str f i' (f (String.unsafe_get str i') result)
  in
  fun f str init -> loop str f (String.length str) init

(* explode and implode from the OCaml Expert FAQ. *)
let explode s =
  let rec exp i l =
    if i < 0 then l else exp (i - 1) (s.[i] :: l) in
  exp (String.length s - 1) []

let implode l =
  let res = Bytes.create (List.length l) in
  let rec imp i = function
  | [] -> res
  | c :: l -> Bytes.set res i c; imp (i + 1) l in
  let s = imp 0 l in
  (* 's' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string s

let replace_chars f s =
  let len = String.length s in
  let tlen = ref 0 in
  let rec loop i acc =
    if i = len then
      acc
    else 
      let s = f (unsafe_get s i) in
      tlen := !tlen + length s;
      loop (i+1) (s :: acc)
  in
  let strs = loop 0 [] in
  let sbuf = Bytes.create !tlen in
  let pos = ref !tlen in
  let rec loop2 = function
    | [] -> ()
    | s :: acc ->
      let len = length s in
      pos := !pos - len;
      blit s 0 sbuf !pos len;
      loop2 acc
  in
  loop2 strs;
        (* 'sbuf' doesn't escape and will never be mutated again *)
  Bytes.unsafe_to_string sbuf

let replace ~str ~sub ~by =
  try
    let i = find str sub in
    (true, (slice ~last:i str) ^ by ^ 
                   (slice ~first:(i+(String.length sub)) str))
        with
    Invalid_string -> (false, String.sub str 0 (String.length str))

#if OCAML < 403
let uppercase_ascii = uppercase
let lowercase_ascii = lowercase
let capitalize_ascii = capitalize
let uncapitalize_ascii = uncapitalize

let equal = (=)
#endif

#if OCAML < 404
let split_on_char sep s =
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if unsafe_get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r
#endif

#if OCAML < 405

let rec index_rec_opt s lim i c =
  if i >= lim then None else
  if unsafe_get s i = c then Some i else index_rec_opt s lim (i + 1) c

let index_opt s c = index_rec_opt s (length s) 0 c

let index_from_opt s i c =
  let l = length s in
  if i < 0 || i > l then invalid_arg "ExtString.index_from_opt" else
  index_rec_opt s l i c

let rec rindex_rec_opt s i c =
  if i < 0 then None else
  if unsafe_get s i = c then Some i else rindex_rec_opt s (i - 1) c

let rindex_opt s c = rindex_rec_opt s (length s - 1) c

let rindex_from_opt s i c =
  if i < -1 || i >= length s then
    invalid_arg "ExtString.rindex_from_opt"
  else
    rindex_rec_opt s i c

#endif

end