summaryrefslogtreecommitdiffstats
path: root/lib/default.nix
diff options
context:
space:
mode:
authorlassulus <lassulus@lassul.us>2018-12-12 15:46:41 +0100
committerlassulus <lassulus@lassul.us>2018-12-12 15:46:41 +0100
commit25cf61f6a74b69656d15f52021f25a6c2e4068e6 (patch)
treefcde9910b7e30a27bfd53e394cb763691882e704 /lib/default.nix
parent4d44efa2fceda1308dbe8207e8fd0f122cd64e19 (diff)
parent35be9c66bfa6dd03437f919ec610aed0e9b20b58 (diff)
Merge remote-tracking branch 'ni/master'
Diffstat (limited to 'lib/default.nix')
-rw-r--r--lib/default.nix35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index e352c7be9..347830e8c 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -93,7 +93,13 @@ let
in
if max.pos == 0
then a
- else "${concatStringsSep ":" lhs}::${concatStringsSep ":" rhs}";
+ else let
+ sep =
+ if 8 - (length lhs + length rhs) == 1
+ then ":0:"
+ else "::";
+ in
+ "${concatStringsSep ":" lhs}${sep}${concatStringsSep ":" rhs}";
drop-leading-zeros =
let
@@ -112,6 +118,33 @@ let
(if test ".*::.*" a
then a
else group-zeros (drop-leading-zeros a));
+
+ hashToLength = n: s: substring 0 n (hashString "sha256" s);
+
+ dropLast = n: xs: reverseList (drop n (reverseList xs));
+ takeLast = n: xs: reverseList (take n (reverseList xs));
+
+ # Split string into list of chunks where each chunk is at most n chars long.
+ # The leftmost chunk might shorter.
+ # Example: stringToGroupsOf "123456" -> ["12" "3456"]
+ stringToGroupsOf = n: s: let
+ acc =
+ foldl'
+ (acc: c: if stringLength acc.chunk < n then {
+ chunk = acc.chunk + c;
+ chunks = acc.chunks;
+ } else {
+ chunk = c;
+ chunks = acc.chunks ++ [acc.chunk];
+ })
+ {
+ chunk = "";
+ chunks = [];
+ }
+ (stringToCharacters s);
+ in
+ filter (x: x != []) ([acc.chunk] ++ acc.chunks);
+
};
in