add CCString.Split.{left,right}_exn

This commit is contained in:
Simon Cruanes 2016-02-21 00:00:28 +01:00
parent 41536c6dd6
commit 1cf81c0031
2 changed files with 20 additions and 6 deletions

View file

@ -199,15 +199,19 @@ module Split = struct
let seq ~by s = _mkseq ~by s _tuple3 let seq ~by s = _mkseq ~by s _tuple3
let seq_cpy ~by s = _mkseq ~by s String.sub let seq_cpy ~by s = _mkseq ~by s String.sub
let left ~by s = let left_exn ~by s =
let i = find ~sub:by s in let i = find ~sub:by s in
if i = ~-1 then None if i = ~-1 then raise Not_found
else Some (String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)) else String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)
let right ~by s = let left ~by s = try Some (left_exn ~by s) with Not_found -> None
let right_exn ~by s =
let i = rfind ~sub:by s in let i = rfind ~sub:by s in
if i = ~-1 then None if i = ~-1 then raise Not_found
else Some (String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)) else String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)
let right ~by s = try Some (right_exn ~by s) with Not_found -> None
end end
let compare_versions a b = let compare_versions a b =

View file

@ -299,6 +299,11 @@ module Split : sig
the string the string
@since 0.12 *) @since 0.12 *)
val left_exn : by:string -> string -> string * string
(** Split on the first occurrence of [by] from the leftmost part of the string
@raise Not_found if [by] is not part of the string
@since NEXT_RELEASE *)
(*$T (*$T
Split.left ~by:" " "ab cde f g " = Some ("ab", "cde f g ") Split.left ~by:" " "ab cde f g " = Some ("ab", "cde f g ")
Split.left ~by:"_" "abcde" = None Split.left ~by:"_" "abcde" = None
@ -309,6 +314,11 @@ module Split : sig
the string the string
@since 0.12 *) @since 0.12 *)
val right_exn : by:string -> string -> string * string
(** Split on the first occurrence of [by] from the rightmost part of the string
@raise Not_found if [by] is not part of the string
@since NEXT_RELEASE *)
(*$T (*$T
Split.right ~by:" " "ab cde f g" = Some ("ab cde f", "g") Split.right ~by:" " "ab cde f g" = Some ("ab cde f", "g")
Split.right ~by:"_" "abcde" = None Split.right ~by:"_" "abcde" = None