forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescript.ml
15650 lines (13690 loc) · 531 KB
/
rescript.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
module Ext_array : sig
#1 "ext_array.mli"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
val reverse_range : 'a array -> int -> int -> unit
(** Some utilities for {!Array} operations *)
val reverse_in_place : 'a array -> unit
val reverse : 'a array -> 'a array
val reverse_of_list : 'a list -> 'a array
val filter : 'a array -> ('a -> bool) -> 'a array
val filter_map : 'a array -> ('a -> 'b option) -> 'b array
val range : int -> int -> int array
val map2i : (int -> 'a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
val to_list_f : 'a array -> ('a -> 'b) -> 'b list
val to_list_map : 'a array -> ('a -> 'b option) -> 'b list
val to_list_map_acc : 'a array -> 'b list -> ('a -> 'b option) -> 'b list
val of_list_map : 'a list -> ('a -> 'b) -> 'b array
val rfind_with_index : 'a array -> ('a -> 'b -> bool) -> 'b -> int
type 'a split = No_split | Split of 'a array * 'a array
val find_and_split : 'a array -> ('a -> 'b -> bool) -> 'b -> 'a split
val exists : 'a array -> ('a -> bool) -> bool
val is_empty : 'a array -> bool
val for_all2_no_exn : 'a array -> 'b array -> ('a -> 'b -> bool) -> bool
val for_alli : 'a array -> (int -> 'a -> bool) -> bool
val map : 'a array -> ('a -> 'b) -> 'b array
val iter : 'a array -> ('a -> unit) -> unit
val fold_left : 'b array -> 'a -> ('a -> 'b -> 'a) -> 'a
val get_or : 'a array -> int -> (unit -> 'a) -> 'a
end = struct
#1 "ext_array.ml"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
external ( .!() ) : 'a array -> int -> 'a = "%array_unsafe_get"
external ( .!()<- ) : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
let reverse_range a i len =
if len = 0 then ()
else
for k = 0 to (len - 1) / 2 do
let t = a.!(i + k) in
a.!(i + k) <- a.!(i + len - 1 - k);
a.!(i + len - 1 - k) <- t
done
let reverse_in_place a = reverse_range a 0 (Array.length a)
let reverse a =
let b_len = Array.length a in
if b_len = 0 then [||]
else
let b = Array.copy a in
for i = 0 to b_len - 1 do
Array.unsafe_set b i (Array.unsafe_get a (b_len - 1 - i))
done;
b
let reverse_of_list = function
| [] -> [||]
| hd :: tl ->
let len = List.length tl in
let a = Array.make (len + 1) hd in
let rec fill i = function
| [] -> a
| hd :: tl ->
Array.unsafe_set a i hd;
fill (i - 1) tl
in
fill (len - 1) tl
let filter a f =
let arr_len = Array.length a in
let rec aux acc i =
if i = arr_len then reverse_of_list acc
else
let v = Array.unsafe_get a i in
if f v then aux (v :: acc) (i + 1) else aux acc (i + 1)
in
aux [] 0
let filter_map a (f : _ -> _ option) =
let arr_len = Array.length a in
let rec aux acc i =
if i = arr_len then reverse_of_list acc
else
let v = Array.unsafe_get a i in
match f v with Some v -> aux (v :: acc) (i + 1) | None -> aux acc (i + 1)
in
aux [] 0
let range from to_ =
if from > to_ then invalid_arg "Ext_array.range"
else Array.init (to_ - from + 1) (fun i -> i + from)
let map2i f a b =
let len = Array.length a in
if len <> Array.length b then invalid_arg "Ext_array.map2i"
else Array.mapi (fun i a -> f i a (Array.unsafe_get b i)) a
let rec tolist_f_aux a f i res =
if i < 0 then res
else
let v = Array.unsafe_get a i in
tolist_f_aux a f (i - 1) (f v :: res)
let to_list_f a f = tolist_f_aux a f (Array.length a - 1) []
let rec tolist_aux a f i res =
if i < 0 then res
else
tolist_aux a f (i - 1)
(match f a.!(i) with Some v -> v :: res | None -> res)
let to_list_map a f = tolist_aux a f (Array.length a - 1) []
let to_list_map_acc a acc f = tolist_aux a f (Array.length a - 1) acc
let of_list_map a f =
match a with
| [] -> [||]
| [ a0 ] ->
let b0 = f a0 in
[| b0 |]
| [ a0; a1 ] ->
let b0 = f a0 in
let b1 = f a1 in
[| b0; b1 |]
| [ a0; a1; a2 ] ->
let b0 = f a0 in
let b1 = f a1 in
let b2 = f a2 in
[| b0; b1; b2 |]
| [ a0; a1; a2; a3 ] ->
let b0 = f a0 in
let b1 = f a1 in
let b2 = f a2 in
let b3 = f a3 in
[| b0; b1; b2; b3 |]
| [ a0; a1; a2; a3; a4 ] ->
let b0 = f a0 in
let b1 = f a1 in
let b2 = f a2 in
let b3 = f a3 in
let b4 = f a4 in
[| b0; b1; b2; b3; b4 |]
| a0 :: a1 :: a2 :: a3 :: a4 :: tl ->
let b0 = f a0 in
let b1 = f a1 in
let b2 = f a2 in
let b3 = f a3 in
let b4 = f a4 in
let len = List.length tl + 5 in
let arr = Array.make len b0 in
Array.unsafe_set arr 1 b1;
Array.unsafe_set arr 2 b2;
Array.unsafe_set arr 3 b3;
Array.unsafe_set arr 4 b4;
let rec fill i = function
| [] -> arr
| hd :: tl ->
Array.unsafe_set arr i (f hd);
fill (i + 1) tl
in
fill 5 tl
(**
{[
# rfind_with_index [|1;2;3|] (=) 2;;
- : int = 1
# rfind_with_index [|1;2;3|] (=) 1;;
- : int = 0
# rfind_with_index [|1;2;3|] (=) 3;;
- : int = 2
# rfind_with_index [|1;2;3|] (=) 4;;
- : int = -1
]}
*)
let rfind_with_index arr cmp v =
let len = Array.length arr in
let rec aux i =
if i < 0 then i
else if cmp (Array.unsafe_get arr i) v then i
else aux (i - 1)
in
aux (len - 1)
type 'a split = No_split | Split of 'a array * 'a array
let find_with_index arr cmp v =
let len = Array.length arr in
let rec aux i len =
if i >= len then -1
else if cmp (Array.unsafe_get arr i) v then i
else aux (i + 1) len
in
aux 0 len
let find_and_split arr cmp v : _ split =
let i = find_with_index arr cmp v in
if i < 0 then No_split
else
Split (Array.sub arr 0 i, Array.sub arr (i + 1) (Array.length arr - i - 1))
(** TODO: available since 4.03, use {!Array.exists} *)
let exists a p =
let n = Array.length a in
let rec loop i =
if i = n then false
else if p (Array.unsafe_get a i) then true
else loop (succ i)
in
loop 0
let is_empty arr = Array.length arr = 0
let rec unsafe_loop index len p xs ys =
if index >= len then true
else
p (Array.unsafe_get xs index) (Array.unsafe_get ys index)
&& unsafe_loop (succ index) len p xs ys
let for_alli a p =
let n = Array.length a in
let rec loop i =
if i = n then true
else if p i (Array.unsafe_get a i) then loop (succ i)
else false
in
loop 0
let for_all2_no_exn xs ys p =
let len_xs = Array.length xs in
let len_ys = Array.length ys in
len_xs = len_ys && unsafe_loop 0 len_xs p xs ys
let map a f =
let open Array in
let l = length a in
if l = 0 then [||]
else
let r = make l (f (unsafe_get a 0)) in
for i = 1 to l - 1 do
unsafe_set r i (f (unsafe_get a i))
done;
r
let iter a f =
let open Array in
for i = 0 to length a - 1 do
f (unsafe_get a i)
done
let fold_left a x f =
let open Array in
let r = ref x in
for i = 0 to length a - 1 do
r := f !r (unsafe_get a i)
done;
!r
let get_or arr i cb =
if i >= 0 && i < Array.length arr then Array.unsafe_get arr i else cb ()
end
module Ext_bytes : sig
#1 "ext_bytes.mli"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
= "caml_blit_string"
[@@noalloc]
end = struct
#1 "ext_bytes.ml"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
= "caml_blit_string"
[@@noalloc]
end
module Ext_buffer : sig
#1 "ext_buffer.mli"
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(** Extensible buffers.
This module implements buffers that automatically expand
as necessary. It provides accumulative concatenation of strings
in quasi-linear time (instead of quadratic time when strings are
concatenated pairwise).
*)
(* ReScript customization: customized for efficient digest *)
type t
(** The abstract type of buffers. *)
val create : int -> t
(** [create n] returns a fresh buffer, initially empty.
The [n] parameter is the initial size of the internal byte sequence
that holds the buffer contents. That byte sequence is automatically
reallocated when more than [n] characters are stored in the buffer,
but shrinks back to [n] characters when [reset] is called.
For best performance, [n] should be of the same order of magnitude
as the number of characters that are expected to be stored in
the buffer (for instance, 80 for a buffer that holds one output
line). Nothing bad will happen if the buffer grows beyond that
limit, however. In doubt, take [n = 16] for instance.
If [n] is not between 1 and {!Sys.max_string_length}, it will
be clipped to that interval. *)
val contents : t -> string
(** Return a copy of the current contents of the buffer.
The buffer itself is unchanged. *)
val length : t -> int
(** Return the number of characters currently contained in the buffer. *)
val is_empty : t -> bool
val clear : t -> unit
(** Empty the buffer. *)
val add_char : t -> char -> unit
[@@inline]
(** [add_char b c] appends the character [c] at the end of the buffer [b]. *)
val add_string : t -> string -> unit
(** [add_string b s] appends the string [s] at the end of the buffer [b]. *)
(* val add_bytes : t -> bytes -> unit *)
(** [add_string b s] appends the string [s] at the end of the buffer [b].
@since 4.02 *)
(* val add_substring : t -> string -> int -> int -> unit *)
(** [add_substring b s ofs len] takes [len] characters from offset
[ofs] in string [s] and appends them at the end of the buffer [b]. *)
(* val add_subbytes : t -> bytes -> int -> int -> unit *)
(** [add_substring b s ofs len] takes [len] characters from offset
[ofs] in byte sequence [s] and appends them at the end of the buffer [b].
@since 4.02 *)
(* val add_buffer : t -> t -> unit *)
(** [add_buffer b1 b2] appends the current contents of buffer [b2]
at the end of buffer [b1]. [b2] is not modified. *)
(* val add_channel : t -> in_channel -> int -> unit *)
(** [add_channel b ic n] reads exactly [n] character from the
input channel [ic] and stores them at the end of buffer [b].
Raise [End_of_file] if the channel contains fewer than [n]
characters. *)
val output_buffer : out_channel -> t -> unit
(** [output_buffer oc b] writes the current contents of buffer [b]
on the output channel [oc]. *)
val digest : t -> Digest.t
val not_equal : t -> string -> bool
val add_int_1 : t -> int -> unit
val add_int_2 : t -> int -> unit
val add_int_3 : t -> int -> unit
val add_int_4 : t -> int -> unit
val add_string_char : t -> string -> char -> unit
val add_ninja_prefix_var : t -> string -> unit
val add_char_string : t -> char -> string -> unit
end = struct
#1 "ext_buffer.ml"
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Extensible buffers *)
type t = {
mutable buffer : bytes;
mutable position : int;
mutable length : int;
}
let create n =
let n = if n < 1 then 1 else n in
let s = Bytes.create n in
{ buffer = s; position = 0; length = n }
let contents b = Bytes.sub_string b.buffer 0 b.position
(* let to_bytes b = Bytes.sub b.buffer 0 b.position *)
(* let sub b ofs len =
if ofs < 0 || len < 0 || ofs > b.position - len
then invalid_arg "Ext_buffer.sub"
else Bytes.sub_string b.buffer ofs len *)
(* let blit src srcoff dst dstoff len =
if len < 0 || srcoff < 0 || srcoff > src.position - len
|| dstoff < 0 || dstoff > (Bytes.length dst) - len
then invalid_arg "Ext_buffer.blit"
else
Bytes.unsafe_blit src.buffer srcoff dst dstoff len *)
let length b = b.position
let is_empty b = b.position = 0
let clear b = b.position <- 0
(* let reset b =
b.position <- 0; b.buffer <- b.initial_buffer;
b.length <- Bytes.length b.buffer *)
let resize b more =
let len = b.length in
let new_len = ref len in
while b.position + more > !new_len do
new_len := 2 * !new_len
done;
let new_buffer = Bytes.create !new_len in
(* PR#6148: let's keep using [blit] rather than [unsafe_blit] in
this tricky function that is slow anyway. *)
Bytes.blit b.buffer 0 new_buffer 0 b.position;
b.buffer <- new_buffer;
b.length <- !new_len;
assert (b.position + more <= b.length)
let[@inline] add_char b c =
let pos = b.position in
if pos >= b.length then resize b 1;
Bytes.unsafe_set b.buffer pos c;
b.position <- pos + 1
(* let add_substring b s offset len =
if offset < 0 || len < 0 || offset > String.length s - len
then invalid_arg "Ext_buffer.add_substring/add_subbytes";
let new_position = b.position + len in
if new_position > b.length then resize b len;
Ext_bytes.unsafe_blit_string s offset b.buffer b.position len;
b.position <- new_position *)
(* let add_subbytes b s offset len =
add_substring b (Bytes.unsafe_to_string s) offset len *)
let add_string b s =
let len = String.length s in
let new_position = b.position + len in
if new_position > b.length then resize b len;
Ext_bytes.unsafe_blit_string s 0 b.buffer b.position len;
b.position <- new_position
(* TODO: micro-optimzie *)
let add_string_char b s c =
let s_len = String.length s in
let len = s_len + 1 in
let new_position = b.position + len in
if new_position > b.length then resize b len;
let b_buffer = b.buffer in
Ext_bytes.unsafe_blit_string s 0 b_buffer b.position s_len;
Bytes.unsafe_set b_buffer (new_position - 1) c;
b.position <- new_position
let add_char_string b c s =
let s_len = String.length s in
let len = s_len + 1 in
let new_position = b.position + len in
if new_position > b.length then resize b len;
let b_buffer = b.buffer in
let b_position = b.position in
Bytes.unsafe_set b_buffer b_position c;
Ext_bytes.unsafe_blit_string s 0 b_buffer (b_position + 1) s_len;
b.position <- new_position
(* equivalent to add_char " "; add_char "$"; add_string s *)
let add_ninja_prefix_var b s =
let s_len = String.length s in
let len = s_len + 2 in
let new_position = b.position + len in
if new_position > b.length then resize b len;
let b_buffer = b.buffer in
let b_position = b.position in
Bytes.unsafe_set b_buffer b_position ' ';
Bytes.unsafe_set b_buffer (b_position + 1) '$';
Ext_bytes.unsafe_blit_string s 0 b_buffer (b_position + 2) s_len;
b.position <- new_position
(* let add_bytes b s = add_string b (Bytes.unsafe_to_string s)
let add_buffer b bs =
add_subbytes b bs.buffer 0 bs.position *)
(* let add_channel b ic len =
if len < 0
|| len > Sys.max_string_length
then (* PR#5004 *)
invalid_arg "Ext_buffer.add_channel";
if b.position + len > b.length then resize b len;
really_input ic b.buffer b.position len;
b.position <- b.position + len *)
let output_buffer oc b = output oc b.buffer 0 b.position
external unsafe_string : bytes -> int -> int -> Digest.t = "caml_md5_string"
let digest b = unsafe_string b.buffer 0 b.position
let rec not_equal_aux (b : bytes) (s : string) i len =
if i >= len then false
else
Bytes.unsafe_get b i <> String.unsafe_get s i
|| not_equal_aux b s (i + 1) len
(** avoid a large copy *)
let not_equal (b : t) (s : string) =
let b_len = b.position in
let s_len = String.length s in
b_len <> s_len || not_equal_aux b.buffer s 0 s_len
(**
It could be one byte, two bytes, three bytes and four bytes
TODO: inline for better performance
*)
let add_int_1 (b : t) (x : int) =
let c = Char.unsafe_chr (x land 0xff) in
let pos = b.position in
if pos >= b.length then resize b 1;
Bytes.unsafe_set b.buffer pos c;
b.position <- pos + 1
let add_int_2 (b : t) (x : int) =
let c1 = Char.unsafe_chr (x land 0xff) in
let c2 = Char.unsafe_chr ((x lsr 8) land 0xff) in
let pos = b.position in
if pos + 1 >= b.length then resize b 2;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
b.position <- pos + 2
let add_int_3 (b : t) (x : int) =
let c1 = Char.unsafe_chr (x land 0xff) in
let c2 = Char.unsafe_chr ((x lsr 8) land 0xff) in
let c3 = Char.unsafe_chr ((x lsr 16) land 0xff) in
let pos = b.position in
if pos + 2 >= b.length then resize b 3;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
Bytes.unsafe_set b_buffer (pos + 2) c3;
b.position <- pos + 3
let add_int_4 (b : t) (x : int) =
let c1 = Char.unsafe_chr (x land 0xff) in
let c2 = Char.unsafe_chr ((x lsr 8) land 0xff) in
let c3 = Char.unsafe_chr ((x lsr 16) land 0xff) in
let c4 = Char.unsafe_chr ((x lsr 24) land 0xff) in
let pos = b.position in
if pos + 3 >= b.length then resize b 4;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
Bytes.unsafe_set b_buffer (pos + 2) c3;
Bytes.unsafe_set b_buffer (pos + 3) c4;
b.position <- pos + 4
end
module Ext_spec : sig
#1 "ext_spec.mli"
type 'a t = (string * 'a * string) array
val assoc3 : 'a t -> string -> 'a option
end = struct
#1 "ext_spec.ml"
(* Copyright (C) 2020- Hongbo Zhang, Authors of ReScript
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(* A small module which is also used by {!Bsb_helper} *)
type 'a t = (string * 'a * string) array
let rec unsafe_loop i (l : 'a t) n x =
if i = n then None
else
let y1, y2, _ = Array.unsafe_get l i in
if y1 = x then Some y2 else unsafe_loop (i + 1) l n x
let assoc3 (l : 'a t) (x : string) : 'a option =
let n = Array.length l in
unsafe_loop 0 l n x
end
module Ext_string : sig
#1 "ext_string.pp.mli"
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(** Extension to the standard library [String] module, fixed some bugs like
avoiding locale sensitivity *)
(** default is false *)
val split_by : ?keep_empty:bool -> (char -> bool) -> string -> string list
(** remove whitespace letters ('\t', '\n', ' ') on both side*)
val trim : string -> string
(** default is false *)
val split : ?keep_empty:bool -> string -> char -> string list
(** split by space chars for quick scripting *)
val quick_split_by_ws : string -> string list
val starts_with : string -> string -> bool
(**
return [-1] when not found, the returned index is useful
see [ends_with_then_chop]
*)
val ends_with_index : string -> string -> int
val ends_with : string -> string -> bool
(**
[ends_with_then_chop name ext]
@example:
{[
ends_with_then_chop "a.cmj" ".cmj"
"a"
]}
This is useful in controlled or file case sensitve system
*)
val ends_with_then_chop : string -> string -> string option
(**
[for_all_from s start p]
if [start] is negative, it raises,
if [start] is too large, it returns true
*)
val for_all_from:
string ->
int ->
(char -> bool) ->
bool
val for_all :
string ->
(char -> bool) ->
bool
val is_empty : string -> bool
val repeat : int -> string -> string
val equal : string -> string -> bool
(**
[extract_until s cursor sep]
When [sep] not found, the cursor is updated to -1,
otherwise cursor is increased to 1 + [sep_position]
User can not determine whether it is found or not by
telling the return string is empty since
"\n\n" would result in an empty string too.
*)
(* val extract_until:
string ->
int ref -> (* cursor to be updated *)
char ->
string *)
val index_count:
string ->
int ->
char ->
int ->
int
(* val index_next :
string ->
int ->
char ->
int *)
(**
[find ~start ~sub s]
returns [-1] if not found
*)
val find : ?start:int -> sub:string -> string -> int
val contain_substring : string -> string -> bool
val non_overlap_count : sub:string -> string -> int
val rfind : sub:string -> string -> int
(** [tail_from s 1]
return a substring from offset 1 (inclusive)
*)
val tail_from : string -> int -> string
(** returns negative number if not found *)
val rindex_neg : string -> char -> int
val rindex_opt : string -> char -> int option
val no_char : string -> char -> int -> int -> bool
val no_slash : string -> bool
(** return negative means no slash, otherwise [i] means the place for first slash *)
val no_slash_idx : string -> int
val no_slash_idx_from : string -> int -> int
(** if no conversion happens, reference equality holds *)
val replace_slash_backward : string -> string
(** if no conversion happens, reference equality holds *)
val replace_backward_slash : string -> string
val empty : string
external compare : string -> string -> int = "caml_string_length_based_compare" [@@noalloc];;
val single_space : string
val concat3 : string -> string -> string -> string
val concat4 : string -> string -> string -> string -> string
val concat5 : string -> string -> string -> string -> string -> string
val inter2 : string -> string -> string
val inter3 : string -> string -> string -> string
val inter4 : string -> string -> string -> string -> string
val concat_array : string -> string array -> string
val single_colon : string
val parent_dir_lit : string
val current_dir_lit : string
val capitalize_ascii : string -> string
val capitalize_sub:
string ->
int ->
string
val uncapitalize_ascii : string -> string
val lowercase_ascii : string -> string
(** Play parity to {!Ext_buffer.add_int_1} *)
(* val get_int_1 : string -> int -> int
val get_int_2 : string -> int -> int
val get_int_3 : string -> int -> int
val get_int_4 : string -> int -> int *)
val get_1_2_3_4 :
string ->
off:int ->
int ->
int
val unsafe_sub :
string ->
int ->
int ->
string
val is_valid_hash_number:
string ->
bool
val hash_number_as_i32_exn:
string ->
int32
val first_marshal_char:
string ->
bool
end = struct
#1 "ext_string.pp.ml"
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
* This program 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 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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.