Skip to content

Commit 9a0b746

Browse files
phadejdcoutts
authored andcommitted
Resolves #355: Support grabbing the bytes of intervals
1 parent b5ae5cf commit 9a0b746

9 files changed

Lines changed: 541 additions & 53 deletions

File tree

cborg/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history for cborg
22

3+
## 0.3.0.0
4+
5+
* Add support for grabbing the input bytes of intervals, i.e. `getInputSpan`.
6+
37
## 0.2.10.0
48

59
* Return `TypeUInt64` and `TypeNInt64` from `tokenType` when appropriate, fixing [#324](https://github.com/well-typed/cborg/issues/324)

cborg/cborg.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ test-suite cborg-tests
129129
Tests.Boundary
130130
Tests.ATerm
131131
Tests.ByteOffset
132+
Tests.GetInputSpan
132133
Tests.Canonical
133134
Tests.PreEncoded
134135
Tests.Regress

cborg/src/Codec/CBOR/Decoding.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ module Codec.CBOR.Decoding
7676
, peekByteOffset -- :: Decoder s ByteOffset
7777
, decodeWithByteSpan
7878

79+
, markInput -- :: Decoder s ()
80+
, unmarkInput -- :: Decoder s ()
81+
, getInputSpan -- :: Decoder s LazyByteString
82+
7983
-- ** Canonical CBOR
8084
-- $canonical
8185
, decodeWordCanonical -- :: Decoder s Word
@@ -118,6 +122,7 @@ import GHC.Word
118122
import GHC.Int
119123
import Data.Text (Text)
120124
import Data.ByteString (ByteString)
125+
import qualified Data.ByteString.Lazy as LBS
121126
import Control.Applicative
122127
import Control.Monad.ST
123128
import qualified Control.Monad.Fail as Fail
@@ -193,6 +198,9 @@ data DecodeAction s a
193198
#else
194199
| PeekByteOffset (Int# -> ST s (DecodeAction s a))
195200
#endif
201+
| MarkInput (ST s (DecodeAction s a))
202+
| UnmarkInput (ST s (DecodeAction s a))
203+
| GetInputSpan (LBS.ByteString -> ST s (DecodeAction s a))
196204

197205
-- All the canonical variants
198206
| ConsumeWordCanonical (Word# -> ST s (DecodeAction s a))
@@ -990,6 +998,25 @@ peekByteOffset = Decoder (\k -> return (PeekByteOffset (\off# -> k (I64#
990998
))))
991999
{-# INLINE peekByteOffset #-}
9921000

1001+
-- |
1002+
--
1003+
-- @since 0.3.0.0
1004+
markInput :: Decoder s ()
1005+
markInput = Decoder (\k -> return (MarkInput (k ())))
1006+
1007+
-- |
1008+
--
1009+
-- @since 0.3.0.0
1010+
unmarkInput :: Decoder s ()
1011+
unmarkInput = Decoder (\k -> return (UnmarkInput (k ())))
1012+
1013+
-- |
1014+
--
1015+
-- @since 0.3.0.0
1016+
getInputSpan :: Decoder s LBS.ByteString
1017+
getInputSpan = Decoder (\k -> return (GetInputSpan k))
1018+
1019+
9931020
-- | This captures the pattern of getting the byte offsets before and after
9941021
-- decoding a subterm.
9951022
--

cborg/src/Codec/CBOR/FlatTerm.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import Data.Text (Text)
6767
import qualified Data.Text.Encoding as TE
6868
import Data.ByteString (ByteString)
6969
import qualified Data.ByteString as BS
70+
import qualified Data.ByteString.Lazy as LBS
7071
import Control.Monad.ST
7172
import qualified Control.Monad.ST.Lazy as ST.Lazy
7273

@@ -473,6 +474,9 @@ fromFlatTerm decoder ft =
473474
#else
474475
go ts (PeekByteOffset k)= k 0# >>= go ts
475476
#endif
477+
go ts (MarkInput k) = k >>= go ts
478+
go ts (UnmarkInput k) = k >>= go ts
479+
go ts (GetInputSpan k) = k LBS.empty >>= go ts
476480

477481
go _ (Fail msg) = return $ Left msg
478482
go [] (Done x) = return $ Right x

cborg/src/Codec/CBOR/Magic.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module Codec.CBOR.Magic
5555

5656
, intToWord -- :: Int -> Word
5757
, intToInt64 -- :: Int -> Int64
58+
, int64ToInt
5859

5960
, intToWord64 -- :: Int -> Word64
6061
, int64ToWord64 -- :: Int64 -> Word64
@@ -391,6 +392,10 @@ intToInt64 :: Int -> Int64
391392
intToInt64 = fromIntegral
392393
{-# INLINE intToInt64 #-}
393394

395+
int64ToInt :: Int64 -> Int
396+
int64ToInt = fromIntegral
397+
{-# INLINE int64ToInt #-}
398+
394399
intToWord :: Int -> Word
395400
intToWord = fromIntegral
396401
{-# INLINE intToWord #-}

0 commit comments

Comments
 (0)