Skip to content

Commit 8d1e39a

Browse files
committed
Base32: Lift some helpers up for clarity
1 parent 91105c0 commit 8d1e39a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

hnix-store-core/src/System/Nix/Base32.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ encode c = T.pack $ concatMap char32 [nChar - 1, nChar - 2 .. 0]
1818
-- d58a11e019813902b6c4547ca61a127938b2cc20.
1919
nChar = fromIntegral $ ((BS.length c * 8 - 1) `div` 5) + 1
2020

21+
byte = BS.index c . fromIntegral
22+
23+
-- May need to switch to a more efficient calculation at some
24+
-- point.
25+
bAsInteger :: Integer
26+
bAsInteger = sum [fromIntegral (byte j) * (256 ^ j)
27+
| j <- [0 .. BS.length c - 1]
28+
]
29+
2130
char32 :: Integer -> [Char]
2231
char32 i = [digits32 V.! digitInd]
2332
where
24-
byte j = BS.index c (fromIntegral j)
25-
fromIntegral' :: Num b => Integer -> b
26-
fromIntegral' = fromIntegral
27-
digitInd = fromIntegral' $
28-
sum [fromIntegral (byte j) * (256^j)
29-
| j <- [0 .. BS.length c - 1]]
33+
digitInd = fromIntegral $
34+
bAsInteger
3035
`div` (32^i)
3136
`mod` 32

0 commit comments

Comments
 (0)