about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Olsen <daniel.olsen99@gmail.com>2022-06-11 11:18:54 +0200
committerDaniel Olsen <daniel.olsen99@gmail.com>2022-10-20 20:12:15 +0200
commit4c420ee4854d8b0a9d0d224588f0cd033f6b4381 (patch)
treed64f26d26ece9ba69f41edb7c5608b14cf18be2b /lib
parenta08741ffbdf1adfb8c4acc790120faed8251ee79 (diff)
lib.strings: Add function to do C-style escaping
Diffstat (limited to 'lib')
-rw-r--r--lib/strings.nix13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 54f89d1be2581..b12dac54f4ff3 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -320,6 +320,19 @@ rec {
   */
   escape = list: replaceChars list (map (c: "\\${c}") list);
 
+  /* Escape occurence of the element of `list` in `string` by
+     converting to its ASCII value and prefixing it with \\x.
+     Only works for printable ascii characters.
+
+     Type: escapeC = [string] -> string -> string
+
+     Example:
+       escapeC [" "] "foo bar"
+       => "foo\\x20bar"
+
+  */
+  escapeC = list: replaceChars list (map (c: "\\x${ toLower (lib.toHexString (charToInt c))}") list);
+
   /* Quote string to be used safely within the Bourne shell.
 
      Type: escapeShellArg :: string -> string