From 00022fbeda385d7b6ae2eee44f07eecfc6d92015 Mon Sep 17 00:00:00 2001 From: Bas van Dijk Date: Mon, 20 Apr 2020 12:00:23 +0200 Subject: [PATCH] lib: add the toHex and toBase utility functions `toHex` converts the given positive integer to a string of the hexadecimal representation of that integer. For example: ``` toHex 0 => "0" toHex 16 => "10" toHex 250 => "FA" ``` `toBase base i` converts the positive integer `i` to a list of it digits in the given `base`. For example: ``` toBase 10 123 => [ 1 2 3 ] toBase 2 6 => [ 1 1 0 ] toBase 16 250 => [ 15 10 ] ``` --- lib/default.nix | 2 +- lib/tests/misc.nix | 10 +++++++++ lib/trivial.nix | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/default.nix b/lib/default.nix index 7387e9d9f1f..a5c768ff407 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -67,7 +67,7 @@ let inherit (trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell min max importJSON warn info showWarnings nixpkgsVersion version mod compare - splitByAndCompare functionArgs setFunctionArgs isFunction; + splitByAndCompare functionArgs setFunctionArgs isFunction toHex toBase; inherit (fixedPoints) fix fix' converge extends composeExtensions makeExtensible makeExtensibleWithCustomName; inherit (attrsets) attrByPath hasAttrByPath setAttrByPath diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 36ddd186d7b..7d38b56bc21 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -102,6 +102,16 @@ runTests { expected = 9; }; + testToHex = { + expr = toHex 250; + expected = "FA"; + }; + + testToBase = { + expr = toBase 2 6; + expected = [ 1 1 0 ]; + }; + # STRINGS testConcatMapStrings = { diff --git a/lib/trivial.nix b/lib/trivial.nix index 5788dd435e5..1114e94b523 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -332,4 +332,55 @@ rec { */ isFunction = f: builtins.isFunction f || (f ? __functor && isFunction (f.__functor f)); + + /* Convert the given positive integer to a string of its hexadecimal + representation. For example: + + toHex 0 => "0" + + toHex 16 => "10" + + toHex 250 => "FA" + */ + toHex = i: + let + toHexDigit = d: + if d < 10 + then toString d + else + { + "10" = "A"; + "11" = "B"; + "12" = "C"; + "13" = "D"; + "14" = "E"; + "15" = "F"; + }.${toString d}; + in + lib.concatMapStrings toHexDigit (toBase 16 i); + + /* `toBase base i` converts the positive integer i to a list of its + digits in the given base. For example: + + toBase 10 123 => [ 1 2 3 ] + + toBase 2 6 => [ 1 1 0 ] + + toBase 16 250 => [ 15 10 ] + */ + toBase = base: i: + let + go = i: + if i < base + then [i] + else + let + r = i - ((i / base) * base); + q = (i - r) / base; + in + [r] ++ go q; + in + assert (base >= 2); + assert (i >= 0); + lib.reverseList (go i); }