@@ -24,6 +24,7 @@ import Control.Monad.Trans.Class (lift)
24
24
import Data.Array (snoc , foldRecM )
25
25
import Data.Array as Array
26
26
import Data.ArrayBuffer.Builder (DataBuff (..), PutM , subBuilder )
27
+ import Data.ArrayBuffer.DataView (byteLength )
27
28
import Data.ArrayBuffer.Types (DataView , ByteLength )
28
29
import Data.Enum (class BoundedEnum , fromEnum , toEnum )
29
30
import Data.Foldable (foldl )
@@ -39,7 +40,8 @@ import Data.UInt64 (UInt64)
39
40
import Data.UInt64 as UInt64
40
41
import Effect.Class (class MonadEffect )
41
42
import Parsing (ParserT , Position (..), fail , position )
42
- import Parsing.DataView (takeN )
43
+ import Parsing.Combinators (lookAhead )
44
+ import Parsing.DataView (takeN , takeRest )
43
45
import Protobuf.Internal.Common (Bytes (..), FieldNumber , WireType (..), label )
44
46
import Protobuf.Internal.Decode as Decode
45
47
import Protobuf.Internal.Encode as Encode
@@ -76,6 +78,7 @@ type FieldNumberInt
76
78
= Int
77
79
78
80
-- | Call a parser repeatedly until exactly *N* bytes have been consumed.
81
+ -- | Will fail if not enough bytes remain in the DataView.
79
82
-- | Will fail if too many bytes are consumed.
80
83
manyLength ::
81
84
forall m a .
@@ -85,19 +88,23 @@ manyLength ::
85
88
ByteLength ->
86
89
ParserT DataView m (Array a )
87
90
manyLength p len = do
88
- Position { index: posBegin } <- position
89
- let
90
- go :: List a -> ParserT DataView m (Step (List a ) (List a ))
91
- go accum = do
92
- Position { index: pos } <- position
93
- case compare (pos - posBegin) len of
94
- GT -> fail " manyLength consumed too many bytes."
95
- EQ -> lift $ pure (Done accum)
96
- LT -> do
97
- x <- p
98
- pure (Loop (x : accum))
99
- -- https://github.com/purescript-contrib/purescript-parsing/pull/199#issuecomment-1145956271
100
- Array .reverse <$> Array .fromFoldable <$> tailRecM go List.Nil
91
+ remaining_bytes :: Int <- byteLength <$> lookAhead takeRest
92
+ if remaining_bytes < len
93
+ then fail $ " manyLength " <> show len <> " not enough bytes of input " <> show remaining_bytes <> " remaining."
94
+ else do
95
+ Position { index: posBegin } <- position
96
+ let
97
+ go :: List a -> ParserT DataView m (Step (List a ) (List a ))
98
+ go accum = do
99
+ Position { index: pos } <- position
100
+ case compare (pos - posBegin) len of
101
+ GT -> fail " manyLength consumed too many bytes."
102
+ EQ -> lift $ pure (Done accum)
103
+ LT -> do
104
+ x <- p
105
+ pure (Loop (x : accum))
106
+ -- https://github.com/purescript-contrib/purescript-parsing/pull/199#issuecomment-1145956271
107
+ Array .reverse <$> Array .fromFoldable <$> tailRecM go List.Nil
101
108
102
109
-- | A message field value from an unknown `.proto` definition.
103
110
-- |
0 commit comments