urbit-hob

Haskell utilities for phonetic base wrangling.
Log | Files | Refs | README | LICENSE

commit 7cc5346f3a5d9737af6acbef041b90aa34c81bb4
parent 7cd97c9b8a70f35d756ff15f9c428c206a900c68
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri, 28 Feb 2020 18:35:46 +0400

serialize: handle 0 encoding correctly

'unroll' returned the empty bytestring on an input of 0, leading 'patp
0' to encode ~zod as the empty bytestring.  On the other hand,
'parsePatp "~zod"' would correctly encode ~zod as \NUL.

This led to an equality comparison bug in some circumstances, depending
on how Patp values of ~zod were gotten.

Fixes #3.

Diffstat:
Mlib/Data/Serialize/Extended.hs | 9++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/Data/Serialize/Extended.hs b/lib/Data/Serialize/Extended.hs @@ -11,9 +11,12 @@ import Numeric.Natural (Natural) -- | Simple little-endian ByteString encoding for Naturals. unroll :: Natural -> BS.ByteString -unroll = BS.pack . unfoldr step where - step 0 = Nothing - step i = Just (fromIntegral i, i `shiftR` 8) +unroll nat = case nat of + 0 -> BS.singleton 0 + _ -> BS.pack (unfoldr step nat) + where + step 0 = Nothing + step i = Just (fromIntegral i, i `shiftR` 8) -- | Simple little-endian ByteString decoding for Naturals. roll :: BS.ByteString -> Natural