summaryrefslogtreecommitdiff
path: root/lib/strings.nix
diff options
context:
space:
mode:
authorPatrick Widmer <patrick.widmer@tbwnet.ch>2023-03-02 21:41:51 +0100
committerPatrick Widmer <patrick.widmer@tbwnet.ch>2023-03-03 20:48:55 +0100
commit7089294f10068dbeeed6e2e4d7a24300bf4bacb6 (patch)
treeb905d320eb3a640282fff4d98b0e9068fb136fe0 /lib/strings.nix
parentMerge pull request #217508 from wegank/phodav-2-drop (diff)
downloadnixpkgs-7089294f10068dbeeed6e2e4d7a24300bf4bacb6.tar.gz
strings: add escapeQuery for url encoding
Diffstat (limited to 'lib/strings.nix')
-rw-r--r--lib/strings.nix21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 68d930950662..e49ed4382240 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -34,6 +34,8 @@ rec {
unsafeDiscardStringContext
;
+ asciiTable = import ./ascii-table.nix;
+
/* Concatenate a list of strings.
Type: concatStrings :: [string] -> string
@@ -327,9 +329,7 @@ rec {
=> 40
*/
- charToInt = let
- table = import ./ascii-table.nix;
- in c: builtins.getAttr c table;
+ charToInt = c: builtins.getAttr c asciiTable;
/* Escape occurrence of the elements of `list` in `string` by
prefixing it with a backslash.
@@ -355,6 +355,21 @@ rec {
*/
escapeC = list: replaceStrings list (map (c: "\\x${ toLower (lib.toHexString (charToInt c))}") list);
+ /* Escape the string so it can be safely placed inside a URL
+ query.
+
+ Type: escapeURL :: string -> string
+
+ Example:
+ escapeURL "foo/bar baz"
+ => "foo%2Fbar%20baz"
+ */
+ escapeURL = let
+ unreserved = [ "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "-" "_" "." "~" ];
+ toEscape = builtins.removeAttrs asciiTable unreserved;
+ in
+ replaceStrings (builtins.attrNames toEscape) (lib.mapAttrsToList (_: c: "%${fixedWidthString 2 "0" (lib.toHexString c)}") toEscape);
+
/* Quote string to be used safely within the Bourne shell.
Type: escapeShellArg :: string -> string