mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-11 05:28:31 -05:00
add CCString.replace and tests
This commit is contained in:
parent
9c61cfe53a
commit
87e2ab90e1
2 changed files with 51 additions and 0 deletions
|
|
@ -116,6 +116,39 @@ let rfind ~sub s =
|
||||||
with Exit ->
|
with Exit ->
|
||||||
!i
|
!i
|
||||||
|
|
||||||
|
(* replace substring [s.[pos]....s.[pos+len-1]] by [by] in [s] *)
|
||||||
|
let replace_at_ ~pos ~len ~by s =
|
||||||
|
let b = Buffer.create (length s + length by - len) in
|
||||||
|
Buffer.add_substring b s 0 pos;
|
||||||
|
Buffer.add_string b by;
|
||||||
|
Buffer.add_substring b s (pos+len) (String.length s - pos - len);
|
||||||
|
Buffer.contents b
|
||||||
|
|
||||||
|
let replace ?(which=`All) ~sub ~by s = match which with
|
||||||
|
| `Left ->
|
||||||
|
let i = find ~sub s in
|
||||||
|
if i>=0 then replace_at_ ~pos:i ~len:(String.length sub) ~by s else s
|
||||||
|
| `Right ->
|
||||||
|
let i = rfind ~sub s in
|
||||||
|
if i>=0 then replace_at_ ~pos:i ~len:(String.length sub) ~by s else s
|
||||||
|
| `All ->
|
||||||
|
let b = Buffer.create (String.length s) in
|
||||||
|
let start = ref 0 in
|
||||||
|
while !start < String.length s do
|
||||||
|
let i = find ~start:!start ~sub s in
|
||||||
|
if i>=0 then (
|
||||||
|
(* between last and cur occurrences *)
|
||||||
|
Buffer.add_substring b s !start (i- !start);
|
||||||
|
Buffer.add_string b by;
|
||||||
|
start := i + String.length sub
|
||||||
|
) else (
|
||||||
|
(* add remainder *)
|
||||||
|
Buffer.add_substring b s !start (String.length s - !start);
|
||||||
|
start := String.length s (* stop *)
|
||||||
|
)
|
||||||
|
done;
|
||||||
|
Buffer.contents b
|
||||||
|
|
||||||
module Split = struct
|
module Split = struct
|
||||||
type split_state =
|
type split_state =
|
||||||
| SplitStop
|
| SplitStop
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,24 @@ val rfind : sub:string -> string -> int
|
||||||
rfind ~sub:"bc" "abcdbcd" = 4
|
rfind ~sub:"bc" "abcdbcd" = 4
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
val replace : ?which:[`Left|`Right|`All] -> sub:string -> by:string -> string -> string
|
||||||
|
(** [replace ~sub ~by s] replaces some occurrences of [sub] by [by] in [s]
|
||||||
|
@param which decides whether the occurrences to replace are:
|
||||||
|
{ul
|
||||||
|
{il [`Left] first occurrence from the left (beginning)}
|
||||||
|
{il [`Right] first occurrence from the right (end)}
|
||||||
|
{il [`All] all occurrences (default)}
|
||||||
|
}
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
(*$= & ~printer:CCFun.id
|
||||||
|
(replace ~which:`All ~sub:"a" ~by:"b" "abcdabcd") "bbcdbbcd"
|
||||||
|
(replace ~which:`Left ~sub:"a" ~by:"b" "abcdabcd") "bbcdabcd"
|
||||||
|
(replace ~which:`Right ~sub:"a" ~by:"b" "abcdabcd") "abcdbbcd"
|
||||||
|
(replace ~which:`All ~sub:"ab" ~by:"hello" " abab cdabb a") \
|
||||||
|
" hellohello cdhellob a"
|
||||||
|
*)
|
||||||
|
|
||||||
val is_sub : sub:string -> int -> string -> int -> len:int -> bool
|
val is_sub : sub:string -> int -> string -> int -> len:int -> bool
|
||||||
(** [is_sub ~sub i s j ~len] returns [true] iff the substring of
|
(** [is_sub ~sub i s j ~len] returns [true] iff the substring of
|
||||||
[sub] starting at position [i] and of length [len] *)
|
[sub] starting at position [i] and of length [len] *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue