Fix CCString.Split.{left,right} (#75)

This commit is contained in:
Fabian 2016-08-22 01:52:44 +02:00
parent e704020e35
commit 47abd87f74
2 changed files with 10 additions and 2 deletions

View file

@ -367,14 +367,18 @@ module Split = struct
let left_exn ~by s =
let i = find ~sub:by s in
if i = ~-1 then raise Not_found
else String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)
else
let right = i + String.length by in
String.sub s 0 i, String.sub s right (String.length s - right)
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 raise Not_found
else String.sub s 0 i, String.sub s (i+1) (String.length s - i - 1)
else
let right = i + String.length by in
String.sub s 0 i, String.sub s right (String.length s - right)
let right ~by s = try Some (right_exn ~by s) with Not_found -> None
end

View file

@ -449,7 +449,9 @@ module Split : sig
(*$T
Split.left ~by:" " "ab cde f g " = Some ("ab", "cde f g ")
Split.left ~by:"__" "a__c__e_f" = Some ("a", "c__e_f")
Split.left ~by:"_" "abcde" = None
Split.left ~by:"a_" "abcde" = None
*)
val right : by:string -> string -> (string * string) option
@ -464,7 +466,9 @@ module Split : sig
(*$T
Split.right ~by:" " "ab cde f g" = Some ("ab cde f", "g")
Split.right ~by:"__" "a__c__e_f" = Some ("a__c", "e_f")
Split.right ~by:"_" "abcde" = None
Split.right ~by:"a_" "abcde" = None
*)
end