-
Notifications
You must be signed in to change notification settings - Fork 3
/
hex.nix
46 lines (43 loc) · 911 Bytes
/
hex.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{ lib, ... }:
let
inherit (builtins) getAttr hasAttr;
inherit (lib.lists) foldl;
inherit (lib.strings) stringToCharacters toUpper;
inherit (lib.trivial) toHexString;
# Parse a single hexadecimal digit to an integer
_parseDigit = c:
let
k = toUpper c;
dict = {
"0" = 0;
"1" = 1;
"2" = 2;
"3" = 3;
"4" = 4;
"5" = 5;
"6" = 6;
"7" = 7;
"8" = 8;
"9" = 9;
"A" = 10;
"B" = 11;
"C" = 12;
"D" = 13;
"E" = 14;
"F" = 15;
};
in
assert(hasAttr k dict);
getAttr k dict;
in
{
# Convert an hexadecimal string to an integer
toDec = s:
let
characters = stringToCharacters s;
values = map _parseDigit characters;
in
foldl (acc: n: acc * 16 + n) 0 values;
# Convert an integer to a decimal string
fromDec = toHexString;
}