diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix index 3dde29ca..d831b430 100644 --- a/subprojects/bahaghari/lib/default.nix +++ b/subprojects/bahaghari/lib/default.nix @@ -23,4 +23,5 @@ pkgs.lib.makeExtensible inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs generateGlyphSet generateConversionTable generateBaseDigitType pow; + inherit (self.hex) isHexString; }) diff --git a/subprojects/bahaghari/lib/hex.nix b/subprojects/bahaghari/lib/hex.nix index f8d43fab..3d71a1b2 100644 --- a/subprojects/bahaghari/lib/hex.nix +++ b/subprojects/bahaghari/lib/hex.nix @@ -3,8 +3,18 @@ # purpose. { pkgs, lib }: +let + glyphList = + [ "0" "1" "2" "3" "4" "5" "6" "7" + "8" "9" "A" "B" "C" "D" "E" "F" ]; + + baseSet = lib.generateBaseDigitType glyphList; +in rec { - inherit (pkgs.lib.trivial) toHexString; + /* Returns a convenient glyph set for creating your own conversion or + hex-related functions. + */ + inherit (baseSet) glyphSet conversionTable fromDec toDec; /* A variant of `lib.lists.range` function just with hexadecimal digits. @@ -15,7 +25,7 @@ rec { => [ "F" "10" "11" ] */ range = first: last: - builtins.map (n: toHexString n) (pkgs.lib.lists.range first last); + builtins.map (n: baseSet.fromDec n) (pkgs.lib.lists.range first last); /* Checks if the given hex string is valid or not. diff --git a/subprojects/bahaghari/tests/lib/hex.nix b/subprojects/bahaghari/tests/lib/hex.nix index b0faa8ba..9ec5bbd4 100644 --- a/subprojects/bahaghari/tests/lib/hex.nix +++ b/subprojects/bahaghari/tests/lib/hex.nix @@ -2,10 +2,30 @@ pkgs.lib.runTests { testToHexString = { - expr = lib.hex.toHexString 293454837; + expr = lib.hex.fromDec 293454837; expected = "117DC3F5"; }; + testToHexString2 = { + expr = lib.hex.fromDec 4500; + expected = "1194"; + }; + + testToHexString3 = { + expr = lib.hex.fromDec 5942819; + expected = "5AAE23"; + }; + + testHexToDec = { + expr = lib.hex.toDec "FF"; + expected = 255; + }; + + testHexToDec2 = { + expr = lib.hex.toDec "333FAB333"; + expected = 13756969779; + }; + testCreateHexRange = { expr = lib.hex.range 10 17; expected = [ "A" "B" "C" "D" "E" "F" "10" "11" ];