about summary refs log tree commit diff
path: root/users/sterni/nix/url/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'users/sterni/nix/url/default.nix')
-rw-r--r--users/sterni/nix/url/default.nix39
1 files changed, 39 insertions, 0 deletions
diff --git a/users/sterni/nix/url/default.nix b/users/sterni/nix/url/default.nix
index ce7ed9b83437..37bd0de66ac9 100644
--- a/users/sterni/nix/url/default.nix
+++ b/users/sterni/nix/url/default.nix
@@ -6,6 +6,7 @@ let
     char
     int
     string
+    flow
     ;
 
   reserved = c: builtins.elem c [
@@ -35,8 +36,46 @@ let
         else percentEncode c;
     in lib.concatStrings (builtins.map tr chars);
 
+  decode = s:
+    let
+      tokens = builtins.split "%" s;
+      decodeStep =
+        { result ? ""
+        , inPercent ? false
+        }: s:
+        flow.cond [
+          [
+            (builtins.isList s)
+            {
+              inherit result;
+              inPercent = true;
+            }
+          ]
+          [
+            inPercent
+            {
+              inPercent = false;
+              # first two characters came after an %
+              # the rest is the string until the next %
+              result = result
+                + char.chr (int.fromHex (string.take 2 s))
+                + (string.drop 2 s);
+            }
+          ]
+          [
+            (!inPercent)
+            {
+              result = result + s;
+            }
+          ]
+        ];
+
+    in
+      (builtins.foldl' decodeStep {} tokens).result;
+
 in {
   inherit
     encode
+    decode
     ;
 }