forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_string.ml
531 lines (425 loc) · 14.1 KB
/
ext_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
(* 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. *)
(*
{[ split " test_unsafe_obj_ffi_ppx.cmi" ~keep_empty:false ' ']}
*)
let split_by ?(keep_empty=false) is_delim str =
let len = String.length str in
let rec loop acc last_pos pos =
if pos = -1 then
if last_pos = 0 && not keep_empty then
acc
else
String.sub str 0 last_pos :: acc
else
if is_delim str.[pos] then
let new_len = (last_pos - pos - 1) in
if new_len <> 0 || keep_empty then
let v = String.sub str (pos + 1) new_len in
loop ( v :: acc)
pos (pos - 1)
else loop acc pos (pos - 1)
else loop acc last_pos (pos - 1)
in
loop [] len (len - 1)
let trim s =
let i = ref 0 in
let j = String.length s in
while !i < j &&
let u = String.unsafe_get s !i in
u = '\t' || u = '\n' || u = ' '
do
incr i;
done;
let k = ref (j - 1) in
while !k >= !i &&
let u = String.unsafe_get s !k in
u = '\t' || u = '\n' || u = ' ' do
decr k ;
done;
String.sub s !i (!k - !i + 1)
let split ?keep_empty str on =
if str = "" then [] else
split_by ?keep_empty (fun x -> (x : char) = on) str ;;
let quick_split_by_ws str : string list =
split_by ~keep_empty:false (fun x -> x = '\t' || x = '\n' || x = ' ') str
let starts_with s beg =
let beg_len = String.length beg in
let s_len = String.length s in
beg_len <= s_len &&
(let i = ref 0 in
while !i < beg_len
&& String.unsafe_get s !i =
String.unsafe_get beg !i do
incr i
done;
!i = beg_len
)
let rec ends_aux s end_ j k =
if k < 0 then (j + 1)
else if String.unsafe_get s j = String.unsafe_get end_ k then
ends_aux s end_ (j - 1) (k - 1)
else -1
(** return an index which is minus when [s] does not
end with [beg]
*)
let ends_with_index s end_ : int =
let s_finish = String.length s - 1 in
let s_beg = String.length end_ - 1 in
if s_beg > s_finish then -1
else
ends_aux s end_ s_finish s_beg
let ends_with s end_ = ends_with_index s end_ >= 0
let ends_with_then_chop s beg =
let i = ends_with_index s beg in
if i >= 0 then Some (String.sub s 0 i)
else None
(* let check_suffix_case = ends_with *)
(* let check_suffix_case_then_chop = ends_with_then_chop *)
(* let check_any_suffix_case s suffixes =
Ext_list.exists suffixes (fun x -> check_suffix_case s x) *)
(* let check_any_suffix_case_then_chop s suffixes =
let rec aux suffixes =
match suffixes with
| [] -> None
| x::xs ->
let id = ends_with_index s x in
if id >= 0 then Some (String.sub s 0 id)
else aux xs in
aux suffixes *)
(* it is unsafe to expose such API as unsafe since
user can provide bad input range
*)
let rec unsafe_for_all_range s ~start ~finish p =
start > finish ||
p (String.unsafe_get s start) &&
unsafe_for_all_range s ~start:(start + 1) ~finish p
let for_all_from s start p =
let len = String.length s in
if start < 0 then invalid_arg "Ext_string.for_all_from"
else unsafe_for_all_range s ~start ~finish:(len - 1) p
let for_all s (p : char -> bool) =
unsafe_for_all_range s ~start:0 ~finish:(String.length s - 1) p
let is_empty s = String.length s = 0
let repeat n s =
let len = String.length s in
let res = Bytes.create(n * len) in
for i = 0 to pred n do
String.blit s 0 res (i * len) len
done;
Bytes.to_string res
let unsafe_is_sub ~sub i s j ~len =
let rec check k =
if k = len
then true
else
String.unsafe_get sub (i+k) =
String.unsafe_get s (j+k) && check (k+1)
in
j+len <= String.length s && check 0
let find ?(start=0) ~sub s =
let exception Local_exit in
let n = String.length sub in
let s_len = String.length s in
let i = ref start in
try
while !i + n <= s_len do
if unsafe_is_sub ~sub 0 s !i ~len:n then
raise_notrace Local_exit;
incr i
done;
-1
with Local_exit ->
!i
let contain_substring s sub =
find s ~sub >= 0
(** TODO: optimize
avoid nonterminating when string is empty
*)
let non_overlap_count ~sub s =
let sub_len = String.length sub in
let rec aux acc off =
let i = find ~start:off ~sub s in
if i < 0 then acc
else aux (acc + 1) (i + sub_len) in
if String.length sub = 0 then invalid_arg "Ext_string.non_overlap_count"
else aux 0 0
let rfind ~sub s =
let exception Local_exit in
let n = String.length sub in
let i = ref (String.length s - n) in
try
while !i >= 0 do
if unsafe_is_sub ~sub 0 s !i ~len:n then
raise_notrace Local_exit;
decr i
done;
-1
with Local_exit ->
!i
let tail_from s x =
let len = String.length s in
if x > len then invalid_arg ("Ext_string.tail_from " ^s ^ " : "^ string_of_int x )
else String.sub s x (len - x)
let equal (x : string) y = x = y
(* let rec index_rec s lim i c =
if i >= lim then -1 else
if String.unsafe_get s i = c then i
else index_rec s lim (i + 1) c *)
let rec index_rec_count s lim i c count =
if i >= lim then -1 else
if String.unsafe_get s i = c then
if count = 1 then i
else index_rec_count s lim (i + 1) c (count - 1)
else index_rec_count s lim (i + 1) c count
let index_count s i c count =
let lim = String.length s in
if i < 0 || i >= lim || count < 1 then
invalid_arg ("index_count: ( " ^string_of_int i ^ "," ^string_of_int count ^ ")" );
index_rec_count s lim i c count
(* let index_next s i c =
index_count s i c 1 *)
(* let extract_until s cursor c =
let len = String.length s in
let start = !cursor in
if start < 0 || start >= len then (
cursor := -1;
""
)
else
let i = index_rec s len start c in
let finish =
if i < 0 then (
cursor := -1 ;
len
)
else (
cursor := i + 1;
i
) in
String.sub s start (finish - start) *)
let rec rindex_rec s i c =
if i < 0 then i else
if String.unsafe_get s i = c then i else rindex_rec s (i - 1) c;;
let rec rindex_rec_opt s i c =
if i < 0 then None else
if String.unsafe_get s i = c then Some i else rindex_rec_opt s (i - 1) c;;
let rindex_neg s c =
rindex_rec s (String.length s - 1) c;;
let rindex_opt s c =
rindex_rec_opt s (String.length s - 1) c;;
(** TODO: can be improved to return a positive integer instead *)
let rec unsafe_no_char x ch i last_idx =
i > last_idx ||
(String.unsafe_get x i <> ch && unsafe_no_char x ch (i + 1) last_idx)
let rec unsafe_no_char_idx x ch i last_idx =
if i > last_idx then -1
else
if String.unsafe_get x i <> ch then
unsafe_no_char_idx x ch (i + 1) last_idx
else i
let no_char x ch i len : bool =
let str_len = String.length x in
if i < 0 || i >= str_len || len >= str_len then invalid_arg "Ext_string.no_char"
else unsafe_no_char x ch i len
let no_slash x =
unsafe_no_char x '/' 0 (String.length x - 1)
let no_slash_idx x =
unsafe_no_char_idx x '/' 0 (String.length x - 1)
let no_slash_idx_from x from =
let last_idx = String.length x - 1 in
assert (from >= 0);
unsafe_no_char_idx x '/' from last_idx
let replace_slash_backward (x : string ) =
let len = String.length x in
if unsafe_no_char x '/' 0 (len - 1) then x
else
String.map (function
| '/' -> '\\'
| x -> x ) x
let replace_backward_slash (x : string)=
let len = String.length x in
if unsafe_no_char x '\\' 0 (len -1) then x
else
String.map (function
|'\\'-> '/'
| x -> x) x
let empty = ""
#ifdef BROWSER
let compare = Bs_hash_stubs.string_length_based_compare
#else
external compare : string -> string -> int = "caml_string_length_based_compare" [@@noalloc];;
#endif
let single_space = " "
let single_colon = ":"
let concat_array sep (s : string array) =
let s_len = Array.length s in
match s_len with
| 0 -> empty
| 1 -> Array.unsafe_get s 0
| _ ->
let sep_len = String.length sep in
let len = ref 0 in
for i = 0 to s_len - 1 do
len := !len + String.length (Array.unsafe_get s i)
done;
let target =
Bytes.create
(!len + (s_len - 1) * sep_len ) in
let hd = (Array.unsafe_get s 0) in
let hd_len = String.length hd in
String.unsafe_blit hd 0 target 0 hd_len;
let current_offset = ref hd_len in
for i = 1 to s_len - 1 do
String.unsafe_blit sep 0 target !current_offset sep_len;
let cur = Array.unsafe_get s i in
let cur_len = String.length cur in
let new_off_set = (!current_offset + sep_len ) in
String.unsafe_blit cur 0 target new_off_set cur_len;
current_offset :=
new_off_set + cur_len ;
done;
Bytes.unsafe_to_string target
let concat3 a b c =
let a_len = String.length a in
let b_len = String.length b in
let c_len = String.length c in
let len = a_len + b_len + c_len in
let target = Bytes.create len in
String.unsafe_blit a 0 target 0 a_len ;
String.unsafe_blit b 0 target a_len b_len;
String.unsafe_blit c 0 target (a_len + b_len) c_len;
Bytes.unsafe_to_string target
let concat4 a b c d =
let a_len = String.length a in
let b_len = String.length b in
let c_len = String.length c in
let d_len = String.length d in
let len = a_len + b_len + c_len + d_len in
let target = Bytes.create len in
String.unsafe_blit a 0 target 0 a_len ;
String.unsafe_blit b 0 target a_len b_len;
String.unsafe_blit c 0 target (a_len + b_len) c_len;
String.unsafe_blit d 0 target (a_len + b_len + c_len) d_len;
Bytes.unsafe_to_string target
let concat5 a b c d e =
let a_len = String.length a in
let b_len = String.length b in
let c_len = String.length c in
let d_len = String.length d in
let e_len = String.length e in
let len = a_len + b_len + c_len + d_len + e_len in
let target = Bytes.create len in
String.unsafe_blit a 0 target 0 a_len ;
String.unsafe_blit b 0 target a_len b_len;
String.unsafe_blit c 0 target (a_len + b_len) c_len;
String.unsafe_blit d 0 target (a_len + b_len + c_len) d_len;
String.unsafe_blit e 0 target (a_len + b_len + c_len + d_len) e_len;
Bytes.unsafe_to_string target
let inter2 a b =
concat3 a single_space b
let inter3 a b c =
concat5 a single_space b single_space c
let inter4 a b c d =
concat_array single_space [| a; b ; c; d|]
let parent_dir_lit = ".."
let current_dir_lit = "."
(* reference {!Bytes.unppercase} *)
let capitalize_ascii (s : string) : string =
if String.length s = 0 then s
else
begin
let c = String.unsafe_get s 0 in
if (c >= 'a' && c <= 'z')
|| (c >= '\224' && c <= '\246')
|| (c >= '\248' && c <= '\254') then
let uc = Char.unsafe_chr (Char.code c - 32) in
let bytes = Bytes.of_string s in
Bytes.unsafe_set bytes 0 uc;
Bytes.unsafe_to_string bytes
else s
end
let capitalize_sub (s : string) len : string =
let slen = String.length s in
if len < 0 || len > slen then invalid_arg "Ext_string.capitalize_sub"
else
if len = 0 then ""
else
let bytes = Bytes.create len in
let uc =
let c = String.unsafe_get s 0 in
if (c >= 'a' && c <= 'z')
|| (c >= '\224' && c <= '\246')
|| (c >= '\248' && c <= '\254') then
Char.unsafe_chr (Char.code c - 32) else c in
Bytes.unsafe_set bytes 0 uc;
for i = 1 to len - 1 do
Bytes.unsafe_set bytes i (String.unsafe_get s i)
done ;
Bytes.unsafe_to_string bytes
let uncapitalize_ascii =
String.uncapitalize_ascii
let lowercase_ascii = String.lowercase_ascii
external (.![]) : string -> int -> int = "%string_unsafe_get"
let get_int_1_unsafe (x : string) off : int =
x.![off]
let get_int_2_unsafe (x : string) off : int =
x.![off] lor
x.![off+1] lsl 8
let get_int_3_unsafe (x : string) off : int =
x.![off] lor
x.![off+1] lsl 8 lor
x.![off+2] lsl 16
let get_int_4_unsafe (x : string) off : int =
x.![off] lor
x.![off+1] lsl 8 lor
x.![off+2] lsl 16 lor
x.![off+3] lsl 24
let get_1_2_3_4 (x : string) ~off len : int =
if len = 1 then get_int_1_unsafe x off
else if len = 2 then get_int_2_unsafe x off
else if len = 3 then get_int_3_unsafe x off
else if len = 4 then get_int_4_unsafe x off
else assert false
let unsafe_sub x offs len =
let b = Bytes.create len in
Ext_bytes.unsafe_blit_string x offs b 0 len;
(Bytes.unsafe_to_string b)
let is_valid_hash_number (x:string) =
let len = String.length x in
len > 0 && (
let a = x.![0] in
a <= 57 &&
(if len > 1 then
a > 48 &&
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
else
a >= 48 )
)
let hash_number_as_i32_exn
( x : string) : int32 =
Int32.of_string x
let first_marshal_char (x : string) =
x <> "" &&
( String.unsafe_get x 0 = '\132')