diff --git a/lib/strings.nix b/lib/strings.nix index 86af4d438344..eb3c2971ad59 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -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 diff --git a/lib/tests.nix b/lib/tests.nix index 251282d29904..bef9cdee696d 100644 --- a/lib/tests.nix +++ b/lib/tests.nix @@ -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" "" ]; + }; + }