From da6619cffed4aae4da168a11ae46473ad397b07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gaspard?= Date: Wed, 15 Mar 2017 22:16:04 +0100 Subject: [PATCH] libs: make splitString also split last separator (#23851) * libs: make splitString also split last separator * libs: add tests for splitStrings --- lib/strings.nix | 2 +- lib/tests.nix | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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" "" ]; + }; + }