10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-03 12:43:52 +01:00
quantum_package/ocaml/String_ext.ml

143 lines
2.7 KiB
OCaml
Raw Permalink Normal View History

2017-08-18 18:28:33 +02:00
include String
(** Split a string on a given character *)
let split ?(on=' ') str =
2017-08-18 19:43:52 +02:00
split_on_char on str
(*
2017-08-18 18:28:33 +02:00
let rec do_work ?(accu=[]) ?(left="") = function
| "" -> List.rev (left::accu)
| s ->
let new_s =
(length s) - 1
|> sub s 1
in
if (s.[0] = on) then
let new_accu =
left :: accu
in
do_work ~accu:new_accu new_s
else
let new_left =
concat "" [ left ; make 1 s.[0] ]
in
do_work ~accu ~left:new_left new_s
in
do_work str
2017-08-18 19:43:52 +02:00
*)
2017-08-18 18:28:33 +02:00
(** Strip blanks on the left of a string *)
let ltrim s =
let rec do_work s l =
match s.[0] with
| '\n'
| ' ' -> do_work (sub s 1 (l-1)) (l-1)
| _ -> s
in
let l =
length s
in
if (l > 0) then
do_work s l
else
s
(** Strip blanks on the right of a string *)
let rtrim s =
let rec do_work s l =
let newl =
l-1
in
match s.[newl] with
| '\n'
| ' ' -> do_work (sub s 0 (newl)) (newl)
| _ -> s
in
let l =
length s
in
if (l > 0) then
do_work s l
else
s
(** Strip blanks on the right and left of a string *)
let strip = String.trim
(** Split a string in two pieces when a character is found the 1st time from the left *)
let lsplit2_exn ?(on=' ') s =
let length =
String.length s
in
let rec do_work i =
if (i = length) then
begin
raise Not_found
end
else if (s.[i] = on) then
( String.sub s 0 i,
String.sub s (i+1) (length-i-1) )
else
do_work (i+1)
in
do_work 0
(** Split a string in two pieces when a character is found the 1st time from the right *)
let rsplit2_exn ?(on=' ') s =
let length =
String.length s
in
let rec do_work i =
if (i = -1) then
begin
raise Not_found
end
else if (s.[i] = on) then
( String.sub s 0 i,
String.sub s (i+1) (length-i-1) )
else
do_work (i-1)
in
do_work length
2017-08-18 19:43:52 +02:00
let lsplit2 ?(on=' ') s =
try
Some (lsplit2_exn ~on s)
with
| Not_found -> None
let rsplit2 ?(on=' ') s =
try
Some (rsplit2_exn ~on s)
with
| Not_found -> None
2017-08-18 18:28:33 +02:00
let to_list s =
Array.init (String.length s) (fun i -> s.[i])
|> Array.to_list
2017-08-18 19:43:52 +02:00
2017-08-18 18:28:33 +02:00
let fold ~init ~f s =
to_list s
|> List.fold_left f init
2017-08-18 19:43:52 +02:00
2017-08-18 18:28:33 +02:00
2017-08-18 19:43:52 +02:00
let is_prefix ~prefix s =
let len =
String.length prefix
in
if len > String.length s then
false
else
prefix = String.sub s 0 len
2017-08-18 18:28:33 +02:00
2017-08-18 19:43:52 +02:00
let of_char c =
String.make 1 c