@@ -2,25 +2,31 @@
22module Protobuf.Decode64
33( zigzag64
44, varint64
5−)
5+) where
66
77import Prelude
8−import Effect (Effect, liftEffect)
8+import Effect (Effect)
9+import Effect.Class (liftEffect)
910import Text.Parsing.Parser (ParserT, fail)
1011import Text.Parsing.Parser.DataView as Parse
11−import Data.Long.Unsigned (Long, Unsigned, Signed, toUnsigned, fromInt)
1212import Data.Long.Bits((.^.), (.&.), (.|.), complement)
13−import Data.Long as Long (fromInt)
13+import Data.Long.Internal (Long, Unsigned, Signed, signedToUnsigned, unsafeFromInt)
14+import Data.UInt (UInt)
15+import Data.UInt as UInt
1416import Data.ArrayBuffer.Types (DataView)
1517import Protobuf.Common (FieldNumber, WireType)
1618
19+
20+fromInt :: UInt -> Long Unsigned
21+fromInt x = unsafeFromInt <<< UInt.toInt
22+
1723-- | https://stackoverflow.com/questions/2210923/zig-zag-decoding
1824zigzag64 :: Long Signed -> Long Unsigned
19−zigzag64 n = toUnsigned $ (n `shr` 1) .^. (lnegate (n .&. 1))
20− where lnegate x = complement x + (Long.fromInt 1)
25+zigzag64 n = signedToUnsigned $ (n `shr` 1) .^. (lnegate (n .&. 1))
26+ where lnegate x = complement x + (unsafeFromInt 1)
2127
2228-- | https://developers.google.com/protocol-buffers/docs/encoding#varints
23−varint64 :: forall m. MonadEffect m => ParserT DataView m UInt
29+varint64 :: forall m. MonadEffect m => ParserT DataView m (Long Unsigned)
2430varint64 = do
2531 n_0 <- fromInt <$> Parse.anyUInt8
2632 if n_0 < u0x80
@@ -70,15 +76,15 @@ varint64 = do
7076 n_9 <- fromInt <$> Parse.anyUint8
7177 pure $ acc_8 .|. (n_9 `shl` u63)
7278 where
73− u7 = fromInt 7
74− u14 = fromInt 14
75− u21 = fromInt 21
76− u28 = fromInt 28
77− u35 = fromInt 35
78− u42 = fromInt 42
79− u49 = fromInt 49
80− u56 = fromInt 56
81− u63 = fromInt 63
82− u0x7F = fromInt 0x7F
83− u0x80 = fromInt 0x80
79+ u7 = unsafeFromInt 7
80+ u14 = unsafeFromInt 14
81+ u21 = unsafeFromInt 21
82+ u28 = unsafeFromInt 28
83+ u35 = unsafeFromInt 35
84+ u42 = unsafeFromInt 42
85+ u49 = unsafeFromInt 49
86+ u56 = unsafeFromInt 56
87+ u63 = unsafeFromInt 63
88+ u0x7F = unsafeFromInt 0x7F
89+ u0x80 = unsafeFromInt 0x80
8490