summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2023-11-07 18:29:31 +0100
committerGitHub <noreply@github.com>2023-11-07 18:29:31 +0100
commit17012aa0d202dcce7ac51cf82e047674f9fe545e (patch)
treef73f3944163b45939262e797cba9500fcbcc2843 /lib
parent0cd6f66fb7b23346550564255439d4d8a2156be2 (diff)
parent206d20426cb2959aab8cc3cfc8e62aa68bc7b52f (diff)
Merge pull request #261676 from h7x4/lib-add-replicatestring
lib.strings: add `replicate`
Diffstat (limited to 'lib')
-rw-r--r--lib/strings.nix14
-rw-r--r--lib/tests/misc.nix5
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 628669d86bbd4..695aaaacd3488 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -144,6 +144,20 @@ rec {
   */
   concatLines = concatMapStrings (s: s + "\n");
 
+  /*
+    Replicate a string n times,
+    and concatenate the parts into a new string.
+
+    Type: replicate :: int -> string -> string
+
+    Example:
+      replicate 3 "v"
+      => "vvv"
+      replicate 5 "hello"
+      => "hellohellohellohellohello"
+  */
+  replicate = n: s: concatStrings (lib.lists.replicate n s);
+
   /* 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 47853f47278a3..0d30e93aafb9d 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -191,6 +191,11 @@ runTests {
     expected = "a\nb\nc\n";
   };
 
+  testReplicateString = {
+    expr = strings.replicate 5 "hello";
+    expected = "hellohellohellohellohello";
+  };
+
   testSplitStringsSimple = {
     expr = strings.splitString "." "a.b.c.d";
     expected = [ "a" "b" "c" "d" ];