commit 56697a761114f4bb7209af45189cfb70b5bbd957
parent 194569f64ff8485e0a2e537079a82b9a52c74bd1
Author: Jared Tobin <jared@jtobin.io>
Date: Fri, 3 Jan 2025 19:54:46 -0330
lib: minor tweaks
Diffstat:
3 files changed, 16 insertions(+), 21 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -39,15 +39,11 @@ base32_decode = bgroup "base32 decode" [
bech32_encode :: Benchmark
bech32_encode = bgroup "bech32 encode" [
bench "120b" $ nf (Bech32.encode "bc") "jtobin was here"
- , bench "128b (non 40-bit multiple length)" $
- nf (Bech32.encode "bc") "jtobin was here!"
]
bech32_decode :: Benchmark
bech32_decode = bgroup "bech32 decode" [
bench "120b" $ nf Bech32.decode "bc1df6x7cnfdcs8wctnyp5x2un9f0pw8y"
- , bench "128b (non 40-bit multiple length)" $
- nf Bech32.decode "bc1df6x7cnfdcs8wctnyp5x2un9yyg90e5y"
]
suite :: Benchmark
diff --git a/lib/Data/ByteString/Base32.hs b/lib/Data/ByteString/Base32.hs
@@ -226,8 +226,8 @@ finalize bs@(BI.PS _ _ l)
.|. w5_2 `B.shiftL` 1
.|. w5_3 `B.shiftR` 4
- w16 = fi w8_1
- .|. fi w8_0 `B.shiftL` 8
+ !w16 = fi w8_1
+ .|. fi w8_0 `B.shiftL` 8
guard (w5_3 `B.shiftL` 4 == 0)
pure (BSB.word16BE w16)
@@ -286,16 +286,16 @@ decode_chunk bs = do
w5_7 <- word5 (BU.unsafeIndex bs 7)
let w40 :: Word64
- w40 = fi w5_0 `B.shiftL` 35
- .|. fi w5_1 `B.shiftL` 30
- .|. fi w5_2 `B.shiftL` 25
- .|. fi w5_3 `B.shiftL` 20
- .|. fi w5_4 `B.shiftL` 15
- .|. fi w5_5 `B.shiftL` 10
- .|. fi w5_6 `B.shiftL` 05
- .|. fi w5_7
- w32 = fi (w40 `B.shiftR` 8) :: Word32
- w8 = fi (0b11111111 .&. w40) :: Word8
+ !w40 = fi w5_0 `B.shiftL` 35
+ .|. fi w5_1 `B.shiftL` 30
+ .|. fi w5_2 `B.shiftL` 25
+ .|. fi w5_3 `B.shiftL` 20
+ .|. fi w5_4 `B.shiftL` 15
+ .|. fi w5_5 `B.shiftL` 10
+ .|. fi w5_6 `B.shiftL` 05
+ .|. fi w5_7
+ !w32 = fi (w40 `B.shiftR` 8) :: Word32
+ !w8 = fi (0b11111111 .&. w40) :: Word8
pure $ BSB.word32BE w32 <> BSB.word8 w8
diff --git a/lib/Data/ByteString/Bech32/Internal.hs b/lib/Data/ByteString/Bech32/Internal.hs
@@ -18,6 +18,7 @@ import qualified Data.Bits as B
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Builder.Extra as BE
+import qualified Data.ByteString.Internal as BI
import qualified Data.ByteString.Unsafe as BU
import qualified Data.Primitive.PrimArray as PA
import Data.Word (Word32)
@@ -68,11 +69,9 @@ polymod = BS.foldl' alg 1 where
in loop_gen (succ i) b (chk `B.xor` sor)
valid_hrp :: BS.ByteString -> Bool
-valid_hrp hrp
- | l == 0 || l > 83 = False
- | otherwise = BS.all (\b -> (b > 32) && (b < 127)) hrp
- where
- l = BS.length hrp
+valid_hrp hrp@(BI.PS _ _ l)
+ | l == 0 || l > 83 = False
+ | otherwise = BS.all (\b -> (b > 32) && (b < 127)) hrp
hrp_expand :: BS.ByteString -> BS.ByteString
hrp_expand bs = toStrict