File: astring_string.ml

package info (click to toggle)
ocaml-astring 0.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 396 kB
  • sloc: ml: 4,475; makefile: 16
file content (785 lines) | stat: -rw-r--r-- 24,822 bytes parent folder | download | duplicates (2)
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
(*---------------------------------------------------------------------------
   Copyright (c) 2015 The astring programmers. All rights reserved.
   Distributed under the ISC license, see terms at the end of the file.
  ---------------------------------------------------------------------------*)

open Astring_unsafe

let strf = Format.asprintf

(* String *)

type t = string

let empty = Astring_base.empty
let v ~len f =
  let b = Bytes.create len in
  for i = 0 to len - 1 do bytes_unsafe_set b i (f i) done;
  bytes_unsafe_to_string b

let length = string_length
let get = string_safe_get
let get_byte s i = char_to_byte (get s i)
let unsafe_get = string_unsafe_get
let unsafe_get_byte s i = char_to_byte (unsafe_get s i)

let head ?(rev = false) s =
  let len = length s in
  if len = 0 then None else
  Some (string_unsafe_get s (if rev then len - 1 else 0))

let get_head ?(rev = false) s =
  let len = length s in
  if len = 0 then invalid_arg Astring_base.err_empty_string else
  string_unsafe_get s (if rev then len - 1 else 0)

let hash c = Hashtbl.hash c

(* Appending strings *)

let append s0 s1 =
  let l0 = length s0 in
  if l0 = 0 then s1 else
  let l1 = length s1 in
  if l1 = 0 then s0 else
  let b = Bytes.create (l0 + l1) in
  bytes_unsafe_blit_string s0 0 b 0 l0;
  bytes_unsafe_blit_string s1 0 b l0 l1;
  bytes_unsafe_to_string b

let concat ?(sep = empty) = function
| [] -> empty
| [s] -> s
| s :: ss ->
    let s_len = length s in
    let sep_len = length sep in
    let rec cat_len sep_count l ss =
      if l < 0 then l else
      match ss with
      | s :: ss -> cat_len (sep_count + 1) (l + length s) ss
      | [] ->
          if sep_len = 0 then l else
          let max_sep_count = Sys.max_string_length / sep_len in
          if sep_count < 0 || sep_count > max_sep_count then -1 else
          sep_count * sep_len + l
    in
    let cat_len = cat_len 0 s_len ss in
    if cat_len < 0 then invalid_arg Astring_base.err_max_string_len else
    let b = Bytes.create cat_len in
    bytes_unsafe_blit_string s 0 b 0 s_len;
    let rec loop i = function
    | [] -> bytes_unsafe_to_string b
    | str :: ss ->
        let sep_first = i in
        let str_first = i + sep_len in
        let str_len = length str in
        bytes_unsafe_blit_string sep 0 b sep_first sep_len;
        bytes_unsafe_blit_string str 0 b str_first str_len;
        loop (str_first + str_len) ss
    in
    loop s_len ss

(* Predicates *)

let is_empty s = length s = 0

let is_prefix ~affix s =
  let len_a = length affix in
  let len_s = length s in
  if len_a > len_s then false else
  let max_idx_a = len_a - 1 in
  let rec loop i =
    if i > max_idx_a then true else
    if unsafe_get affix i <> unsafe_get s i then false else loop (i + 1)
  in
  loop 0

let is_infix ~affix s =
  let len_a = length affix in
  let len_s = length s in
  if len_a > len_s then false else
  let max_idx_a = len_a - 1 in
  let max_idx_s = len_s - len_a in
  let rec loop i k =
    if i > max_idx_s then false else
    if k > max_idx_a then true else
    if k > 0 then
      if unsafe_get affix k = unsafe_get s (i + k)
      then loop i (k + 1) else loop (i + 1) 0
    else
      if unsafe_get affix 0 = unsafe_get s i
      then loop i 1 else loop (i + 1) 0
  in
  loop 0 0

let is_suffix ~affix s =
  let max_idx_a = length affix - 1 in
  let max_idx_s = length s - 1 in
  if max_idx_a > max_idx_s then false else
  let rec loop i =
    if i > max_idx_a then true else
    if unsafe_get affix (max_idx_a - i) <> unsafe_get s (max_idx_s - i)
    then false
    else loop (i + 1)
  in
  loop 0

let for_all sat s = Astring_base.for_all sat s ~first:0 ~last:(length s - 1)
let exists sat s = Astring_base.exists sat s ~first:0 ~last:(length s - 1)
let equal = string_equal
let compare = string_compare

(* Extracting substrings *)

let with_range ?(first = 0) ?(len = max_int) s =
  if len < 0 then invalid_arg (Astring_base.err_neg_len len) else
  if len = 0 then empty else
  let s_len = length s in
  let max_idx = s_len - 1 in
  let last = match len with
  | len when len = max_int -> max_idx
  | len ->
      let last = first + len - 1 in
      if last > max_idx then max_idx else last
  in
  let first = if first < 0 then 0 else first in
  if first > max_idx || last < 0 || first > last then empty else
  if first = 0 && last = max_idx then s else
  unsafe_string_sub s first (last + 1 - first)

let with_index_range ?(first = 0) ?last s =
  let s_len = length s in
  let max_idx = s_len - 1 in
  let last = match last with
  | None -> max_idx
  | Some last -> if last > max_idx then max_idx else last
  in
  let first = if first < 0 then 0 else first in
  if first > max_idx || last < 0 || first > last then empty else
  if first = 0 && last = max_idx then s else
  unsafe_string_sub s first (last + 1 - first)

let trim ?(drop = Astring_char.Ascii.is_white) s =
  let len = length s in
  if len = 0 then s else
  let max_idx = len - 1 in
  let rec left_pos i =
    if i > max_idx then len else
    if drop (unsafe_get s i) then left_pos (i + 1) else i
  in
  let rec right_pos i =
    if i < 0 then 0 else
    if drop (unsafe_get s i) then right_pos (i - 1) else (i + 1)
  in
  let left = left_pos 0 in
  if left = len then empty else
  let right = right_pos max_idx in
  if left = 0 && right = len then s else
  unsafe_string_sub s left (right - left)

let fspan ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then (empty, s) else
  let len = length s in
  let max_idx = len - 1 in
  let max_idx = let k = max - 1 in (if k > max_idx then max_idx else k) in
  let need_idx = min in
  let rec loop i =
    if i <= max_idx && sat (unsafe_get s i) then loop (i + 1) else
    if i < need_idx || i = 0 then (empty, s) else
    if i = len then (s, empty) else
    unsafe_string_sub s 0 i, unsafe_string_sub s i (len - i)
  in
  loop 0

let rspan ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then (s, empty) else
  let len = length s in
  let max_idx = len - 1 in
  let min_idx = let k = len - max in (if k < 0 then 0 else k) in
  let need_idx = max_idx - min in
  let rec loop i =
    if i >= min_idx && sat (unsafe_get s i) then loop (i - 1) else
    if i > need_idx || i = max_idx then (s, empty) else
    if i = -1 then (empty, s) else
    let cut = i + 1 in
    unsafe_string_sub s 0 cut, unsafe_string_sub s cut (len - cut)
  in
  loop max_idx

let span ?(rev = false) ?min ?max ?sat s = match rev with
| true  -> rspan ?min ?max ?sat s
| false -> fspan ?min ?max ?sat s

(* N.B. c&p of fspan *)
let ftake ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then empty else
  let len = length s in
  let max_idx = len - 1 in
  let max_idx = let k = max - 1 in (if k > max_idx then max_idx else k) in
  let need_idx = min in
  let rec loop i =
    if i <= max_idx && sat (unsafe_get s i) then loop (i + 1) else
    if i < need_idx || i = 0 then empty else
    if i = len then s else
    unsafe_string_sub s 0 i
  in
  loop 0

(* N.B. c&p of rspan *)
let rtake ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then empty else
  let len = length s in
  let max_idx = len - 1 in
  let min_idx = let k = len - max in (if k < 0 then 0 else k) in
  let need_idx = max_idx - min in
  let rec loop i =
    if i >= min_idx && sat (unsafe_get s i) then loop (i - 1) else
    if i > need_idx || i = max_idx then empty else
    if i = -1 then s else
    let cut = i + 1 in
    unsafe_string_sub s cut (len - cut)
  in
  loop max_idx

let take ?(rev = false) ?min ?max ?sat s = match rev with
| true  -> rtake ?min ?max ?sat s
| false -> ftake ?min ?max ?sat s

(* N.B. c&p of fspan *)
let fdrop ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then s else
  let len = length s in
  let max_idx = len - 1 in
  let max_idx = let k = max - 1 in (if k > max_idx then max_idx else k) in
  let need_idx = min in
  let rec loop i =
    if i <= max_idx && sat (unsafe_get s i) then loop (i + 1) else
    if i < need_idx || i = 0 then s else
    if i = len then empty else
    unsafe_string_sub s i (len - i)
  in
  loop 0

(* N.B. c&p of rspan *)
let rdrop ?(min = 0) ?(max = max_int) ?(sat = fun _ -> true) s =
  if min < 0 then invalid_arg (Astring_base.err_neg_min min) else
  if max < 0 then invalid_arg (Astring_base.err_neg_max max) else
  if min > max || max = 0 then s else
  let len = length s in
  let max_idx = len - 1 in
  let min_idx = let k = len - max in (if k < 0 then 0 else k) in
  let need_idx = max_idx - min in
  let rec loop i =
    if i >= min_idx && sat (unsafe_get s i) then loop (i - 1) else
    if i > need_idx || i = max_idx then s else
    if i = -1 then empty else
    let cut = i + 1 in
    unsafe_string_sub s 0 cut
  in
  loop max_idx

let drop ?(rev = false) ?min ?max ?sat s = match rev with
| true  -> rdrop ?min ?max ?sat s
| false -> fdrop ?min ?max ?sat s

let fcut ~sep s =
  let sep_len = length sep in
  if sep_len = 0 then invalid_arg Astring_base.err_empty_sep else
  let s_len = length s in
  let max_sep_idx = sep_len - 1 in
  let max_s_idx = s_len - sep_len in
  let rec check_sep i k =
    if k > max_sep_idx then
      let r_start = i + sep_len in
      Some (unsafe_string_sub s 0 i,
            unsafe_string_sub s r_start (s_len - r_start))
    else
      if unsafe_get s (i + k) = unsafe_get sep k
      then check_sep i (k + 1)
      else scan (i + 1)
  and scan i =
    if i > max_s_idx then None else
    if unsafe_get s i = unsafe_get sep 0 then check_sep i 1 else scan (i + 1)
  in
  scan 0

let rcut ~sep s =
  let sep_len = length sep in
  if sep_len = 0 then invalid_arg Astring_base.err_empty_sep else
  let s_len = length s in
  let max_sep_idx = sep_len - 1 in
  let max_s_idx = s_len - 1 in
  let rec check_sep i k =
    if k > max_sep_idx then
      let r_start = i + sep_len in
      Some (unsafe_string_sub s 0 i,
            unsafe_string_sub s r_start (s_len - r_start))
    else
      if unsafe_get s (i + k) = unsafe_get sep k
      then check_sep i (k + 1)
      else rscan (i - 1)
  and rscan i =
    if i < 0 then None else
    if unsafe_get s i = unsafe_get sep 0 then check_sep i 1 else rscan (i - 1)
  in
  rscan (max_s_idx - max_sep_idx)

let cut ?(rev = false) ~sep s = if rev then rcut ~sep s else fcut ~sep s

let add_sub ~no_empty s ~start ~stop acc =
  if start = stop then (if no_empty then acc else empty :: acc) else
  unsafe_string_sub s start (stop - start) :: acc

let fcuts ~no_empty ~sep s =
  let sep_len = length sep in
  if sep_len = 0 then invalid_arg Astring_base.err_empty_sep else
  let s_len = length s in
  let max_sep_idx = sep_len - 1 in
  let max_s_idx = s_len - sep_len in
  let rec check_sep start i k acc =
    if k > max_sep_idx then
      let new_start = i + sep_len in
      scan new_start new_start (add_sub ~no_empty s ~start ~stop:i acc)
    else
      if unsafe_get s (i + k) = unsafe_get sep k
      then check_sep start i (k + 1) acc
      else scan start (i + 1) acc
  and scan start i acc =
    if i > max_s_idx then
      if start = 0 then (if no_empty && s_len = 0 then [] else [s]) else
      List.rev (add_sub ~no_empty s ~start ~stop:s_len acc)
    else
      if unsafe_get s i = unsafe_get sep 0
      then check_sep start i 1 acc
      else scan start (i + 1) acc
  in
  scan 0 0 []

let rcuts ~no_empty ~sep s =
  let sep_len = length sep in
  if sep_len = 0 then invalid_arg Astring_base.err_empty_sep else
  let s_len = length s in
  let max_sep_idx = sep_len - 1 in
  let max_s_idx = s_len - 1 in
  let rec check_sep stop i k acc =
    if k > max_sep_idx then
      let start = i + sep_len in
      rscan i (i - sep_len) (add_sub ~no_empty s ~start ~stop acc)
    else if unsafe_get s (i + k) = unsafe_get sep k
    then check_sep stop i (k + 1) acc
    else rscan stop (i - 1) acc
  and rscan stop i acc =
    if i < 0 then
      if stop = s_len then (if no_empty && s_len = 0 then [] else [s]) else
      add_sub ~no_empty s ~start:0 ~stop:stop acc
    else if unsafe_get s i = unsafe_get sep 0
    then check_sep stop i 1 acc
    else rscan stop (i - 1) acc
  in
  rscan s_len (max_s_idx - max_sep_idx) []

let cuts ?(rev = false) ?(empty = true) ~sep s = match rev with
| true  -> rcuts ~no_empty:(not empty) ~sep s
| false -> fcuts ~no_empty:(not empty) ~sep s

let fields ?(empty = true) ?(is_sep = Astring_char.Ascii.is_white) s =
  let no_empty = not empty in
  let max_pos = length s in
  let rec loop i end_pos acc =
    if i < 0 then begin
      if end_pos = max_pos
      then (if no_empty && max_pos = 0 then [] else [s])
      else add_sub ~no_empty s ~start:0 ~stop:end_pos acc
    end else begin
      if not (is_sep (unsafe_get s i)) then loop (i - 1) end_pos acc else
      loop (i - 1) i (add_sub ~no_empty s ~start:(i + 1) ~stop:end_pos acc)
    end
  in
  loop (max_pos - 1) max_pos []

(* Substrings *)

type sub = Astring_sub.t

module Sub = Astring_sub

let sub = Sub.v
let sub_with_range = Sub.of_string_with_range
let sub_with_index_range = Sub.of_string_with_index_range

(* Traversing *)

let ffind ?start sat s =
  let max_idx = length s - 1 in
  let rec loop i =
    if i > max_idx then None else
    if sat (unsafe_get s i) then Some i else loop (i + 1)
  in
  match start with
  | None -> loop 0
  | Some i when i < 0 -> loop 0
  | Some i -> loop i

let rfind ?start sat s =
  let max_idx = length s - 1 in
  let rec loop i =
    if i < 0 then None else
    if sat (unsafe_get s i) then Some i else loop (i - 1)
  in
  match start with
  | None -> loop max_idx
  | Some i when i > max_idx -> loop max_idx
  | Some i -> loop i

let find ?(rev = false) ?start sat s = match rev with
| false -> ffind ?start sat s
| true  -> rfind ?start sat s

let ffind_sub ?start ~sub s =
  let len_sub = length sub in
  let len_s = length s in
  let max_idx_sub = len_sub - 1 in
  let max_idx_s = if len_sub <> 0 then len_s - len_sub else len_s - 1 in
  let rec loop i k =
    if i > max_idx_s then None else
    if k > max_idx_sub then Some i else
    if k > 0 then
      if unsafe_get sub k = unsafe_get s (i + k)
      then loop i (k + 1) else loop (i + 1) 0
    else
      if unsafe_get sub 0 = unsafe_get s i
      then loop i 1 else loop (i + 1) 0
  in
  match start with
  | None -> loop 0 0
  | Some i when i < 0 -> loop 0 0
  | Some i -> loop i 0

let rfind_sub ?start ~sub s =
  let len_sub = length sub in
  let len_s = length s in
  let max_idx_sub = len_sub - 1 in
  let max_idx_s = if len_sub <> 0 then len_s - len_sub else len_s - 1 in
  let rec loop i k =
    if i < 0 then None else
    if k > max_idx_sub then Some i else
    if k > 0 then
      if unsafe_get sub k = unsafe_get s (i + k)
      then loop i (k + 1) else loop (i - 1) 0
    else
      if unsafe_get sub 0 = unsafe_get s i
      then loop i 1 else loop (i - 1) 0
  in
  match start with
  | None -> loop max_idx_s 0
  | Some i when i > max_idx_s -> loop max_idx_s 0
  | Some i -> loop i 0

let find_sub ?(rev = false) ?start ~sub s = match rev with
| false -> ffind_sub ?start ~sub s
| true  -> rfind_sub ?start ~sub s

let filter sat s =
  let max_idx = length s - 1 in
  let rec with_buf b k i = (* k is the write index in b *)
    if i > max_idx then Bytes.sub_string b 0 k else
    let c = unsafe_get s i in
    if sat c then (bytes_unsafe_set b k c; with_buf b (k + 1) (i + 1)) else
    with_buf b k (i + 1)
  in
  let rec try_no_alloc i =
    if i > max_idx then s else
    if (sat (unsafe_get s i)) then try_no_alloc (i + 1) else
    if i = max_idx then unsafe_string_sub s 0 i else
    let b = Bytes.of_string s in (* copy and overwrite starting from i *)
    with_buf b i (i + 1)
  in
  try_no_alloc 0

let filter_map f s =
  let max_idx = length s - 1 in
  let rec with_buf b k i = (* k is the write index in b *)
    if i > max_idx then
      (if k > max_idx then bytes_unsafe_to_string b else Bytes.sub_string b 0 k)
    else
    match f (unsafe_get s i) with
    | None -> with_buf b k (i + 1)
    | Some c -> bytes_unsafe_set b k c; with_buf b (k + 1) (i + 1)
  in
  let rec try_no_alloc i =
    if i > max_idx then s else
    let c = unsafe_get s i in
    match f c with
    | None ->
        if i = max_idx then unsafe_string_sub s 0 i else
        let b = Bytes.of_string s in
        with_buf b i (i + 1)
    | Some cm when cm <> c ->
        let b = Bytes.of_string s in
        bytes_unsafe_set b i cm;
        with_buf b (i + 1) (i + 1)
    | Some _ ->
        try_no_alloc (i + 1)
  in
  try_no_alloc 0

let map f s =
  let max_idx = length s - 1 in
  let rec with_buf b i =
    if i > max_idx then bytes_unsafe_to_string b else
    (bytes_unsafe_set b i (f (unsafe_get s i)); with_buf b (i + 1))
  in
  let rec try_no_alloc i =
    if i > max_idx then s else
    let c = unsafe_get s i in
    match f c with
    | cm when cm <> c ->
        let b = Bytes.of_string s in
        bytes_unsafe_set b i cm;
        with_buf b (i + 1)
    | _ ->
        try_no_alloc (i + 1)
  in
  try_no_alloc 0

let mapi f s =
  let max_idx = length s - 1 in
  let rec with_buf b i =
    if i > max_idx then bytes_unsafe_to_string b else
    (bytes_unsafe_set b i (f i (unsafe_get s i)); with_buf b (i + 1))
  in
  let rec try_no_alloc i =
    if i > max_idx then s else
    let c = unsafe_get s i in
    match f i c with
    | cm when cm <> c ->
        let b = Bytes.of_string s in
        bytes_unsafe_set b i cm;
        with_buf b (i + 1)
    | _ ->
        try_no_alloc (i + 1)
  in
  try_no_alloc 0

let fold_left f acc s =
  Astring_base.fold_left f acc s ~first:0 ~last:(length s - 1)

let fold_right f s acc =
  Astring_base.fold_right f s acc ~first:0 ~last:(length s - 1)

let iter f s = for i = 0 to length s - 1 do f (unsafe_get s i) done
let iteri f s = for i = 0 to length s - 1 do f i (unsafe_get s i) done

(* Strings as US-ASCII code point sequences *)

module Ascii = struct

  let is_valid s =
    let max_idx = length s - 1 in
    let rec loop i =
      if i > max_idx then true else
      if unsafe_get s i > Astring_char.Ascii.max_ascii then false else
      loop (i + 1)
    in
    loop 0

  (* Casing transforms *)

  let caseify is_not_case to_case s =
    let max_idx = length s - 1 in
    let caseify b i =
      for k = i to max_idx do
        bytes_unsafe_set b k (to_case (unsafe_get s k))
      done;
      bytes_unsafe_to_string b
    in
    let rec try_no_alloc i =
      if i > max_idx then s else
      if is_not_case (unsafe_get s i) then caseify (Bytes.of_string s) i else
      try_no_alloc (i + 1)
    in
    try_no_alloc 0

  let uppercase s =
    caseify Astring_char.Ascii.is_lower Astring_char.Ascii.uppercase s

  let lowercase s =
    caseify Astring_char.Ascii.is_upper Astring_char.Ascii.lowercase s

  let caseify_first is_not_case to_case s =
    if length s = 0 then s else
    let c = unsafe_get s 0 in
    if not (is_not_case c) then s else
    let b = Bytes.of_string s in
    bytes_unsafe_set b 0 (to_case c);
    bytes_unsafe_to_string b

  let capitalize s =
    caseify_first Astring_char.Ascii.is_lower Astring_char.Ascii.uppercase s

  let uncapitalize s =
    caseify_first Astring_char.Ascii.is_upper Astring_char.Ascii.lowercase s

  (* Escape *)

  let escape = Astring_escape.escape
  let unescape = Astring_escape.unescape
  let escape_string = Astring_escape.escape_string
  let unescape_string = Astring_escape.unescape_string
end

(* Pretty printing *)

let pp = Format.pp_print_string
let dump ppf s =
  Format.pp_print_char ppf '"';
  Format.pp_print_string ppf (Ascii.escape_string s);
  Format.pp_print_char ppf '"';
  ()

(* String sets and maps *)

module Set = struct
  include Set.Make (String)

  let pp ?sep:(pp_sep = Format.pp_print_cut) pp_elt ppf ss =
    let pp_elt elt is_first =
      if is_first then () else pp_sep ppf ();
      pp_elt ppf elt; false
    in
    ignore (fold pp_elt ss true)

  let dump_str = dump
  let dump ppf ss =
    let pp_elt elt is_first =
      if is_first then () else Format.fprintf ppf "@ ";
      Format.fprintf ppf "%a" dump_str elt;
      false
    in
    Format.fprintf ppf "@[<1>{";
    ignore (fold pp_elt ss true);
    Format.fprintf ppf "}@]";
    ()

  let err_empty () = invalid_arg "empty set"
  let err_absent s ss =
    invalid_arg (strf "%a not in set %a" dump_str s dump ss)

  let get_min_elt ss = try min_elt ss with Not_found -> err_empty ()
  let min_elt ss = try Some (min_elt ss) with Not_found -> None

  let get_max_elt ss = try max_elt ss with Not_found -> err_empty ()
  let max_elt ss = try Some (max_elt ss) with Not_found -> None

  let get_any_elt ss = try choose ss with Not_found -> err_empty ()
  let choose ss = try Some (choose ss) with Not_found -> None

  let get s ss = try find s ss with Not_found -> err_absent s ss
  let find s ss = try Some (find s ss) with Not_found -> None

  let of_list = List.fold_left (fun acc s -> add s acc) empty

  let of_stdlib_set s = s
  let to_stdlib_set s = s
end

module Map = struct
  include Map.Make (String)

  let err_empty () = invalid_arg "empty map"
  let err_absent s = invalid_arg (strf "%a is not bound in map" dump s)

  let get_min_binding m = try min_binding m with Not_found -> err_empty ()
  let min_binding m = try Some (min_binding m) with Not_found -> None

  let get_max_binding m = try max_binding m with Not_found -> err_empty ()
  let max_binding m = try Some (max_binding m) with Not_found -> None

  let get_any_binding m = try choose m with Not_found -> err_empty ()
  let choose m = try Some (choose m) with Not_found -> None

  let get k s = try find k s with Not_found -> err_absent k
  let find k m = try Some (find k m) with Not_found -> None

  let dom m = fold (fun k _ acc -> Set.add k acc) m Set.empty

  let of_list bs = List.fold_left (fun m (k,v) -> add k v m) empty bs

  let of_stdlib_map m = m
  let to_stdlib_map m = m

  let pp ?sep:(pp_sep = Format.pp_print_cut) pp_binding ppf (m : 'a t) =
    let pp_binding k v is_first =
      if is_first then () else pp_sep ppf ();
      pp_binding ppf (k, v); false
    in
    ignore (fold pp_binding m true)

  let dump_str = dump
  let dump pp_v ppf m =
    let pp_binding k v is_first =
      if is_first then () else Format.fprintf ppf "@ ";
      Format.fprintf ppf "@[<1>(@[%a@],@ @[%a@])@]" dump k pp_v v;
      false
    in
    Format.fprintf ppf "@[<1>{";
    ignore (fold pp_binding m true);
    Format.fprintf ppf "}@]";
    ()

  let dump_string_map ppf m = dump dump_str ppf m
end

type set = Set.t
type 'a map = 'a Map.t

(* Uniqueness *)

let uniquify ss =
  let add (seen, ss as acc) v =
    if Set.mem v seen then acc else (Set.add v seen, v :: ss)
  in
  List.rev (snd (List.fold_left add (Set.empty, []) ss))

(* OCaml base type conversions *)

let of_char = Astring_base.of_char
let to_char = Astring_base.to_char
let of_bool = Astring_base.of_bool
let to_bool = Astring_base.to_bool
let of_int = Astring_base.of_int
let to_int = Astring_base.to_int
let of_nativeint = Astring_base.of_nativeint
let to_nativeint = Astring_base.to_nativeint
let of_int32 = Astring_base.of_int32
let to_int32 = Astring_base.to_int32
let of_int64 = Astring_base.of_int64
let to_int64 = Astring_base.to_int64
let of_float = Astring_base.of_float
let to_float = Astring_base.to_float

(*---------------------------------------------------------------------------
   Copyright (c) 2015 The astring programmers

   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  ---------------------------------------------------------------------------*)