tvl-depot/users/sterni/nix/url/default.nix
Vincent Ambo aa122cbae7 style: format entire depot with nixpkgs-fmt
This CL can be used to compare the style of nixpkgs-fmt against other
formatters (nixpkgs, alejandra).

Change-Id: I87c6abff6bcb546b02ead15ad0405f81e01b6d9e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4397
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Reviewed-by: lukegb <lukegb@tvl.fyi>
Reviewed-by: wpcarro <wpcarro@gmail.com>
Reviewed-by: Profpatsch <mail@profpatsch.de>
Reviewed-by: kanepyork <rikingcoding@gmail.com>
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: cynthia <cynthia@tvl.fyi>
Reviewed-by: edef <edef@edef.eu>
Reviewed-by: eta <tvl@eta.st>
Reviewed-by: grfn <grfn@gws.fyi>
2022-01-31 16:11:53 +00:00

100 lines
1.6 KiB
Nix

{ depot, lib, ... }:
let
inherit (depot.users.sterni.nix)
char
int
string
flow
;
reserved = c: builtins.elem c [
"!"
"#"
"$"
"&"
"'"
"("
")"
"*"
"+"
","
"/"
":"
";"
"="
"?"
"@"
"["
"]"
];
unreserved = c: char.asciiAlphaNum c
|| builtins.elem c [ "-" "_" "." "~" ];
percentEncode = c:
if unreserved c
then c
else "%" + (string.fit
{
width = 2;
char = "0";
side = "left";
}
(int.toHex (char.ord c)));
encode = { leaveReserved ? false }: s:
let
chars = lib.stringToCharacters s;
tr = c:
if leaveReserved && reserved c
then c
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
;
}