fix: pad start with zeros

This commit is contained in:
orion 2023-12-16 13:25:42 -06:00
parent af76dc3b27
commit c479776340
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -23,9 +23,8 @@ import Data.Show.Generic (genericShow)
import Data.String as String
import Data.Traversable (for, traverse)
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Effect.Unsafe (unsafePerformEffect)
import Node.Buffer (Buffer, BufferValueType(..))
import Node.Buffer (BufferValueType(..))
import Node.Buffer as Buffer
import Node.Buffer.Immutable (ImmutableBuffer)
import Node.Buffer.Immutable as Buffer.Immutable
@ -181,6 +180,9 @@ derive instance Eq Segment
instance Show Segment where
show = genericShow
segmentZero :: Segment
segmentZero = Segment ConB VoA ConB VoA ConB
segmentBits :: Segment -> Int
segmentBits (Segment a b c d e) =
let
@ -231,6 +233,16 @@ derive instance Eq Proquint
instance Show Proquint where
show = genericShow
pad :: Int -> Proquint -> Proquint
pad n pq@(Proquint segs) =
let
len = Array.length segs
in
if len < n then
Proquint $ Array.replicate (n - len) segmentZero <> segs
else
pq
toString :: Proquint -> String
toString (Proquint segments) = intercalate "-" $ map segmentToString segments
@ -246,12 +258,12 @@ fromInt n = fromBits $ unsafePerformEffect do
toInt :: Proquint -> Maybe Int
toInt pq@(Proquint segs)
| Array.length segs <= 2 = Int.fromNumber $ Buffer.Immutable.read UInt32BE 0 $ toBits pq
| Array.length segs <= 2 = Int.fromNumber $ Buffer.Immutable.read UInt32BE 0 $ toBits $ pad 2 pq
| otherwise = Nothing
toBigInt :: Proquint -> Maybe BigInt
toBigInt pq@(Proquint segs)
| Array.length segs <= 4 = Just $ Buffer.Immutable.BigInt.readBigUInt64BE 0 $ toBits pq
| Array.length segs <= 4 = Just $ Buffer.Immutable.BigInt.readBigUInt64BE 0 $ toBits $ pad 4 pq
| otherwise = Nothing
fromBigInt :: BigInt -> Proquint