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:
parent
7f18816447
commit
234534e301
|
@ -249,11 +249,7 @@ let drop s n =
|
||||||
|
|
||||||
let split_n s n =
|
let split_n s n =
|
||||||
let len = length s in
|
let len = length s in
|
||||||
if n > len then
|
let n = min n len in
|
||||||
Exn.code_error "String.split_n"
|
|
||||||
[ "s", Sexp.Atom s
|
|
||||||
; "n", Sexp.Atom (string_of_int n)
|
|
||||||
];
|
|
||||||
( sub s ~pos:0 ~len:n
|
( sub s ~pos:0 ~len:n
|
||||||
, sub s ~pos:n ~len:(len - n)
|
, sub s ~pos:n ~len:(len - n)
|
||||||
)
|
)
|
||||||
|
|
|
@ -57,11 +57,7 @@ String.split_n "foobar" 3;;
|
||||||
|
|
||||||
String.split_n "foobar" 10;;
|
String.split_n "foobar" 10;;
|
||||||
[%%expect{|
|
[%%expect{|
|
||||||
Exception:
|
- : string * string = ("foobar", "")
|
||||||
Code_error
|
|
||||||
(List
|
|
||||||
[Atom "String.split_n"; List [Atom "s"; Atom "foobar"];
|
|
||||||
List [Atom "n"; Atom "10"]]).
|
|
||||||
|}]
|
|}]
|
||||||
|
|
||||||
String.split_n "foobar" 0;;
|
String.split_n "foobar" 0;;
|
||||||
|
@ -81,9 +77,5 @@ String.split_n "" 0;;
|
||||||
|
|
||||||
String.split_n "" 10;;
|
String.split_n "" 10;;
|
||||||
[%%expect{|
|
[%%expect{|
|
||||||
Exception:
|
- : string * string = ("", "")
|
||||||
Code_error
|
|
||||||
(List
|
|
||||||
[Atom "String.split_n"; List [Atom "s"; Atom ""];
|
|
||||||
List [Atom "n"; Atom "10"]]).
|
|
||||||
|}]
|
|}]
|
||||||
|
|
Loading…
Reference in New Issue