Make String.spit_n work with n > len

To make things consistent with drop/take

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-08-26 14:43:22 +03:00
parent 7f18816447
commit 234534e301
2 changed files with 3 additions and 15 deletions

View File

@ -249,11 +249,7 @@ let drop s n =
let split_n s n =
let len = length s in
if n > len then
Exn.code_error "String.split_n"
[ "s", Sexp.Atom s
; "n", Sexp.Atom (string_of_int n)
];
let n = min n len in
( sub s ~pos:0 ~len:n
, sub s ~pos:n ~len:(len - n)
)

View File

@ -57,11 +57,7 @@ String.split_n "foobar" 3;;
String.split_n "foobar" 10;;
[%%expect{|
Exception:
Code_error
(List
[Atom "String.split_n"; List [Atom "s"; Atom "foobar"];
List [Atom "n"; Atom "10"]]).
- : string * string = ("foobar", "")
|}]
String.split_n "foobar" 0;;
@ -81,9 +77,5 @@ String.split_n "" 0;;
String.split_n "" 10;;
[%%expect{|
Exception:
Code_error
(List
[Atom "String.split_n"; List [Atom "s"; Atom ""];
List [Atom "n"; Atom "10"]]).
- : string * string = ("", "")
|}]