Source file batSubstring.ml
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
type t = string * int * int
let empty () = "", 0, 0
let to_string (s,o,l) = String.sub s o l
let of_string s = s, 0, String.length s
let make len c = String.make len c, 0, len
let create len = String.make len '\000', 0, len
reate
create 0 = empty ()
*)
let equal (s1,o1,l1) (s2,o2,l2) =
if l1 <> l2 then false
else
let rec loop i =
if i = l1 then true
else if s1.[i+o1] <> s2.[i+o2] then false
else loop (i + 1)
in loop 0
T equal
equal (of_string "abc") (of_string "abc") = true
equal (substring "aba" 0 1) (substring "aba" 2 1) = true
equal (substring "aba" 1 1) (substring "aba" 2 1) = false
equal (substring "abc" 0 2) (substring "cab" 1 2) = true
*)
let of_input inp =
let tempsize = 16384 in
let buf = Buffer.create tempsize
and tmp = Bytes.create tempsize in
let n = ref 0 in
while n := BatIO.input inp tmp 0 tempsize; !n > 0 do
BatBytesCompat.buffer_add_subbytes buf tmp 0 !n;
done;
Buffer.contents buf, 0, Buffer.length buf
let substring str off len =
let sl = String.length str in
if off < 0 then invalid_arg "Substring.substring: negative offset not allowed";
if len < 0 then invalid_arg "Substring.substring: negative length not allowed";
if off + len > sl then invalid_arg "Substring.substring: offset + length past end of string";
(str,off,len)
let unsafe_substring str off len =
(str, off, len)
*$T unsafe_substring
(unsafe_substring "foobar" 1 3) = (substring "foobar" 1 3)
*)
let extract s o = function
en -> substring s o len
| None -> substring s o (String.length s - o)
let all = of_string
let base s = s
let is_empty (_,_,len) = len = 0
is_empty
is_empty (substring "foobar" 0 0) = true
is_empty (substring "foobar" 0 2) = false
*)
let getc (str,off,len) =
if len = 0 then None else Some (str.[off], (str, off+1, len-1))
$T getc
getc (substring "foobar" 1 3) = Some ('o', substring "foobar" 2 2)
getc (empty ()) = None
*)
let first (str,off,len) = if len = 0 then None else Some str.[off]
let triml k (str,off,len) =
if k < 0 then invalid_arg "Substring.triml: negative trim not allowed";
if k > len then (str, off+len, 0) else (str, off+k, len-k)
let trimr k (str,off,len) =
if k < 0 then invalid_arg "Substring.trimr: negative trim not allowed";
if k > len then (str, off, 0) else (str, off, len-k)
let get (str, off, len) k =
if k < 0 then invalid_arg "Substring.get: negative index not allowed";
if k > len then invalid_arg "Substring.get: index outside of substring";
str.[off+k]
let size (_,_,len) = len
let length = size
let slice (str,off,len) off2 len2_opt =
if off2 < 0 then invalid_arg "Substring.slice: negative offset not allowed";
let len2 = match len2_opt with None -> len-off2 | Some i -> i in
if len2 + off2 > len then invalid_arg "Substring.slice: invalid slice";
(str, off+off2, len2)
let concat ssl =
let len = List.fold_left (fun acc (_,_,l) ->acc+l) 0 ssl in
let item = Bytes.create len in
let write =
let pos = ref 0 in
fun (s,o,len) -> Bytes.blit_string s o item !pos len; pos := !pos + len
in
List.iter write ssl;
Bytes.unsafe_to_string item
let explode (str,off,len) =
let rec exp i l =
if i < off then l else exp (i - 1) (str.[i] :: l) in
exp (off+len-1) []
let is_prefix str1 (str2, off, len) =
let l1 = String.length str1 in
if l1 > len then false
else
let rec loop i =
if i < 0 then true
else if str1.[i] <> str2.[off+i] then false
else loop (i-1) in
loop (pred l1)
let compare (str1, off1, len1) (str2, off2, len2) =
let rec loop i =
if i >= len1 then if i >= len2 then 0 else -1
else if i >= len2 then 1
else
let c1 = str1.[off1+i] and c2 = str2.[off2+i] in
if c1 > c2 then 1
else if c1 < c2 then -1
else loop (i+1)
in
loop 0
let index_from (str, off, len) i c =
let rec aux k =
if k = len then raise Not_found
else if str.[off+k] = c then k
else aux (k+1)
in
if i > len || i < 0 then invalid_arg "Substring.index_from"
else aux i
let index sus c = index_from sus 0 c
$T index
(try (index (substring "foobar" 1 3) 'y') with Not_found -> 0) = 0
index (substring "foobar" 1 3) 'b' = 2
*)
let rindex_from (str, off, len) i c =
let rec aux k =
if k < 0 then raise Not_found
else if str.[off+k] = c then k
else aux (k-1)
in
if i > len || i < 0 then invalid_arg "Substring.rindex_from"
else aux i
let rindex sus c = rindex_from sus (size sus - 1) c
let contains ss c = try ignore (index ss c); true with Not_found -> false
(** not implemented: collate *)
let dropl p (str,off,len) =
let i = ref 0 in
while !i < len && p str.[off+ !i] do incr i; done;
(str, off+ !i, len- !i)
let dropr p (str, off, len) =
let i = ref len in
while !i > 0 && p str.[off+ !i - 1] do decr i; done;
(str, off, !i)
let takel p (str,off,len) =
let i = ref 0 in
while !i < len && p str.[off+ !i] do incr i; done;
(str, off, !i)
let taker p (str, off, len) =
let i = ref len in
while !i > 0 && p str.[off+ !i - 1] do decr i; done;
(str, off+ !i, len- !i)
let splitl p (str, off, len) =
let i = ref 0 in
while !i < len && p str.[off+ !i] do incr i; done;
(str, off, !i), (str, off+ !i, len- !i)
let splitr p (str, off, len) =
let i = ref len in
while !i > 0 && p str.[off+ !i - 1] do decr i; done;
(str, off, !i), (str, off+ !i, len- !i)
let split_at k (str, off, len) =
if k < 0 then invalid_arg "Substring.split_at: negative index";
if k > len then invalid_arg "Substring.split_at: can't split past end of string";
(str, off, k), (str, off+k, len-k)
(** not implemented: position *)
let span (str1, off1, _len1) (str2, off2, len2) =
if str1 <> str2 then invalid_arg
"Substring.span: must be substrings of same parent";
if off1 > off2 + len2 then invalid_arg
"Substring.span: first substring must not be to the right of the second";
(str1, off1, off2+len2-off1)
$T span
(try (span (substring "foo" 0 3) (substring "bar" 0 3)) with Invalid_argument "Substring.span: must be substrings of same parent" -> empty ()) = empty ()
(try (span (substring "foobar" 4 2) (substring "foobar" 0 3)) with Invalid_argument "Substring.span: first substring must not be to the right of the second" -> empty ()) = empty ()
span (substring "foobar" 0 3) (substring "foobar" 3 3) = (substring "foobar" 0 6)
span (substring "foobar" 3 3) (substring "foobar" 0 3) = (substring "foobar" 3 0)
*)
let translate f (str,off,len) =
BatString.init len (fun i -> f str.[off+i])
let tokens p (str,off,len) =
let i = ref 0 and j = ref 0 and acc = BatRefList.empty () in
while !j < len do
while !i < len && p str.[off+ !i] do incr i; done;
j := !i+1;
while !j < len && not (p str.[off+ !j]) do incr j; done;
BatRefList.push acc (str, !i, !j - !i);
i := !j+1;
done;
BatRefList.to_list acc
let fields p (str, off, len) =
let i = ref 0 and j = ref 0 and acc = BatRefList.empty() in
while !j < len do
while !j < len && not (p str.[off+ !j]) do incr j; done;
BatRefList.push acc (str, !i, !j - !i);
incr j; i := !j;
done;
BatRefList.to_list acc
let fold_left f init (str, off, len) =
let rec loop i result =
if (i-off) = len then result
else loop (i + 1) (f result str.[i])
in
loop off init
let fold_lefti f init (str, off, len) =
let rec loop i result =
if (i-off) = len then result
else loop (i + 1) (f result (i-off) str.[i])
in loop off init
let fold_right f (str, off, len) init =
let rec loop i result =
if i = off then result
else loop (i - 1) (f str.[i-1] result)
in
loop (off+len) init
let fold_righti f (str, off, len) init =
let rec loop i result =
if i = off then result
else
let i' = i - 1 in
loop (i - 1) (f (i' - off) str.[i'] result)
in loop (off+len) init
let iter f (str, off, len) =
for i = off to off+len-1 do
f str.[i];
done
let iteri f (str, off, len) =
for i = 0 to len-1 do f i str.[i+off] done
let trim x = dropl BatChar.is_whitespace (dropr BatChar.is_whitespace x)
let split_on_char c (str, off, len) =
let rec loop acc last_pos pos =
if pos = off - 1 then
(str, off, last_pos - off) :: acc
else
if str.[pos] = c then
let pos1 = pos + 1 in
let sub_str = str,pos1,(last_pos - pos1) in
loop (sub_str :: acc) pos (pos - 1)
else loop acc last_pos (pos - 1)
in
loop [] (off+len) (off + len - 1)
let split_on_pipe str = split_on_char '|' str
let split_on_dot str = split_on_char '.' str
let split_on_comma str = split_on_char ',' str
let split_on_slash str = split_on_char '/' str
let rec enum (str, off, len) =
let last_element = off + len - 1 in
let i = ref off in
BatEnum.make
~next:(fun () ->
if !i > last_element then raise BatEnum.No_more_elements
else str.[BatRef.post_incr i] )
~count:(fun () -> len - !i)
~clone:(fun () -> enum (str, !i, len - !i))
let print oc ss = iter (fun c -> BatIO.write oc c) ss
let append_to_buffer buff ss =
let str, ofs, len = base ss in
BatBuffer.add_substring buff str ofs len