fix bug in CCString.Split thanks to @copy (close #75)

This commit is contained in:
Simon Cruanes 2016-08-22 09:26:36 +02:00
commit 7b49dd7f3d
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,8 +449,10 @@ 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:"bb" "abbc" = Some ("a", "c")
Split.left ~by:"a_" "abcde" = None
*)
val right : by:string -> string -> (string * string) option
@ -465,7 +467,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