mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Update CCString.ml with take_while, rtake_while
Added two functions to the `CCString` module addressing #463 following the style of `CCString.drop_while` and `CCString.rdrop_while`. Corresponding `CCString.mli` changes to follow.
This commit is contained in:
parent
2e8d70f073
commit
d8c00f96be
1 changed files with 18 additions and 0 deletions
|
|
@ -585,6 +585,24 @@ let take n s =
|
||||||
else
|
else
|
||||||
s
|
s
|
||||||
|
|
||||||
|
let take_while p s =
|
||||||
|
let i = ref 0 in
|
||||||
|
while !i < String.length s && p (String.unsafe_get s !i) do
|
||||||
|
incr i
|
||||||
|
done;
|
||||||
|
String.sub s 0 !i
|
||||||
|
|
||||||
|
let rtake_while p s =
|
||||||
|
let s_len_pred = String.length s - 1 in
|
||||||
|
let i = ref s_len_pred in
|
||||||
|
while !i >= 0 && p (String.unsafe_get s !i) do
|
||||||
|
decr i
|
||||||
|
done ;
|
||||||
|
if !i < s_len_pred
|
||||||
|
then String.sub s (!i + 1) (s_len_pred - !i)
|
||||||
|
else
|
||||||
|
""
|
||||||
|
|
||||||
let drop n s =
|
let drop n s =
|
||||||
if n < String.length s then
|
if n < String.length s then
|
||||||
String.sub s n (String.length s - n)
|
String.sub s n (String.length s - n)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue