Source file string_utils.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
(**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
let soi = string_of_int
let string_of_char = String.make 1
let string_before s n = String.sub s 0 n
let string_after s n = String.sub s n (String.length s - n)
let string_starts_with long short =
try
let long = String.sub long 0 (String.length short) in
long = short
with Invalid_argument _ ->
false
let string_ends_with long short =
try
let len = String.length short in
let long = String.sub long (String.length long - len) len in
long = short
with Invalid_argument _ ->
false
let substring_index needle =
let needle_len = String.length needle in
if needle_len = 0 then raise (Invalid_argument needle);
let table = Array.make needle_len 0 in
table.(0) <- (-1);
let pos = ref 2 and cnd = ref 0 in
while !pos < needle_len do
if needle.[!pos - 1] = needle.[!cnd] then
(table.(!pos) <- !cnd + 1; incr pos; incr cnd)
else if !cnd > 0 then
cnd := table.(!cnd)
else
(table.(!pos) <- 0; incr pos)
done;
fun haystack ->
let len = String.length haystack in
let p = ref 0 in
let q = ref 0 in
while !p < len && !q < needle_len do
if haystack.[!p] = needle.[!q] then (incr p; incr q)
else if !q = 0 then incr p
else q := table.(!q)
done;
if !q >= needle_len then !p - needle_len
else -1
let is_substring needle =
let substring_index_memo = substring_index needle in
fun haystack -> (substring_index_memo haystack) >= 0
let lstrip s prefix =
let prefix_length = String.length prefix in
if string_starts_with s prefix
then String.sub s prefix_length (String.length s - prefix_length)
else s
let rstrip s suffix =
let result_length = String.length s - String.length suffix in
if string_ends_with s suffix
then String.sub s 0 result_length
else s
let rpartition s c =
let sep_idx = String.rindex s c in
let first = String.sub s 0 sep_idx in
let second =
String.sub s (sep_idx + 1) (String.length s - sep_idx - 1) in
first, second
let is_lowercase_char =
let a_code, z_code = Char.code 'a', Char.code 'z' in
fun chr ->
let code = Char.code chr in
a_code <= code && code <= z_code
let rec is_not_lowercase str i j =
if is_lowercase_char str.[i] then false
else if i = j then true
else is_not_lowercase str (i + 1) j
let fold_left ~f ~acc str =
let acc = ref acc in
String.iter (fun c -> acc := f (!acc) c) str;
!acc
let split c = Str.split (Str.regexp @@ Char.escaped c)
let replace_char needle replacement =
String.map (fun c -> if c = needle then replacement else c)
let split_into_lines str =
let _, (last_start, lines) = fold_left
~f: (fun (idx, (start, lines)) c ->
if c = '\n' && idx > 0 && String.get str (idx-1) = '\r'
then idx+1, (idx+1, lines)
else
if c = '\n' || c = '\r'
then idx+1, (idx+1, (start, idx-start)::lines)
else idx+1, (start, lines)
)
~acc:(0, (0, []))
str
in
List.fold_left
(fun lines (start, len) -> (String.sub str start len)::lines)
[]
((last_start, String.length str - last_start)::lines)
let split_on_newlines content =
let re = Str.regexp "[\n]" in
let lines = Str.split_delim re content in
match List.rev lines with
| "" :: rest -> List.rev rest
| _ -> lines