diff options
author | Silvan Mosberger <contact@infinisil.com> | 2020-03-14 17:53:47 +0100 |
---|---|---|
committer | Silvan Mosberger <contact@infinisil.com> | 2020-04-13 17:27:04 +0200 |
commit | f9eb3d158a47dccb1e4762f3ed91f224fc96dc7e (patch) | |
tree | 46d7c72348f9dd4a95a7d25ad31fabc7c4ceea61 /lib/strings.nix | |
parent | f9e94b9ffee57a20841dbf35d1e96c22cfbcc56e (diff) |
lib/strings: Introduce escapeNixIdentifier
Diffstat (limited to 'lib/strings.nix')
-rw-r--r-- | lib/strings.nix | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix index 7ecd6b143ee8..74e3eaa0722d 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -315,6 +315,21 @@ rec { */ escapeNixString = s: escape ["$"] (builtins.toJSON s); + /* Quotes a string if it can't be used as an identifier directly. + + Type: string -> string + + Example: + escapeNixIdentifier "hello" + => "hello" + escapeNixIdentifier "0abc" + => "\"0abc\"" + */ + escapeNixIdentifier = s: + # Regex from https://github.com/NixOS/nix/blob/d048577909e383439c2549e849c5c2f2016c997e/src/libexpr/lexer.l#L91 + if builtins.match "[a-zA-Z_][a-zA-Z0-9_'-]*" s != null + then s else escapeNixString s; + # Obsolete - use replaceStrings instead. replaceChars = builtins.replaceStrings or ( del: new: s: |