summaryrefslogtreecommitdiffstats
path: root/lib/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'lib/default.nix')
-rw-r--r--lib/default.nix27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix
index e352c7be..64b2d48a 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -112,6 +112,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