mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
Add CCString.{,r}drop_while
This commit is contained in:
parent
9e105a3fbc
commit
d8caef8c02
2 changed files with 22 additions and 9 deletions
|
|
@ -987,20 +987,23 @@ let exists p s =
|
|||
try iter (fun c -> if p c then raise MyExit) s; false
|
||||
with MyExit -> true
|
||||
|
||||
let drop_while f s =
|
||||
let i = ref 0 in
|
||||
while !i < length s && f (unsafe_get s !i) do incr i done;
|
||||
if !i > 0 then sub s !i (length s - !i) else s
|
||||
|
||||
let rdrop_while f s =
|
||||
let i = ref (length s-1) in
|
||||
while !i >= 0 && f (unsafe_get s !i) do decr i done;
|
||||
if !i < length s-1 then sub s 0 (!i+1) else s
|
||||
|
||||
(* notion of whitespace for trim *)
|
||||
let is_space_ = function
|
||||
| ' ' | '\012' | '\n' | '\r' | '\t' -> true
|
||||
| _ -> false
|
||||
|
||||
let ltrim s =
|
||||
let i = ref 0 in
|
||||
while !i < length s && is_space_ (unsafe_get s !i) do incr i done;
|
||||
if !i > 0 then sub s !i (length s - !i) else s
|
||||
|
||||
let rtrim s =
|
||||
let i = ref (length s-1) in
|
||||
while !i >= 0 && is_space_ (unsafe_get s !i) do decr i done;
|
||||
if !i < length s-1 then sub s 0 (!i+1) else s
|
||||
let ltrim s = drop_while is_space_ s
|
||||
let rtrim s = rdrop_while is_space_ s
|
||||
|
||||
(*$= & ~printer:id
|
||||
"abc " (ltrim " abc ")
|
||||
|
|
|
|||
|
|
@ -246,6 +246,16 @@ val exists : (char -> bool) -> string -> bool
|
|||
|
||||
include S with type t := string
|
||||
|
||||
val drop_while : (char -> bool) -> t -> t
|
||||
(** [drop_while f s] discards any characters starting from the left,
|
||||
up to the first character [c] not satisfying [f c].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val rdrop_while : (char -> bool) -> t -> t
|
||||
(** [rdrop_while f s] discards any characters starting from the right,
|
||||
up to the first character [c] not satisfying [f c].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val ltrim : t -> t
|
||||
(** Trim space on the left (see {!String.trim} for more details).
|
||||
@since 1.2 *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue