From ed0b8c26f127525a9ee66f895bdc894cdaa5d685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Sat, 10 Dec 2022 15:56:30 +0100 Subject: [PATCH] lib/strings: add `concatLines` Like `unlines` from Haskell. The aim is to replace the `concatStringsSep "\n"` pattern for generated files, which doesn't add a final newline. --- lib/default.nix | 2 +- lib/strings.nix | 11 +++++++++++ lib/tests/misc.nix | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 68e5b8dea1eb..48d066e17ccc 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -93,7 +93,7 @@ let subtractLists mutuallyExclusive groupBy groupBy'; inherit (self.strings) concatStrings concatMapStrings concatImapStrings intersperse concatStringsSep concatMapStringsSep - concatImapStringsSep makeSearchPath makeSearchPathOutput + concatImapStringsSep concatLines makeSearchPath makeSearchPathOutput makeLibraryPath makeBinPath optionalString hasInfix hasPrefix hasSuffix stringToCharacters stringAsChars escape escapeShellArg escapeShellArgs isValidPosixName toShellVar toShellVars diff --git a/lib/strings.nix b/lib/strings.nix index 9a4f29380d0d..32132e83b242 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -127,6 +127,17 @@ rec { # List of input strings list: concatStringsSep sep (lib.imap1 f list); + /* Concatenate a list of strings, adding a newline at the end of each one. + Defined as `concatMapStrings (s: s + "\n")`. + + Type: concatLines :: [string] -> string + + Example: + concatLines [ "foo" "bar" ] + => "foo\nbar\n" + */ + concatLines = concatMapStrings (s: s + "\n"); + /* Construct a Unix-style, colon-separated search path consisting of the given `subDir` appended to each of the given paths. diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 648c05ab3572..7cd6dd182f47 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -153,6 +153,11 @@ runTests { expected = "a,b,c"; }; + testConcatLines = { + expr = concatLines ["a" "b" "c"]; + expected = "a\nb\nc\n"; + }; + testSplitStringsSimple = { expr = strings.splitString "." "a.b.c.d"; expected = [ "a" "b" "c" "d" ];