3
0
Fork 0
forked from mirrors/nixpkgs

libs: make splitString also split last separator (#23851)

* libs: make splitString also split last separator

* libs: add tests for splitStrings
This commit is contained in:
Léo Gaspard 2017-03-15 22:16:04 +01:00 committed by zimbatm
parent e99bc64552
commit da6619cffe
2 changed files with 31 additions and 1 deletions

View file

@ -291,7 +291,7 @@ rec {
recurse = index: startAt:
let cutUntil = i: [(substring startAt (i - startAt) s)]; in
if index < lastSearch then
if index <= lastSearch then
if startWithSep index then
let restartAt = index + sepLen; in
cutUntil index ++ recurse restartAt restartAt

View file

@ -220,4 +220,34 @@ runTests {
expected = builtins.toJSON val;
};
testSplitStringsSimple = {
expr = strings.splitString "." "a.b.c.d";
expected = [ "a" "b" "c" "d" ];
};
testSplitStringsEmpty = {
expr = strings.splitString "." "a..b";
expected = [ "a" "" "b" ];
};
testSplitStringsOne = {
expr = strings.splitString ":" "a.b";
expected = [ "a.b" ];
};
testSplitStringsNone = {
expr = strings.splitString "." "";
expected = [ "" ];
};
testSplitStringsFirstEmpty = {
expr = strings.splitString "/" "/a/b/c";
expected = [ "" "a" "b" "c" ];
};
testSplitStringsLastEmpty = {
expr = strings.splitString ":" "2001:db8:0:0042::8a2e:370:";
expected = [ "2001" "db8" "0" "0042" "" "8a2e" "370" "" ];
};
}