mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-01-30 08:44:31 +00:00
lib/strings: Introduce escapeNixIdentifier
This commit is contained in:
parent
f9e94b9ffe
commit
f9eb3d158a
@ -315,6 +315,21 @@ rec {
|
|||||||
*/
|
*/
|
||||||
escapeNixString = s: escape ["$"] (builtins.toJSON s);
|
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.
|
# Obsolete - use replaceStrings instead.
|
||||||
replaceChars = builtins.replaceStrings or (
|
replaceChars = builtins.replaceStrings or (
|
||||||
del: new: s:
|
del: new: s:
|
||||||
|
Loading…
Reference in New Issue
Block a user