ocaml-core / base / core / lib / core_string.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
 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
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
module Array = Caml.ArrayLabels
module Char = Core_char
module String = Caml.StringLabels
module List = Core_list
module Stack = Core_stack
open Sexplib.Std
open Bin_prot.Std
open Staged

let phys_equal = Caml.(==)

let invalid_argf = Core_printf.invalid_argf

let failwiths = Error.failwiths

module T = struct
  include Comparator.Make_binable (struct
    type t = string with sexp, bin_io
    let compare = String.compare
  end)

  (* = on two strings avoids calling compare_val, which is what happens
     with String.compare *)
  let equal (x : string) y = x = y
end

include T

type elt = char

let max_length = Caml.Sys.max_string_length

(* Standard functions *)
let blit = String.blit
let capitalize = String.capitalize
let concat ?(sep="") l = String.concat ~sep l
let copy = String.copy
let escaped = String.escaped
let fill = String.fill
let index_exn = String.index
let index_from_exn = String.index_from
let length = String.length
let lowercase = String.lowercase
let make = String.make
let rindex_exn = String.rindex
let rindex_from_exn = String.rindex_from
let sub = String.sub
let uncapitalize = String.uncapitalize
let uppercase = String.uppercase
external create : int -> string = "caml_create_string"
external get : string -> int -> char = "%string_safe_get"
external length : string -> int = "%string_length"
external set : string -> int -> char -> unit = "%string_safe_set"

let contains ?pos ?len t char =
  let (pos, len) =
    Ordered_collection_common.get_pos_len_exn ?pos ?len ~length:(length t)
  in
  let last = pos + len in
  let rec loop i = i < last && (t.[i] = char || loop (i + 1)) in
  loop pos
;;

TEST = contains "" 'a' = false
TEST = contains "a" 'a' = true
TEST = contains "a" 'b' = false
TEST = contains "ab" 'a' = true
TEST = contains "ab" 'b' = true
TEST = contains "ab" 'c' = false
TEST = contains "abcd" 'b' ~pos:1 ~len:0 = false
TEST = contains "abcd" 'b' ~pos:1 ~len:1 = true
TEST = contains "abcd" 'c' ~pos:1 ~len:2 = true
TEST = contains "abcd" 'd' ~pos:1 ~len:2 = false
TEST = contains "abcd" 'd' ~pos:1 = true
TEST = contains "abcd" 'a' ~pos:1 = false

let index t char =
  try Some (index_exn t char)
  with Not_found -> None

let rindex t char =
  try Some (rindex_exn t char)
  with Not_found -> None

let index_from t pos char =
  try Some (index_from_exn t pos char)
  with Not_found -> None

let rindex_from t pos char =
  try Some (rindex_from_exn t pos char)
  with Not_found -> None

let id x = x
let of_string = id
let to_string = id

let iter t ~f = String.iter t ~f

let init n ~f =
  if n < 0 then invalid_argf "String.init %d" n ();
  let t = create n in
  for i = 0 to n - 1 do
    t.[i] <- f i;
  done;
  t
;;

(** See {!Core_array.normalize} for the following 4 functions. *)
let normalize t i =
  Ordered_collection_common.normalize ~length_fun:String.length t i
let slice t start stop =
  Ordered_collection_common.slice ~length_fun:String.length ~sub_fun:String.sub
    t start stop

let nget x i =
  x.[normalize x i]
let nset x i v =
  x.[normalize x i] <- v

let invalid_argf = Core_printf.invalid_argf

let to_list s =
  let rec loop acc i =
    if i < 0 then
      acc
    else
      loop (s.[i] :: acc) (i-1)
  in
  loop [] (String.length s - 1)

let to_list_rev s =
  let len = String.length s in
  let rec loop acc i =
    if i = len then
      acc
    else
      loop (s.[i] :: acc) (i+1)
  in
  loop [] 0

(** Efficient string splitting *)

let lsplit2_exn line ~on:delim =
  let pos = String.index line delim in
  (String.sub line ~pos:0 ~len:pos,
   String.sub line ~pos:(pos+1) ~len:(String.length line - pos - 1)
  )

let rsplit2_exn line ~on:delim =
  let pos = String.rindex line delim in
  (String.sub line ~pos:0 ~len:pos,
   String.sub line ~pos:(pos+1) ~len:(String.length line - pos - 1)
  )

let lsplit2 line ~on =
  try Some (lsplit2_exn line ~on) with Not_found -> None

let rsplit2 line ~on =
  try Some (rsplit2_exn line ~on) with Not_found -> None

let rec char_list_mem l (c:char) =
  match l with
  | [] -> false
  | hd::tl -> hd = c || char_list_mem tl c

let split_gen str ~on =
  let is_delim =
    match on with
    | `char c' -> (fun c -> c = c')
    | `char_list l -> (fun c -> char_list_mem l c)
  in
  let len = String.length str in
  let rec loop acc last_pos pos =
    if pos = -1 then
      String.sub str ~pos:0 ~len:last_pos :: acc
    else
      if is_delim str.[pos] then
        let pos1 = pos + 1 in
        let sub_str = String.sub str ~pos:pos1 ~len:(last_pos - pos1) in
        loop (sub_str :: acc) pos (pos - 1)
    else loop acc last_pos (pos - 1)
  in
  loop [] len (len - 1)
;;

let split str ~on = split_gen str ~on:(`char on) ;;

let split_on_chars str ~on:chars =
  split_gen str ~on:(`char_list chars)
;;

(* [is_suffix s ~suff] returns [true] if the string [s] ends with the suffix [suff] *)
let is_suffix s ~suffix =
  let len_suff = String.length suffix in
  let len_s = String.length s in
  len_s >= len_suff
  && (let rec loop i =
        i = len_suff || (suffix.[len_suff - 1 - i] = s.[len_s - 1 - i] && loop (i + 1))
      in
      loop 0)

let is_prefix s ~prefix =
  let len_pref = String.length prefix in
  String.length s >= len_pref
  && (let rec loop i =
        i = len_pref || (prefix.[i] = s.[i] && loop (i + 1))
      in
      loop 0)
;;

let wrap_sub_n t n ~name ~pos ~len ~on_error =
  if n < 0 then
    invalid_arg (name ^ " expecting nonnegative argument")
  else
    try
      sub t ~pos ~len
    with _ ->
      on_error

let drop_prefix t n = wrap_sub_n ~name:"drop_prefix" t n ~pos:n ~len:(length t - n) ~on_error:""
let drop_suffix t n = wrap_sub_n ~name:"drop_suffix" t n ~pos:0 ~len:(length t - n) ~on_error:""
let prefix t n = wrap_sub_n ~name:"prefix" t n ~pos:0 ~len:n ~on_error:t
let suffix t n = wrap_sub_n ~name:"suffix" t n ~pos:(length t - n) ~len:n ~on_error:t

let lfindi ?(pos=0) t ~f =
  let n = length t in
  let rec loop i =
    if i = n then None
    else if f i t.[i] then Some i
    else loop (i + 1)
  in
  loop pos
;;

TEST = lfindi "bob" ~f:(fun _ c -> 'b' = c) = Some 0
TEST = lfindi ~pos:0 "bob" ~f:(fun _ c -> 'b' = c) = Some 0
TEST = lfindi ~pos:1 "bob" ~f:(fun _ c -> 'b' = c) = Some 2
TEST = lfindi "bob" ~f:(fun _ c -> 'x' = c) = None

let find t ~f =
  match lfindi t ~f:(fun _ c -> f c) with
  | None -> None | Some i -> Some t.[i]

let find_map t ~f =
  let n = length t in
  let rec loop i =
    if i = n then None
    else
      match f t.[i] with
      | None -> loop (i + 1)
      | Some _ as res -> res
  in
  loop 0
;;

TEST = find_map "fop" ~f:(fun c -> if c >= 'o' then Some c else None) = Some 'o'
TEST = find_map "bar" ~f:(fun _ -> None) = None
TEST = find_map "" ~f:(fun _ -> assert false) = None

let rfindi ?pos t ~f =
  let rec loop i =
    if i < 0 then None
    else begin
      if f i t.[i] then Some i
      else loop (i - 1)
    end
  in
  let pos =
    match pos with
    | Some pos -> pos
    | None -> length t - 1
  in
  loop pos
;;

TEST = rfindi "bob" ~f:(fun _ c -> 'b' = c) = Some 2
TEST = rfindi ~pos:2 "bob" ~f:(fun _ c -> 'b' = c) = Some 2
TEST = rfindi ~pos:1 "bob" ~f:(fun _ c -> 'b' = c) = Some 0
TEST = rfindi "bob" ~f:(fun _ c -> 'x' = c) = None

let last_non_whitespace t = rfindi t ~f:(fun _ c -> not (Char.is_whitespace c))

let rstrip t =
  match last_non_whitespace t with
  | None -> ""
  | Some i ->
    if i = length t - 1
    then t
    else prefix t (i + 1)
;;

let first_non_whitespace t = lfindi t ~f:(fun _ c -> not (Char.is_whitespace c))

let lstrip t =
  match first_non_whitespace t with
  | None -> ""
  | Some 0 -> t
  | Some n -> drop_prefix t n
;;

(* [strip t] could be implemented as [lstrip (rstrip t)].  The implementation
   below saves (at least) a factor of two allocation, by only allocating the
   final result.  This also saves some amount of time. *)
let strip t =
  let length = length t in
  if length = 0
    || not (Char.is_whitespace t.[0] || Char.is_whitespace t.[length - 1])
  then t
  else
    match first_non_whitespace t with
    | None -> ""
    | Some first ->
        match last_non_whitespace t with
        | None -> assert false
        | Some last -> sub t ~pos:first ~len:(last - first + 1)
;;

let mapi t ~f =
  let l = String.length t in
  let t' = String.create l in
  for i = 0 to l - 1 do
    t'.[i] <- f i t.[i]
  done;
  t'

(* repeated code to avoid requiring an extra allocation for a closure on each call. *)
let map t ~f =
  let l = String.length t in
  let t' = String.create l in
  for i = 0 to l - 1 do
    t'.[i] <- f t.[i]
  done;
  t'

let to_array s = Array.init (String.length s) ~f:(fun i -> s.[i])

let tr ~target ~replacement s = map ~f:(fun c -> if c = target then replacement else c) s

let tr_inplace ~target ~replacement s = (* destructive version of tr *)
  for i = 0 to String.length s - 1 do
    if s.[i] = target then s.[i] <- replacement
  done

let exists s ~f =
  let rec loop i = i > 0 && (let i = i - 1 in f s.[i] || loop i) in
  loop (length s)
;;

let for_all s ~f =
  let rec loop i = i = 0 || (let i = i - 1 in f s.[i] && loop i) in
  loop (length s)
;;

let fold t ~init ~f =
  let n = length t in
  let rec loop i ac = if i = n then ac else loop (i + 1) (f ac t.[i]) in
  loop 0 init
;;

let foldi t ~init ~f =
  let n = length t in
  let rec loop i ac = if i = n then ac else loop (i + 1) (f i ac t.[i]) in
  loop 0 init
;;

TEST = (foldi "hello" ~init:[] ~f:(fun i acc ch -> (i,ch)::acc)
        = List.rev [0,'h';1,'e';2,'l';3,'l';4,'o'])

let count t ~f = Container.fold_count fold t ~f

let is_empty t = String.length t = 0

let mem ?(equal = Char.(=)) t c =
  let rec loop i = i < length t && (equal c t.[i] || loop (i + 1)) in
  loop 0
;;

let concat_array ?sep ar = concat ?sep (Array.to_list ar)

let concat_map ?sep s ~f = concat_array ?sep (Array.map (to_array s) ~f)

let chop_prefix s ~prefix =
  if is_prefix s ~prefix then
    Some (drop_prefix s (String.length prefix))
  else
    None

let chop_prefix_exn s ~prefix =
  match chop_prefix s ~prefix with
  | Some str -> str
  | None ->
      raise (Invalid_argument
               (Printf.sprintf "Core_string.chop_prefix_exn %S %S" s prefix))

let chop_suffix s ~suffix =
  if is_suffix s ~suffix then
    Some (drop_suffix s (String.length suffix))
  else
    None

let chop_suffix_exn s ~suffix =
  match chop_suffix s ~suffix with
  | Some str -> str
  | None ->
      raise (Invalid_argument
               (Printf.sprintf "Core_string.chop_suffix_exn %S %S" s suffix))

(* The following function returns exactly the same results as the standard hash function
   on strings (it performs exactly the same computation), but it is faster on short
   strings (because we don't have to call the generic C function). For random strings of
   length 4 to 6, it is 40% faster. For strings of length 30 or more, the standard hash
   function is faster.
*)
let hash s =
  let len = String.length s in
  if len = 0 then 0
  else if len > 30 then Hashtbl.hash_param 1 1 s
  else
    let res = ref (int_of_char (String.unsafe_get s 0)) in
    for i = 1 to len - 1 do
      res := !res * 19 + int_of_char (String.unsafe_get s i)
    done;
    !res land 0x3FFFFFFF

module Infix = struct
  let ( </> ) str (start,stop) = slice str start stop
end

include (Hashable.Make_binable (struct
  include T
  let hash = hash
end):Hashable.S_binable with type t := t)
module Map = Core_map.Make_binable (T)
module Set = Core_set.Make_binable (T)

(* for interactive top-levels -- modules deriving from String should have String's pretty
   printer. *)
let pp ppf s = Format.fprintf ppf "%s" s

(* fast version, if we ever need it:
  let concat_array ~sep ar =
  let ar_len = Array.length ar in
  if ar_len = 0 then ""
  else
    let sep_len = String.length sep in
    let res_len_ref = ref (sep_len * (ar_len - 1)) in
    for i = 0 to ar_len - 1 do
      res_len_ref := !res_len_ref + String.length ar.(i)
    done;
    let res = String.create !res_len_ref in
    let str_0 = ar.(0) in
    let len_0 = String.length str_0 in
    String.blit ~src:str_0 ~src_pos:0 ~dst:res ~dst_pos:0 ~len:len_0;
    let pos_ref = ref len_0 in
    for i = 1 to ar_len - 1 do
      let pos = !pos_ref in
      String.blit ~src:sep ~src_pos:0 ~dst:res ~dst_pos:pos ~len:sep_len;
      let new_pos = pos + sep_len in
      let str_i = ar.(i) in
      let len_i = String.length str_i in
      String.blit ~src:str_i ~src_pos:0 ~dst:res ~dst_pos:new_pos ~len:len_i;
      pos_ref := new_pos + len_i
    done;
    res
  *)

let of_char c = String.make 1 c

let of_char_list l =
  let t = create (List.length l) in
  List.iteri l ~f:(fun i c -> t.[i] <- c);
  t

TEST = of_char_list ['a';'b';'c'] = "abc"
TEST = of_char_list [] = ""

module Escaping = struct

  (* If this is changed, make sure to update [escape], which attempts to ensure all the
     invariants checked here.  *)
  let build_and_validate_escapeworthy_map escapeworthy_map escape_char =
    let escapeworthy_map =
      if List.Assoc.mem escapeworthy_map escape_char then
        escapeworthy_map
      else
        (escape_char, escape_char) :: escapeworthy_map
    in
    (* Check that if escapeworthy_map is one-to-one. *)
    List.fold escapeworthy_map ~init:(Char.Set.empty, Char.Set.empty)
      ~f:(fun (acc_from, acc_to) (c_from, c_to) ->
        if Char.Set.mem acc_from c_from || Char.Set.mem acc_to c_to
        then begin
          failwiths "escapeworthy_map not one-to-one"
            (c_from, c_to, escapeworthy_map)
            (<:sexp_of< char * char * (char * char) list >>)
        end
        else Char.Set.add acc_from c_from, Char.Set.add acc_to c_to)
    |! ignore;
    escapeworthy_map
  ;;

  let escape_gen_exn ~escapeworthy_map ~escape_char =
    let escapeworthy_map =
      build_and_validate_escapeworthy_map escapeworthy_map escape_char
    in
    let escapeworthy =
      let a = Array.create 256 (-1) in
      List.iter ~f:(fun (k, v) -> a.(Char.to_int k) <- Char.to_int v)
        escapeworthy_map;
      a
    in
    stage (fun src ->
      (* calculate a list of (index of char to escape * escaped char) first, the order is
         from tail to head *)
      let to_escape =
        foldi src ~init:[] ~f:(fun i acc c ->
          match escapeworthy.(Char.to_int c) with
          | -1 -> acc
          | n ->
            (* (index of char to escape * escaped char) *)
            (i, Char.unsafe_of_int n) :: acc)
      in
      match to_escape with
      | [] -> src
      | _ ->
        (* [to_escape] divide [src] to [List.length to_escape + 1] pieces separated by the
           chars to escape.

           For example there are 3 chars to escape, [to_escape] ([(c); (b); (a)]) divides
           [src] to 4 pieces (indicated by [---0---; ---1---; ---2---; ---3---]),

           ---0---(a)---1---(b)---2---(c)---3---

           Then we create a [dst] of length [String.length src + 3] to store the result,
           copy piece ---3--- to [dst] directly, then copy [escape_char][escaped_char] of
           (c) to [dst]; then move on to next; after 3 iterations, copy piece ---3--- and
           we are done.

           Suppose (a), (b), (c) are escaped to (\A), (\B) and (\C), then the
           result will be

           ---0---(\A)---1---(\B)---2---(\C)---3---
        *)
        (* set [last_dst_pos] and [last_idx] to length of [dst] and [src] first *)
        let dst = String.create (String.length src + List.length to_escape) in
        (* index of last char to escape *)
        let last_idx = ref (String.length src) in
        let last_dst_pos = ref (String.length dst) in
        List.iter to_escape ~f:(fun (idx (*index of char to escape*), escaped_char) ->
          (* take first iteration for example *)
          (* calculate length of ---3---, minus 1 because we don't copy (c) *)
          let len = !last_idx - idx - 1 in
          (* set the dst_pos to copy to *)
          let dst_pos = !last_dst_pos - len in
          (* copy ---3---, set [src_pos] to [idx + 1] to skip (c) *)
          blit ~src ~src_pos:(idx + 1) ~dst ~dst_pos ~len;
          (* backoff [dst_pos] by 2 to copy the [escape_char] and [escaped_char] of (c) *)
          let dst_pos = dst_pos - 2 in
          dst.[dst_pos] <- escape_char;
          dst.[dst_pos + 1] <- escaped_char;
          (* update [last_dst_pos] and [last_idx] *)
          last_dst_pos := dst_pos;
          last_idx := idx
        );
        (* copy ---0--- *)
        blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len:!last_idx;
        dst
    )
  ;;

  let escape_gen ~escapeworthy_map ~escape_char =
    Or_error.try_with (fun () ->
      unstage (escape_gen_exn ~escapeworthy_map ~escape_char))
  ;;

  TEST_MODULE "escape_gen" = struct
    let escape = unstage
      (escape_gen_exn
         ~escapeworthy_map:[('%','p');('^','c')] ~escape_char:'_')

    TEST = escape "" = ""
    TEST = escape "foo" = "foo"
    TEST = escape "_" = "__"
    TEST = escape "foo%bar" = "foo_pbar"
    TEST = escape "^foo%" = "_cfoo_p"

    let escape2 = unstage
      (escape_gen_exn
         ~escapeworthy_map:[('_','.');('%','p');('^','c')] ~escape_char:'_')

    TEST = escape2 "_." = "_.."
    TEST = escape2 "_" = "_."
    TEST = escape2 "foo%_bar" = "foo_p_.bar"
    TEST = escape2 "_foo%" = "_.foo_p"

    let checks_for_one_to_one escapeworthy_map =
      try
        let _escape = escape_gen_exn ~escapeworthy_map ~escape_char:'_' in
        false
      with _ -> true

    TEST = checks_for_one_to_one [('%','p');('^','c');('$','c')]
    TEST = checks_for_one_to_one [('%','p');('^','c');('%','d')]
  end

  let escape ~escapeworthy ~escape_char =
    (* For [escape_gen_exn], we don't know how to fix invalid escapeworthy_map so we have
       to raise exception; but in this case, we know how to fix duplicated elements in
       escapeworthy list, so we just fix it instead of raising exception to make this
       function easier to use.  *)
    let escapeworthy_map =
      List.map ~f:(fun c -> (c, c))
        (Char.Set.to_list (Char.Set.remove (Char.Set.of_list escapeworthy) escape_char))
    in
    try
      escape_gen_exn ~escapeworthy_map ~escape_char
    with _ -> assert false
  ;;

  let unescape_gen_exn ~escapeworthy_map ~escape_char =
    let escapeworthy_map =
      build_and_validate_escapeworthy_map escapeworthy_map escape_char
    in
    let escapeworthy =
      let a = Array.create 256 (-1) in
      List.iter ~f:(fun (k, v) -> a.(Char.to_int v) <- Char.to_int k)
        escapeworthy_map;
      a
    in
    stage (fun src ->
      (* Continue the example in [escape_gen_exn], now we unescape

         ---0---(\A)---1---(\B)---2---(\C)---3---

         back to

         ---0---(a)---1---(b)---2---(c)---3---

         We first calculate a list of indexes of escape chars, (\ in this example), then
         we can decide the length of result [String.length src - 3].

         Then we create a string [dst] to store the result, copy ---3--- to it, then copy
         unescaped char of (C), then move on to next iteration. After 3 iterations copy
         ---0--- and we are done.
      *)
      (* indexes of escape chars *)
      let to_unescape =
        let escaping = ref false in
        let to_unescape = Stack.create () in
        for i = 0 to length src - 1 do
          if !escaping then (
            escaping := false
          ) else if src.[i] = escape_char then (
            escaping := true;
            Stack.push to_unescape i
          )
        done;
        Stack.to_list to_unescape
      in
      match to_unescape with
      | [] -> src
      | _ ->
        let dst = create (String.length src - List.length to_unescape) in
        (* set [last_dst_pos] and [last_idx] to length of [dst] and [src] first *)
        let last_dst_pos = ref (String.length dst) in
        let last_idx = ref (String.length src) in
        List.iter to_unescape ~f:(fun idx (* index of [escape_char] *) ->
          (* take 1st iteration as example, calculate the length of ---3---, minus 2 to
             skip (\C) *)
          let len = !last_idx - idx - 2 in
          (* set [dst_pos] to the position to copy ---3--- to *)
          let dst_pos = !last_dst_pos - len in
          (* copy ---3--- *)
          blit ~src ~src_pos:(idx + 2) ~dst ~dst_pos ~len;
          (* backoff [dst_pos] by 1 to copy unescaped char (c) *)
          let dst_pos = dst_pos - 1 in
          dst.[dst_pos] <-
            ( match escapeworthy.(Char.to_int src.[idx + 1]) with
            | -1 -> src.[idx + 1]
            | n -> Char.unsafe_of_int n);
          (* update [last_dst_pos] and [last_idx] *)
          last_dst_pos := dst_pos;
          last_idx := idx;
        );
        (* copy ---0--- *)
        blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len:!last_idx;
        dst
    )
  ;;

  let unescape_gen ~escapeworthy_map ~escape_char =
    Or_error.try_with (fun () ->
      unstage (unescape_gen_exn ~escapeworthy_map ~escape_char))
  ;;


  TEST_MODULE "unescape_gen" = struct
    let unescape =
      unstage
        (unescape_gen_exn ~escapeworthy_map:['%','p';'^','c'] ~escape_char:'_')

    TEST = unescape "__" = "_"
    TEST = unescape "foo" = "foo"
    TEST = unescape "__" = "_"
    TEST = unescape "foo_pbar" = "foo%bar"
    TEST = unescape "_cfoo_p" = "^foo%"

    let unescape2 =
      unstage
        (unescape_gen_exn ~escapeworthy_map:['_','.';'%','p';'^','c'] ~escape_char:'_')

    TEST = unescape2 "__" = "_"
    TEST = unescape2 "_.." = "_."
    TEST = unescape2 "_." = "_"
    TEST = unescape2 "foo_p_.bar" = "foo%_bar"
    TEST = unescape2 "_.foo_p" = "_foo%"

    (* generate [n] random string and check if escaping and unescaping are consistent *)
    let random_test ~escapeworthy_map ~escape_char n =
      let escape =
        unstage (escape_gen_exn ~escapeworthy_map ~escape_char)
      in
      let unescape =
        unstage (unescape_gen_exn ~escapeworthy_map ~escape_char)
      in
      let test str =
        let escaped = escape str in
        let unescaped = unescape escaped in
        if str <> unescaped then
          failwith (
            Printf.sprintf
              "string: %s\nescaped string: %s\nunescaped string: %s"
              str escaped unescaped)
      in
      let array_random_elem arr =
        arr.(Random.int (Array.length arr))
      in
      let random_char =
        let print_chars =
          List.range (Char.to_int Char.min_value) (Char.to_int Char.max_value + 1)
          |! List.filter_map ~f:Char.of_int
          |! List.filter ~f:Char.is_print
          |! Array.of_list
        in
        fun () -> array_random_elem print_chars
      in
      let escapeworthy_chars =
        List.map escapeworthy_map ~f:fst |! Array.of_list
      in
      try
        for i = 0 to n - 1 do
          let str =
            List.init (Random.int 50) ~f:(fun _ ->
              let p = Random.int 100 in
              if p < 10 then
                escape_char
              else if p < 25 then
                array_random_elem escapeworthy_chars
              else
                random_char ()
            )
            |! of_char_list
          in
          test str
        done;
        true
      with e ->
        raise e;
        false

    TEST = random_test 1000 ~escapeworthy_map:['%','p';'^','c'] ~escape_char:'_'
    TEST = random_test 1000 ~escapeworthy_map:['_','.';'%','p';'^','c'] ~escape_char:'_'
  end

  let unescape ~escape_char =
    unescape_gen_exn ~escapeworthy_map:[] ~escape_char

  TEST_MODULE "unescape" = struct
    let unescape = unstage (unescape ~escape_char:'_')
    TEST = unescape "foo" = "foo"
    TEST = unescape "__" = "_"
    TEST = unescape "foo_%bar" = "foo%bar"
    TEST = unescape "_^foo_%" = "^foo%"
  end

  (* In an escaped string, any char is either `Escaping, `Escaped or `Literal. For
     example, the escape statuses of chars in string "a_a__" with escape_char = '_' are

       a : `Literal
       _ : `Escaping
       a : `Escaped
       _ : `Escaping
       _ : `Escaped

     [update_escape_status str ~escape_char i previous_status] gets escape status of
     str.[i] basing on escape status of str.[i - 1]
  *)
  let update_escape_status str ~escape_char i = function
    | `Escaping -> `Escaped
    | `Literal
    | `Escaped -> if str.[i] = escape_char then `Escaping else `Literal
  ;;

  (* Get escape status of str.[pos] *)
  let escape_status str ~escape_char pos =
    let rec loop i status =
      let status = update_escape_status str ~escape_char i status in
      if i = pos then status else loop (i + 1) status
    in
    loop 0 `Literal
  ;;

  let check_bound str pos function_name =
    if pos >= String.length str || pos < 0 then
      invalid_argf "%s: out of bounds" function_name ()
  ;;

  let is_char_escaping str ~escape_char pos =
    check_bound str pos "is_char_escaping";
    escape_status str ~escape_char pos = `Escaping
  ;;

  TEST_MODULE "is_char_escaping" = struct
    let is = is_char_escaping ~escape_char:'_'
    TEST = is "___" 0 = true
    TEST = is "___" 1 = false
    TEST = is "___" 2 = true (* considered escaping, though there's nothing to escape *)

    TEST = is "a_b__c" 0 = false
    TEST = is "a_b__c" 1 = true
    TEST = is "a_b__c" 2 = false
    TEST = is "a_b__c" 3 = true
    TEST = is "a_b__c" 4 = false
    TEST = is "a_b__c" 5 = false
  end

  let is_char_escaped str ~escape_char pos =
    check_bound str pos "is_char_escaped";
    escape_status str ~escape_char pos = `Escaped
  ;;

  TEST_MODULE "is_char_escaped" = struct
    let is = is_char_escaped ~escape_char:'_'
    TEST = is "___" 2 = false
    TEST = is "x" 0 = false
    TEST = is "_x" 1 = true
    TEST = is "sadflkas____sfff" 12 = false
    TEST = is "s_____s" 6 = true
  end

  let is_char_literal str ~escape_char pos =
    check_bound str pos "is_char_literal";
    escape_status str ~escape_char pos = `Literal
  ;;

  TEST_MODULE "is_char_literal" = struct
    let is_char_literal = is_char_literal ~escape_char:'_'
    TEST = is_char_literal "123456" 4 = true
    TEST = is_char_literal "12345_6" 6 = false
    TEST = is_char_literal "12345_6" 5 = false
    TEST = is_char_literal "123__456" 4 = false
    TEST = is_char_literal "123456__" 7 = false
    TEST = is_char_literal "__123456" 1 = false
    TEST = is_char_literal "__123456" 0 = false
    TEST = is_char_literal "__123456" 2 = true
  end

  let index_from str ~escape_char pos char =
    let rec loop i status =
      if i >= length str then None
      else
        let status = update_escape_status str ~escape_char i status in
        if i >= pos && status = `Literal && str.[i] = char then Some i
        else loop (i + 1) status
    in
    loop 0 `Literal
  ;;

  let index_from_exn str ~escape_char pos char =
    match index_from str ~escape_char pos char with
    | None -> raise Not_found
    | Some pos -> pos
  ;;

  let index str ~escape_char char = index_from str ~escape_char 0 char
  let index_exn str ~escape_char char = index_from_exn str ~escape_char 0 char

  TEST_MODULE "index_from" = struct
    let f = index_from ~escape_char:'_'
    TEST = f "__" 0 '_' = None
    TEST = f "_.." 0 '.' = Some 2
    TEST = f "1273456_7789" 3 '7' = Some 9
    TEST = f "1273_7456_7789" 3 '7' = Some 11
    TEST = f "1273_7456_7789" 3 'z' = None
  end

  let rindex_from str ~escape_char pos char =
    let rec loop i status last_found =
      if i > pos then last_found
      else
        let status = update_escape_status str ~escape_char i status in
        loop (i + 1) status
          (if status = `Literal && str.[i] = char then Some i else last_found)
    in
    loop 0 `Literal None
  ;;

  let rindex_from_exn str ~escape_char pos char =
    match rindex_from str ~escape_char pos char with
    | None -> raise Not_found
    | Some pos -> pos
  ;;

  let rindex str ~escape_char char =
    rindex_from str ~escape_char (String.length str - 1) char
  ;;

  let rindex_exn str ~escape_char char =
    match rindex_from str ~escape_char (String.length str - 1) char with
    | None -> raise Not_found
    | Some pos -> pos
  ;;

  TEST_MODULE "rindex_from" = struct
    let f = rindex_from ~escape_char:'_'
    TEST = f "__" 0 '_' = None
    TEST = f "123456_37839" 9 '3' = Some 2
    TEST = f "123_2321" 6 '2' = Some 6
    TEST = f "123_2321" 5 '2' = Some 1
  end

  (* [split_gen str ~escape_char ~on] works similarly to [String.split_gen], with an
     additional requirement: only split on literal chars, not escaping or escaped *)
  let split_gen str ~escape_char ~on =
    let is_delim = match on with
      | `char c' -> (fun c -> c = c')
      | `char_list l -> (fun c -> char_list_mem l c)
    in
    let len = String.length str in
    let rec loop acc status last_pos pos =
      if pos = len then
        List.rev (String.sub str ~pos:last_pos ~len:(len - last_pos) :: acc)
      else
        let status = update_escape_status str ~escape_char pos status in
        if status = `Literal && is_delim str.[pos] then
          let sub_str = String.sub str ~pos:last_pos ~len:(pos - last_pos) in
          loop (sub_str :: acc) status (pos + 1) (pos + 1)
        else loop acc status last_pos (pos + 1)
    in
    loop [] `Literal 0 0
  ;;

  let split str ~on = split_gen str ~on:(`char on) ;;

  let split_on_chars str ~on:chars =
    split_gen str ~on:(`char_list chars)
  ;;

  TEST_MODULE "split_on_gen" = struct
    let split = split_gen ~escape_char:'_' ~on:(`char ',')
    TEST = split "foo,bar,baz" = ["foo"; "bar"; "baz"]
    TEST = split "foo_,bar,baz" = ["foo_,bar"; "baz"]
    TEST = split "foo_,bar_,baz" = ["foo_,bar_,baz"]
    TEST = split "foo__,bar,baz" = ["foo__"; "bar"; "baz"]
    TEST = split "foo,bar,baz_," = ["foo"; "bar"; "baz_,"]
    TEST = split "foo,bar_,baz_,," = ["foo"; "bar_,baz_,"; ""]

    let split = split_gen ~escape_char:'_' ~on:(`char_list [',';':'])
    TEST = split "foo,bar:baz" = ["foo"; "bar"; "baz"]
    TEST = split "foo_,bar,baz" = ["foo_,bar"; "baz"]
    TEST = split "foo_:bar_,baz" = ["foo_:bar_,baz"]
    TEST = split "foo,bar,baz_," = ["foo"; "bar"; "baz_,"]
    TEST = split "foo:bar_,baz_,," = ["foo"; "bar_,baz_,"; ""]
  end

  let split2 str ~on ~escape_char f =
    match f str ~escape_char on with
    | None -> None
    | Some pos ->
      Some
        (String.sub str ~pos:0 ~len:pos,
         String.sub str ~pos:(pos + 1) ~len:(String.length str - pos - 1))
  ;;

  let split2_exn str ~on ~escape_char f =
    match split2 str ~on ~escape_char f with
    | None -> raise Not_found
    | Some x -> x
  ;;

  let lsplit2 str ~on ~escape_char = split2 str ~on ~escape_char index
  let rsplit2 str ~on ~escape_char = split2 str ~on ~escape_char rindex
  let lsplit2_exn str ~on ~escape_char = split2_exn str ~on ~escape_char index
  let rsplit2_exn str ~on ~escape_char = split2_exn str ~on ~escape_char rindex

  TEST_MODULE "split2" = struct
    let split2 = split2 ~escape_char:'_' ~on:','
    TEST = split2 "foo_,bar,baz_,0" index = Some ("foo_,bar", "baz_,0")
    TEST = split2 "foo_,bar,baz_,0" rindex = Some ("foo_,bar", "baz_,0")
    TEST = split2 "foo_,bar" index = None
    TEST = split2 "foo_,bar" rindex = None
  end
end
;;

let min (x : t) y = if x < y then x else y
let max (x : t) y = if x > y then x else y
let compare (x : t) y = compare x y
let ascending = compare
let descending x y = compare y x
let ( >= ) x y = (x : t) >= y
let ( <= ) x y = (x : t) <= y
let ( = ) x y = (x : t) = y
let ( > ) x y = (x : t) > y
let ( < ) x y = (x : t) < y
let ( <> ) x y = (x : t) <> y
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.