From 1cf81c003184abac38c5aff3da13e35eec74ad69 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 21 Feb 2016 00:00:28 +0100 Subject: [PATCH] add `CCString.Split.{left,right}_exn` --- src/core/CCString.cppo.ml | 16 ++++++++++------ src/core/CCString.mli | 10 ++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index dacda08b..0574eab9 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -199,15 +199,19 @@ module Split = struct let seq ~by s = _mkseq ~by s _tuple3 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 - if i = ~-1 then None - else Some (String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)) + if i = ~-1 then raise Not_found + 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 - if i = ~-1 then None - else Some (String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)) + if i = ~-1 then raise Not_found + 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 let compare_versions a b = diff --git a/src/core/CCString.mli b/src/core/CCString.mli index b9201530..dd2b82d5 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -299,6 +299,11 @@ module Split : sig the string @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 Split.left ~by:" " "ab cde f g " = Some ("ab", "cde f g ") Split.left ~by:"_" "abcde" = None @@ -309,6 +314,11 @@ module Split : sig the string @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 Split.right ~by:" " "ab cde f g" = Some ("ab cde f", "g") Split.right ~by:"_" "abcde" = None