@@ -97,6 +97,8 @@ let rec split_last = function
97
97
98
98
module Stdlib = struct
99
99
module List = struct
100
+ include List
101
+
100
102
type 'a t = 'a list
101
103
102
104
let rec compare cmp l1 l2 =
@@ -227,6 +229,45 @@ module Stdlib = struct
227
229
228
230
let print ppf t =
229
231
Format. pp_print_string ppf t
232
+
233
+ let begins_with ?(from = 0 ) str ~prefix =
234
+ let rec helper idx =
235
+ if idx < 0 then true
236
+ else
237
+ String. get str (from + idx) = String. get prefix idx && helper (idx-1 )
238
+ in
239
+ let n = String. length str in
240
+ let m = String. length prefix in
241
+ if n > = from + m then helper (m-1 ) else false
242
+
243
+ let split_on_string str ~split_on =
244
+ let n = String. length str in
245
+ let m = String. length split_on in
246
+ let rec helper acc last_idx idx =
247
+ if idx = n then
248
+ let cur = String. sub str last_idx (idx - last_idx) in
249
+ List. rev (cur :: acc)
250
+ else if begins_with ~from: idx str ~prefix: split_on then
251
+ let cur = String. sub str last_idx (idx - last_idx) in
252
+ helper (cur :: acc) (idx + m) (idx + m)
253
+ else
254
+ helper acc last_idx (idx + 1 )
255
+ in
256
+ helper [] 0 0
257
+
258
+ let split_on_chars str ~split_on :chars =
259
+ let rec helper chars_left s acc =
260
+ match chars_left with
261
+ | [] -> s :: acc
262
+ | c :: cs ->
263
+ List. fold_right (helper cs) (String. split_on_char c s) acc
264
+ in
265
+ helper chars str []
266
+
267
+ let split_last_exn str ~split_on =
268
+ let n = String. length str in
269
+ let ridx = String. rindex str split_on in
270
+ String. sub str 0 ridx, String. sub str (ridx + 1 ) (n - ridx - 1 )
230
271
end
231
272
232
273
external compare : 'a -> 'a -> int = " %compare"
0 commit comments