-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathmlBytes.js
949 lines (853 loc) · 26.5 KB
/
mlBytes.js
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
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2010-2014 Jérôme Vouillon
// Laboratoire PPS - CNRS Université Paris Diderot
//
// 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, with linking exception;
// either version 2.1 of the License, or (at your option) any 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.
// An OCaml string is an object with three fields:
// - tag 't'
// - length 'l'
// - contents 'c'
//
// The contents of the string can be either a JavaScript array or
// a JavaScript string. The length of this string can be less than the
// length of the OCaml string. In this case, remaining bytes are
// assumed to be zeroes. Arrays are mutable but consumes more memory
// than strings. A common pattern is to start from an empty string and
// progressively fill it from the start. Partial strings makes it
// possible to implement this efficiently.
//
// When converting to and from UTF-16, we keep track of whether the
// string is composed only of ASCII characters (in which case, no
// conversion needs to be performed) or not.
//
// The string tag can thus take the following values:
// full string BYTE | UNKNOWN: 0
// BYTE | ASCII: 9
// BYTE | NOT_ASCII: 8
// string prefix PARTIAL: 2
// array ARRAY: 4
//
// One can use bit masking to discriminate these different cases:
// known_encoding(x) = x&8
// is_ascii(x) = x&1
// kind(x) = x&6
//Provides: caml_str_repeat
function caml_str_repeat(n, s) {
return s.repeat(n);
}
//Provides: caml_subarray_to_jsbytes
//Weakdef
// Pre ECMAScript 5, [apply] would not support array-like object.
// In such setup, Typed_array would be implemented as polyfill, and [f.apply] would
// fail here. Mark the primitive as Weakdef, so that people can override it easily.
function caml_subarray_to_jsbytes(a, i, len) {
var f = String.fromCharCode;
if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a);
var s = "";
for (; 0 < len; i += 1024, len -= 1024)
s += f.apply(null, a.slice(i, i + Math.min(len, 1024)));
return s;
}
//Provides: caml_sub_uint8_array_to_jsbytes
//Weakdef
// Pre ECMAScript 5, [apply] would not support array-like object.
// In such setup, Typed_array would be implemented as polyfill, and [f.apply] would
// fail here. Mark the primitive as Weakdef, so that people can override it easily.
function caml_sub_uint8_array_to_jsbytes(a, i, len) {
var f = String.fromCharCode;
if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a);
var s = "";
for (; 0 < len; i += 1024, len -= 1024)
s += f.apply(null, a.subarray(i, i + Math.min(len, 1024)));
return s;
}
//Provides: caml_utf8_of_utf16
function caml_utf8_of_utf16(s) {
for (var b = "", t = b, c, d, i = 0, l = s.length; i < l; i++) {
c = s.charCodeAt(i);
if (c < 0x80) {
for (var j = i + 1; j < l && (c = s.charCodeAt(j)) < 0x80; j++);
if (j - i > 512) {
t.slice(0, 1);
b += t;
t = "";
b += s.slice(i, j);
} else t += s.slice(i, j);
if (j === l) break;
i = j;
}
if (c < 0x800) {
t += String.fromCharCode(0xc0 | (c >> 6));
t += String.fromCharCode(0x80 | (c & 0x3f));
} else if (c < 0xd800 || c >= 0xdfff) {
t += String.fromCharCode(
0xe0 | (c >> 12),
0x80 | ((c >> 6) & 0x3f),
0x80 | (c & 0x3f),
);
} else if (
c >= 0xdbff ||
i + 1 === l ||
(d = s.charCodeAt(i + 1)) < 0xdc00 ||
d > 0xdfff
) {
// Unmatched surrogate pair, replaced by \ufffd (replacement character)
t += "\xef\xbf\xbd";
} else {
i++;
c = (c << 10) + d - 0x35fdc00;
t += String.fromCharCode(
0xf0 | (c >> 18),
0x80 | ((c >> 12) & 0x3f),
0x80 | ((c >> 6) & 0x3f),
0x80 | (c & 0x3f),
);
}
if (t.length > 1024) {
t.slice(0, 1);
b += t;
t = "";
}
}
return b + t;
}
//Provides: caml_utf16_of_utf8
function caml_utf16_of_utf8(s) {
for (var b = "", t = "", c, c1, c2, v, i = 0, l = s.length; i < l; i++) {
c1 = s.charCodeAt(i);
if (c1 < 0x80) {
for (var j = i + 1; j < l && (c1 = s.charCodeAt(j)) < 0x80; j++);
if (j - i > 512) {
t.slice(0, 1);
b += t;
t = "";
b += s.slice(i, j);
} else t += s.slice(i, j);
if (j === l) break;
i = j;
}
v = 1;
if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) {
c = c2 + (c1 << 6);
if (c1 < 0xe0) {
v = c - 0x3080;
if (v < 0x80) v = 1;
} else {
v = 2;
if (++i < l && ((c2 = s.charCodeAt(i)) & -64) === 128) {
c = c2 + (c << 6);
if (c1 < 0xf0) {
v = c - 0xe2080;
if (v < 0x800 || (v >= 0xd7ff && v < 0xe000)) v = 2;
} else {
v = 3;
if (
++i < l &&
((c2 = s.charCodeAt(i)) & -64) === 128 &&
c1 < 0xf5
) {
v = c2 - 0x3c82080 + (c << 6);
if (v < 0x10000 || v > 0x10ffff) v = 3;
}
}
}
}
}
if (v < 4) {
// Invalid sequence
i -= v;
t += "\ufffd";
} else if (v > 0xffff)
t += String.fromCharCode(0xd7c0 + (v >> 10), 0xdc00 + (v & 0x3ff));
else t += String.fromCharCode(v);
if (t.length > 1024) {
t.slice(0, 1);
b += t;
t = "";
}
}
return b + t;
}
//Provides: jsoo_is_ascii
function jsoo_is_ascii(s) {
// The regular expression gets better at around this point for all browsers
if (s.length < 24) {
// Spidermonkey gets much slower when s.length >= 24 (on 64 bit archs)
for (var i = 0; i < s.length; i++) if (s.charCodeAt(i) > 127) return false;
return true;
}
// biome-ignore lint/suspicious/noControlCharactersInRegex: expected
else return !/[^\x00-\x7f]/.test(s);
}
//Provides: caml_bytes_unsafe_get mutable
function caml_bytes_unsafe_get(s, i) {
switch (s.t & 6) {
case 0 /* BYTES */:
return s.c.charCodeAt(i);
case 2 /* PARTIAL */:
if (i >= s.c.length) return 0;
return s.c.charCodeAt(i);
case 4 /* ARRAY */:
return s.c[i];
}
}
//Provides: caml_bytes_unsafe_set
//Requires: caml_convert_bytes_to_array
function caml_bytes_unsafe_set(s, i, c) {
// The OCaml compiler uses Char.unsafe_chr on integers larger than 255!
c &= 0xff;
if (s.t !== 4 /* ARRAY */) {
if (i === s.c.length) {
s.c += String.fromCharCode(c);
if (i + 1 === s.l) s.t = 0; /*BYTES | UNKOWN*/
return 0;
}
caml_convert_bytes_to_array(s);
}
s.c[i] = c;
return 0;
}
//Provides: caml_string_bound_error
//Requires: caml_invalid_argument
function caml_string_bound_error() {
caml_invalid_argument("index out of bounds");
}
//Provides: caml_bytes_bound_error
//Requires: caml_invalid_argument
function caml_bytes_bound_error() {
caml_invalid_argument("index out of bounds");
}
//Provides: caml_string_get
//Requires: caml_string_bound_error, caml_string_unsafe_get
//Requires: caml_ml_string_length
function caml_string_get(s, i) {
if (i >>> 0 >= caml_ml_string_length(s)) caml_string_bound_error();
return caml_string_unsafe_get(s, i);
}
//Provides: caml_string_get16
//Requires: caml_string_unsafe_get, caml_string_bound_error
//Requires: caml_ml_string_length
function caml_string_get16(s, i) {
if (i >>> 0 >= caml_ml_string_length(s) - 1) caml_string_bound_error();
var b1 = caml_string_unsafe_get(s, i),
b2 = caml_string_unsafe_get(s, i + 1);
return (b2 << 8) | b1;
}
//Provides: caml_bytes_get16
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
function caml_bytes_get16(s, i) {
if (i >>> 0 >= s.l - 1) caml_bytes_bound_error();
var b1 = caml_bytes_unsafe_get(s, i),
b2 = caml_bytes_unsafe_get(s, i + 1);
return (b2 << 8) | b1;
}
//Provides: caml_string_get32
//Requires: caml_string_unsafe_get, caml_string_bound_error
//Requires: caml_ml_string_length
function caml_string_get32(s, i) {
if (i >>> 0 >= caml_ml_string_length(s) - 3) caml_string_bound_error();
var b1 = caml_string_unsafe_get(s, i),
b2 = caml_string_unsafe_get(s, i + 1),
b3 = caml_string_unsafe_get(s, i + 2),
b4 = caml_string_unsafe_get(s, i + 3);
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
}
//Provides: caml_bytes_get32
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
function caml_bytes_get32(s, i) {
if (i >>> 0 >= s.l - 3) caml_bytes_bound_error();
var b1 = caml_bytes_unsafe_get(s, i),
b2 = caml_bytes_unsafe_get(s, i + 1),
b3 = caml_bytes_unsafe_get(s, i + 2),
b4 = caml_bytes_unsafe_get(s, i + 3);
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
}
//Provides: caml_string_get64
//Requires: caml_string_unsafe_get, caml_string_bound_error
//Requires: caml_int64_of_bytes
//Requires: caml_ml_string_length
function caml_string_get64(s, i) {
if (i >>> 0 >= caml_ml_string_length(s) - 7) caml_string_bound_error();
var a = new Array(8);
for (var j = 0; j < 8; j++) {
a[7 - j] = caml_string_unsafe_get(s, i + j);
}
return caml_int64_of_bytes(a);
}
//Provides: caml_bytes_get64
//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error
//Requires: caml_int64_of_bytes
function caml_bytes_get64(s, i) {
if (i >>> 0 >= s.l - 7) caml_bytes_bound_error();
var a = new Array(8);
for (var j = 0; j < 8; j++) {
a[7 - j] = caml_bytes_unsafe_get(s, i + j);
}
return caml_int64_of_bytes(a);
}
//Provides: caml_bytes_get
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_get
function caml_bytes_get(s, i) {
if (i >>> 0 >= s.l) caml_bytes_bound_error();
return caml_bytes_unsafe_get(s, i);
}
//Provides: caml_string_set
//Requires: caml_failwith
//If: js-string
function caml_string_set(s, i, c) {
caml_failwith("caml_string_set");
}
//Provides: caml_string_set
//Requires: caml_string_unsafe_set, caml_string_bound_error
//If: !js-string
function caml_string_set(s, i, c) {
if (i >>> 0 >= s.l) caml_string_bound_error();
return caml_string_unsafe_set(s, i, c);
}
//Provides: caml_bytes_set16
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
function caml_bytes_set16(s, i, i16) {
if (i >>> 0 >= s.l - 1) caml_bytes_bound_error();
var b2 = 0xff & (i16 >> 8),
b1 = 0xff & i16;
caml_bytes_unsafe_set(s, i + 0, b1);
caml_bytes_unsafe_set(s, i + 1, b2);
return 0;
}
//Provides: caml_bytes_set32
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
function caml_bytes_set32(s, i, i32) {
if (i >>> 0 >= s.l - 3) caml_bytes_bound_error();
var b4 = 0xff & (i32 >> 24),
b3 = 0xff & (i32 >> 16),
b2 = 0xff & (i32 >> 8),
b1 = 0xff & i32;
caml_bytes_unsafe_set(s, i + 0, b1);
caml_bytes_unsafe_set(s, i + 1, b2);
caml_bytes_unsafe_set(s, i + 2, b3);
caml_bytes_unsafe_set(s, i + 3, b4);
return 0;
}
//Provides: caml_bytes_set64
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
//Requires: caml_int64_to_bytes
function caml_bytes_set64(s, i, i64) {
if (i >>> 0 >= s.l - 7) caml_bytes_bound_error();
var a = caml_int64_to_bytes(i64);
for (var j = 0; j < 8; j++) {
caml_bytes_unsafe_set(s, i + 7 - j, a[j]);
}
return 0;
}
//Provides: caml_bytes_set
//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set
function caml_bytes_set(s, i, c) {
if (i >>> 0 >= s.l) caml_bytes_bound_error();
return caml_bytes_unsafe_set(s, i, c);
}
//Provides: caml_bytes_of_utf16_jsstring
//Requires: jsoo_is_ascii, caml_utf8_of_utf16, MlBytes
function caml_bytes_of_utf16_jsstring(s) {
var tag = 9 /* BYTES | ASCII */;
if (!jsoo_is_ascii(s))
(tag = 8) /* BYTES | NOT_ASCII */, (s = caml_utf8_of_utf16(s));
return new MlBytes(tag, s, s.length);
}
//Provides: MlBytes
//Requires: caml_convert_string_to_bytes, jsoo_is_ascii, caml_utf16_of_utf8
class MlBytes {
constructor(tag, contents, length) {
this.t = tag;
this.c = contents;
this.l = length;
}
toString() {
switch (this.t) {
case 9: /*BYTES | ASCII*/
case 8 /*BYTES | NOT_ASCII*/:
return this.c;
case 4: /* ARRAY */
case 2 /* PARTIAL */:
// biome-ignore lint/suspicious/noFallthroughSwitchClause:
caml_convert_string_to_bytes(this);
// fallthrough
case 0 /*BYTES | UNKOWN*/:
if (jsoo_is_ascii(this.c)) this.t = 9; /*BYTES | ASCII*/
else this.t = 8; /*BYTES | NOT_ASCII*/
return this.c;
}
}
toUtf16() {
var r = this.toString();
if (this.t === 9) return r;
return caml_utf16_of_utf8(r);
}
slice() {
var content = this.t === 4 ? this.c.slice() : this.c;
return new MlBytes(this.t, content, this.l);
}
}
//Provides: caml_convert_string_to_bytes
//Requires: caml_str_repeat, caml_sub_uint8_array_to_jsbytes
function caml_convert_string_to_bytes(s) {
/* Assumes not BYTES */
if (s.t === 2 /* PARTIAL */) s.c += caml_str_repeat(s.l - s.c.length, "\0");
else s.c = caml_sub_uint8_array_to_jsbytes(s.c, 0, s.c.length);
s.t = 0; /*BYTES | UNKOWN*/
}
//Provides: caml_convert_bytes_to_array
function caml_convert_bytes_to_array(s) {
/* Assumes not ARRAY */
var a = new Uint8Array(s.l);
var b = s.c,
l = b.length,
i = 0;
for (; i < l; i++) a[i] = b.charCodeAt(i);
for (l = s.l; i < l; i++) a[i] = 0;
s.c = a;
s.t = 4; /* ARRAY */
return a;
}
//Provides: caml_uint8_array_of_bytes mutable
//Requires: caml_convert_bytes_to_array
function caml_uint8_array_of_bytes(s) {
if (s.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s);
return s.c;
}
//Provides: caml_uint8_array_of_string mutable
//Requires: caml_ml_string_length, caml_string_unsafe_get
function caml_uint8_array_of_string(s) {
var l = caml_ml_string_length(s);
var a = new Uint8Array(l);
var i = 0;
for (; i < l; i++) a[i] = caml_string_unsafe_get(s, i);
return a;
}
//Provides: caml_create_string const
//Requires: MlBytes, caml_invalid_argument
//If: !js-string
function caml_create_string(len) {
if (len < 0) caml_invalid_argument("String.create");
return new MlBytes(len ? 2 : 9, "", len);
}
//Provides: caml_create_string const
//Requires: caml_invalid_argument
//If: js-string
function caml_create_string(len) {
caml_invalid_argument("String.create");
}
//Provides: caml_create_bytes const
//Requires: MlBytes,caml_invalid_argument
function caml_create_bytes(len) {
if (len < 0) caml_invalid_argument("Bytes.create");
return new MlBytes(len ? 2 : 9, "", len);
}
//Provides: caml_string_of_array
//Requires: caml_subarray_to_jsbytes, caml_string_of_jsbytes
function caml_string_of_array(a) {
return caml_string_of_jsbytes(caml_subarray_to_jsbytes(a, 0, a.length));
}
//Provides: caml_string_of_uint8_array
//Requires: caml_sub_uint8_array_to_jsbytes
//If: js-string
function caml_string_of_uint8_array(a) {
return caml_sub_uint8_array_to_jsbytes(a, 0, a.length);
}
//Provides: caml_string_of_uint8_array
//Requires: caml_bytes_of_uint8_array
//If: !js-string
function caml_string_of_uint8_array(a) {
return caml_bytes_of_uint8_array(a.slice());
}
//Provides: caml_bytes_of_array
//Requires: MlBytes
function caml_bytes_of_array(a) {
if (!(a instanceof Uint8Array)) {
a = new Uint8Array(a);
}
return new MlBytes(4, a, a.length);
}
//Provides: caml_bytes_of_uint8_array
//Requires: MlBytes
function caml_bytes_of_uint8_array(a) {
return new MlBytes(4, a, a.length);
}
//Provides: caml_bytes_compare mutable
//Requires: caml_convert_string_to_bytes
function caml_bytes_compare(s1, s2) {
s1.t & 6 && caml_convert_string_to_bytes(s1);
s2.t & 6 && caml_convert_string_to_bytes(s2);
return s1.c < s2.c ? -1 : s1.c > s2.c ? 1 : 0;
}
//Provides: caml_bytes_equal mutable (const, const)
//Requires: caml_convert_string_to_bytes
function caml_bytes_equal(s1, s2) {
if (s1 === s2) return 1;
s1.t & 6 && caml_convert_string_to_bytes(s1);
s2.t & 6 && caml_convert_string_to_bytes(s2);
return s1.c === s2.c ? 1 : 0;
}
//Provides: caml_string_notequal mutable (const, const)
//Requires: caml_string_equal
function caml_string_notequal(s1, s2) {
return 1 - caml_string_equal(s1, s2);
}
//Provides: caml_bytes_notequal mutable (const, const)
//Requires: caml_bytes_equal
function caml_bytes_notequal(s1, s2) {
return 1 - caml_bytes_equal(s1, s2);
}
//Provides: caml_bytes_lessequal mutable
//Requires: caml_convert_string_to_bytes
function caml_bytes_lessequal(s1, s2) {
s1.t & 6 && caml_convert_string_to_bytes(s1);
s2.t & 6 && caml_convert_string_to_bytes(s2);
return s1.c <= s2.c ? 1 : 0;
}
//Provides: caml_bytes_lessthan mutable
//Requires: caml_convert_string_to_bytes
function caml_bytes_lessthan(s1, s2) {
s1.t & 6 && caml_convert_string_to_bytes(s1);
s2.t & 6 && caml_convert_string_to_bytes(s2);
return s1.c < s2.c ? 1 : 0;
}
//Provides: caml_string_greaterequal
//Requires: caml_string_lessequal
function caml_string_greaterequal(s1, s2) {
return caml_string_lessequal(s2, s1);
}
//Provides: caml_bytes_greaterequal
//Requires: caml_bytes_lessequal
function caml_bytes_greaterequal(s1, s2) {
return caml_bytes_lessequal(s2, s1);
}
//Provides: caml_string_greaterthan
//Requires: caml_string_lessthan
function caml_string_greaterthan(s1, s2) {
return caml_string_lessthan(s2, s1);
}
//Provides: caml_bytes_greaterthan
//Requires: caml_bytes_lessthan
function caml_bytes_greaterthan(s1, s2) {
return caml_bytes_lessthan(s2, s1);
}
//Provides: caml_fill_bytes
//Requires: caml_str_repeat, caml_convert_bytes_to_array
//Alias: caml_fill_string
function caml_fill_bytes(s, i, l, c) {
if (l > 0) {
if (i === 0 && (l >= s.l || (s.t === 2 /* PARTIAL */ && l >= s.c.length))) {
if (c === 0) {
s.c = "";
s.t = 2; /* PARTIAL */
} else {
s.c = caml_str_repeat(l, String.fromCharCode(c));
s.t = l === s.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */
}
} else {
if (s.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s);
for (l += i; i < l; i++) s.c[i] = c;
}
}
return 0;
}
//Provides: caml_blit_bytes
//Requires: caml_sub_uint8_array_to_jsbytes, caml_convert_bytes_to_array
function caml_blit_bytes(s1, i1, s2, i2, len) {
if (len === 0) return 0;
if (
i2 === 0 &&
(len >= s2.l || (s2.t === 2 /* PARTIAL */ && len >= s2.c.length))
) {
s2.c =
s1.t === 4 /* ARRAY */
? caml_sub_uint8_array_to_jsbytes(s1.c, i1, len)
: i1 === 0 && s1.c.length === len
? s1.c
: s1.c.slice(i1, i1 + len);
s2.t = s2.c.length === s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */
} else if (s2.t === 2 /* PARTIAL */ && i2 === s2.c.length) {
s2.c +=
s1.t === 4 /* ARRAY */
? caml_sub_uint8_array_to_jsbytes(s1.c, i1, len)
: i1 === 0 && s1.c.length === len
? s1.c
: s1.c.slice(i1, i1 + len);
s2.t = s2.c.length === s2.l ? 0 /* BYTES | UNKOWN */ : 2; /* PARTIAL */
} else {
if (s2.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s2);
var c1 = s1.c,
c2 = s2.c;
if (s1.t === 4 /* ARRAY */) {
if (i2 <= i1) {
for (var i = 0; i < len; i++) c2[i2 + i] = c1[i1 + i];
} else {
for (var i = len - 1; i >= 0; i--) c2[i2 + i] = c1[i1 + i];
}
} else {
var l = Math.min(len, c1.length - i1);
for (var i = 0; i < l; i++) c2[i2 + i] = c1.charCodeAt(i1 + i);
for (; i < len; i++) c2[i2 + i] = 0;
}
}
return 0;
}
//Provides: caml_blit_string
//Requires: caml_blit_bytes, caml_bytes_of_string
function caml_blit_string(a, b, c, d, e) {
caml_blit_bytes(caml_bytes_of_string(a), b, c, d, e);
return 0;
}
//Provides: caml_ml_bytes_length const
function caml_ml_bytes_length(s) {
return s.l;
}
//Provides: caml_string_concat const
//If: js-string
function caml_string_concat(a, b) {
return a + b;
}
//Provides: caml_string_concat const
//Requires: caml_convert_string_to_bytes, MlBytes
//If: !js-string
function caml_string_concat(s1, s2) {
s1.t & 6 && caml_convert_string_to_bytes(s1);
s2.t & 6 && caml_convert_string_to_bytes(s2);
return new MlBytes(0, s1.c + s2.c, s1.l + s2.l);
}
//Provides: caml_string_unsafe_get const
//If: js-string
function caml_string_unsafe_get(s, i) {
return s.charCodeAt(i);
}
//Provides: caml_ml_string_length const
//If: js-string
function caml_ml_string_length(s) {
return s.length;
}
//Provides: caml_string_compare const
//If: js-string
function caml_string_compare(s1, s2) {
return s1 < s2 ? -1 : s1 > s2 ? 1 : 0;
}
//Provides: caml_string_equal const
//If: js-string
function caml_string_equal(s1, s2) {
if (s1 === s2) return 1;
return 0;
}
//Provides: caml_string_lessequal const
//If: js-string
function caml_string_lessequal(s1, s2) {
return s1 <= s2 ? 1 : 0;
}
//Provides: caml_string_lessthan const
//If: js-string
function caml_string_lessthan(s1, s2) {
return s1 < s2 ? 1 : 0;
}
//Provides: caml_string_of_bytes
//Requires: caml_convert_string_to_bytes, caml_string_of_jsbytes
//If: js-string
function caml_string_of_bytes(s) {
s.t & 6 && caml_convert_string_to_bytes(s);
return caml_string_of_jsbytes(s.c);
}
//Provides: caml_bytes_of_string const
//Requires: caml_bytes_of_jsbytes, caml_jsbytes_of_string
//If: js-string
function caml_bytes_of_string(s) {
return caml_bytes_of_jsbytes(caml_jsbytes_of_string(s));
}
//Provides: caml_string_of_jsbytes const
//If: js-string
function caml_string_of_jsbytes(x) {
return x;
}
//Provides: caml_jsbytes_of_string const
//If: js-string
function caml_jsbytes_of_string(x) {
return x;
}
//Provides: caml_jsstring_of_string const
//Requires: jsoo_is_ascii, caml_utf16_of_utf8
//If: js-string
function caml_jsstring_of_string(s) {
if (jsoo_is_ascii(s)) return s;
return caml_utf16_of_utf8(s);
}
//Provides: caml_string_of_jsstring const
//Requires: jsoo_is_ascii, caml_utf8_of_utf16, caml_string_of_jsbytes
//If: js-string
function caml_string_of_jsstring(s) {
if (jsoo_is_ascii(s)) return caml_string_of_jsbytes(s);
else return caml_string_of_jsbytes(caml_utf8_of_utf16(s));
}
//Provides: caml_bytes_of_jsbytes const
//Requires: MlBytes
function caml_bytes_of_jsbytes(s) {
return new MlBytes(0, s, s.length);
}
// The section below should be used when use-js-string=false
//Provides: caml_string_unsafe_get const
//Requires: caml_bytes_unsafe_get
//If: !js-string
function caml_string_unsafe_get(s, i) {
return caml_bytes_unsafe_get(s, i);
}
//Provides: caml_string_unsafe_set
//Requires: caml_bytes_unsafe_set
//If: !js-string
function caml_string_unsafe_set(s, i, c) {
return caml_bytes_unsafe_set(s, i, c);
}
//Provides: caml_ml_string_length const
//Requires: caml_ml_bytes_length
//If: !js-string
function caml_ml_string_length(s) {
return caml_ml_bytes_length(s);
}
//Provides: caml_string_compare
//Requires: caml_bytes_compare
//If: !js-string
function caml_string_compare(s1, s2) {
return caml_bytes_compare(s1, s2);
}
//Provides: caml_string_equal
//Requires: caml_bytes_equal
//If: !js-string
function caml_string_equal(s1, s2) {
return caml_bytes_equal(s1, s2);
}
//Provides: caml_string_lessequal
//Requires: caml_bytes_lessequal
//If: !js-string
function caml_string_lessequal(s1, s2) {
return caml_bytes_lessequal(s1, s2);
}
//Provides: caml_string_lessthan
//Requires: caml_bytes_lessthan
//If: !js-string
function caml_string_lessthan(s1, s2) {
return caml_bytes_lessthan(s1, s2);
}
//Provides: caml_string_of_bytes
//If: !js-string
function caml_string_of_bytes(s) {
return s;
}
//Provides: caml_bytes_of_string const
//If: !js-string
function caml_bytes_of_string(s) {
return s;
}
//Provides: caml_string_of_jsbytes const
//Requires: caml_bytes_of_jsbytes
//If: !js-string
function caml_string_of_jsbytes(s) {
return caml_bytes_of_jsbytes(s);
}
//Provides: caml_jsbytes_of_string const
//Requires: caml_convert_string_to_bytes
//If: !js-string
function caml_jsbytes_of_string(s) {
s.t & 6 && caml_convert_string_to_bytes(s);
return s.c;
}
//Provides: caml_jsstring_of_string mutable (const)
//If: !js-string
function caml_jsstring_of_string(s) {
return s.toUtf16();
}
//Provides: caml_string_of_jsstring
//Requires: caml_bytes_of_utf16_jsstring
//If: !js-string
function caml_string_of_jsstring(s) {
return caml_bytes_of_utf16_jsstring(s);
}
//Provides: caml_is_ml_bytes
//Requires: MlBytes
function caml_is_ml_bytes(s) {
return s instanceof MlBytes;
}
//Provides: caml_ml_bytes_content
//Requires: MlBytes, caml_convert_string_to_bytes
//Returns a (full) string of bytes or an array
function caml_ml_bytes_content(s) {
switch (s.t & 6) {
case 2 /* PARTIAL */:
caml_convert_string_to_bytes(s);
return s.c;
default: /* BYTES or ARRAY */
return s.c;
}
}
//Provides: caml_is_ml_string
//Requires: jsoo_is_ascii
//If: js-string
function caml_is_ml_string(s) {
// biome-ignore lint/suspicious/noControlCharactersInRegex: expected
return typeof s === "string" && !/[^\x00-\xff]/.test(s);
}
//Provides: caml_is_ml_string
//Requires: caml_is_ml_bytes
//If: !js-string
function caml_is_ml_string(s) {
return caml_is_ml_bytes(s);
}
// The functions below are deprecated
//Provides: caml_js_to_byte_string const
//Requires: caml_string_of_jsbytes
//Deprecated: Use [caml_string_of_jsbytes] instead
function caml_js_to_byte_string(s) {
return caml_string_of_jsbytes(s);
}
//Provides: caml_js_from_string mutable (const)
//Requires: caml_jsstring_of_string
//Deprecated: Use [caml_jsstring_of_string] instead
function caml_js_from_string(s) {
return caml_jsstring_of_string(s);
}
//Provides: caml_to_js_string mutable (const)
//Requires: caml_jsstring_of_string
//Deprecated: Use [caml_jsstring_of_string] instead
function caml_to_js_string(s) {
return caml_jsstring_of_string(s);
}
//Provides: caml_js_to_string const
//Requires: caml_string_of_jsstring
//Deprecated: Use [caml_string_of_jsstring] instead
function caml_js_to_string(s) {
return caml_string_of_jsstring(s);
}
//Provides: caml_array_of_string
//Requires: caml_uint8_array_of_string
//Deprecated: Use [caml_uint8_array_of_string] instead
function caml_array_of_string(x) {
return caml_uint8_array_of_string(x);
}
//Provides: caml_array_of_bytes
//Requires: caml_uint8_array_of_bytes
//Deprecated: Use [caml_uint8_array_of_bytes] instead
function caml_array_of_bytes(x) {
return caml_uint8_array_of_bytes(x);
}
//Provides: caml_new_string
//Requires: caml_string_of_jsbytes
//Deprecated: Use [caml_string_of_jsbytes] instead
function caml_new_string(s) {
return caml_string_of_jsbytes(s);
}