Skip to content

Commit e5dc683

Browse files
committed
fixes for new release
1 parent 6b50ca3 commit e5dc683

22 files changed

+435
-230
lines changed

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Significant and compatibility-breaking changes.
2+
3+
Version 0.3:
4+
- Removed 'flatStrict' and 'unflatStrict' (use 'flat' and 'unflat' instead that also encode/decode strictly)
5+
- `unflatWith` now takes a decoder for the unpadded value (previously it expected a padded decoder) and decodes the padded value
6+
- Added some decoding primitives
7+
- Added Data.ByteString.Convert

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ data Direction = North | South | Center | East | West deriving (Show,Generic,Fla
3131
data List a = Nil | Cons a (List a) deriving (Show,Generic,Flat)
3232
```
3333

34-
For encoding, use `flat`, for decoding, use `unflat` (or equivalently: `flatStrict` and `unflatStrict`):
34+
For encoding, use `flat`, for decoding, use `unflat`:
3535

3636
```haskell
37-
unflatStrict . flat $ Cons North (Cons South Nil) :: Decoded (List Direction)
37+
unflat . flat $ Cons North (Cons South Nil) :: Decoded (List Direction)
3838
-> Right (Cons North (Cons South Nil))
3939
```
4040

@@ -119,9 +119,9 @@ Briefly:
119119
* Encoding: `store` and `flat` are usually faster
120120
* Decoding: `store`, `cereal` and `flat` are usually faster
121121

122-
One thing that is not shown by the benchmarks is that, if the serialized data is to be transferred over a network, the total total transfer time (encoding time + transmission time + decoding time) is usually dominated by the transmission time and that's where the smaller binaries produced by flat give it a significant advantage.
122+
One thing that is not shown by the benchmarks is that, if the serialized data is to be transferred over a network, the total transfer time (encoding time + transmission time + decoding time) is usually dominated by the transmission time and that's where the smaller binaries produced by flat give it a significant advantage.
123123

124-
Consider for example the Cars dataset. As you can see in the following comparison with `store`, the overall top performer for encoding/decoding speed, the total transfer time is actually significantly lower for `flat` for all except the highest transmission speeds.
124+
Consider for example the Cars dataset. As you can see in the following comparison with `store`, the overall top performer for encoding/decoding speed, the transfer time is actually significantly lower for `flat` for all except the highest transmission speeds.
125125

126126
||Store|Flat|
127127
|---|---|---|

flat.cabal

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
name: flat
2-
version: 0.2.2
2+
version: 0.2.3
33
synopsis: Principled and efficient bit-oriented binary serialization.
44
description: See the <http://github.com/tittoassini/flat online tutorial>.
55
homepage: http://github.com/tittoassini/flat
6-
copyright: Copyright: (c) 2016 Pasqualino `Titto` Assini
7-
author: Pasqualino `Titto` Assini
8-
license: BSD3
9-
license-file: LICENSE
10-
maintainer: tittoassini@gmail.com
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Pasqualino `Titto` Assini
9+
maintainer: tittoassini@gmail.com
10+
copyright: Copyright: (c) 2016 Pasqualino `Titto` Assini
1111
category: Data,Parsing,Serialization
1212
cabal-version: >=1.10
1313
build-type: Simple
14-
tested-with: GHC ==7.10.3 GHC ==8.0.2
14+
Tested-With: GHC == 7.10.3 GHC == 8.0.1 GHC == 8.0.2
1515
extra-source-files:
1616
stack.yaml
1717
stack801.yaml
1818
stack802.yaml
1919
README.md
20+
CHANGELOG
2021

2122
source-repository head
2223
type: git
2324
location: https://github.com/tittoassini/flat
2425

2526
library
2627
exposed-modules:
28+
Data.ByteString.Convert
2729
Data.Flat.Bits
2830
Data.Flat.Class
2931
Data.Flat.Decoder

other/Pretty.hs

Lines changed: 0 additions & 24 deletions
This file was deleted.

other/Test.hs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
{-# LANGUAGE FlexibleInstances ,CPP #-}
88
{-# LANGUAGE LambdaCase ,ScopedTypeVariables #-}
99
module Test where
10-
import Data.Binary.FloatCast
1110
import qualified Data.ByteString as B
1211
import qualified Data.ByteString.Lazy as L
1312
import qualified Data.ByteString.Short.Internal as SBS
1413
import Data.Coerce
1514
import Data.Flat
16-
import Data.Flat.Peek
1715
import Data.Flat.Decoder
18-
import Data.Flat.Pretty
1916
import Data.Int
2017
import qualified Data.Sequence as S
2118
import qualified Data.Text as T
22-
import Data.Word
2319
import Prelude hiding (exponent, sign)
2420
import System.Endian
2521
import Text.Printf
2622
import Data.Flat.Bits
2723
import qualified Data.Map as M
24+
import Data.FloatCast
25+
-- import qualified Data.ByteString as B
26+
-- import qualified Data.ByteString.Lazy as L
27+
import Data.Word
28+
import Text.PrettyPrint.HughesPJClass
29+
-- import Text.Printf
2830

2931
instance Flat [Word16]
3032
instance Flat [Int16]
@@ -33,8 +35,11 @@ instance Flat [Word8]
3335
instance Flat [(Word64,Word16)]
3436
instance Flat [ABC]
3537

38+
--u :: B.ByteString
39+
u = unflatWith (dBEBits8 3) [128+64+32+1::Word8]
40+
3641
m1 = M.fromList [(False,True)]
37-
mmm = (size m1 0,bits m1, unflat $ flat m1 :: Decoded (M.Map Bool Bool))
42+
mmm = (size m1 0,bits m1, unflatRaw $ flat m1 :: Decoded (M.Map Bool Bool))
3843

3944
-- deriving instance {-# OVERLAPPABLE #-} Flat a => Flat [a]
4045
-- instance Flat a => Flat [a]
@@ -73,7 +78,7 @@ newtype A = A Bool
7378
newtype B = B Bool
7479
data C = C Bool
7580

76-
kkk = [[127,1],[128,1,1],[129,1,1],[255,127,1],[128,128,1,1],[129,128,1,1],[255,255,1,1],[128,128,2,1],[129,128,2,1],[255,255,127,1],[128,128,128,1,1],[129,128,128,1,1]] == map (L.unpack . flat) [127::Word32,128,129,16383,16384,16385,32767,32768,32769,2097151,2097152,2097153]
81+
kkk = [[127,1],[128,1,1],[129,1,1],[255,127,1],[128,128,1,1],[129,128,1,1],[255,255,1,1],[128,128,2,1],[129,128,2,1],[255,255,127,1],[128,128,128,1,1],[129,128,128,1,1]] == map (B.unpack . flat) [127::Word32,128,129,16383,16384,16385,32767,32768,32769,2097151,2097152,2097153]
7782

7883
-- qqq = size N4 0 + size N5 0
7984

@@ -145,7 +150,7 @@ n1 = Node (Node (Leaf 11) (Leaf 22)) (Node (Leaf 33) (Leaf 44))
145150
-- [240,88,90,33,22,96,176,180,66,44,224,176,180,66,44,193,97,104,132,89]
146151
n2 = Node (Node n1 n1) (Node (Node n1 n1) n1)
147152

148-
u = pp $ WW 11 22 33 44 True
153+
u4 = pp $ WW 11 22 33 44 True
149154
uu = pp (11::Word32,22::Word64,33::Word64)
150155
--o = encodings $ postAligned $ T2 (11::Word32) (22::Word64)
151156
ww = unflat (flat (0::Word16)) :: Decoded Word16
@@ -201,7 +206,7 @@ y11 =show __GLASGOW_HASKELL__
201206
pp :: forall a . (Flat a, Show a) => a -> IO ()
202207
-- pp v = putStrLn (unwords [show v,"->",show (size :: Size a),show $ getSize (postAligned v),show $ encode v,"->",show $ L.unpack $ flat v])
203208
-- pp v = putStrLn (unwords [show v,"->",show (size :: Size a),show $ getSize v,show $ encode v,"->",show $ L.unpack $ flat v])
204-
pp v = putStrLn (unwords [show v,"->",show $ getSize v,show $ encode v,"->",show $ L.unpack $ flat v])
209+
pp v = putStrLn (unwords [show v,"->",show $ getSize v,show $ encode v,"->",show $ flat v])
205210

206211
-- gg :: Flat a => a -> Vector Bool
207212
gg = bits . flat $ "abc"
@@ -213,7 +218,9 @@ bs = [32,32,32::Word8]
213218

214219
-- px v = putStrLn (unwords [show v,"->",show $ encode v,"->",show $ L.unpack $ flatRaw (T2 v FillerEnd)])
215220

216-
px v = let bs = L.unpack $ flat v in putStrLn (unwords [show v,"->",show $ encode v,"->",show bs,show $ sum bs])
221+
-- px :: Flat a => a -> IO ()
222+
px v = let bs = B.unpack $ flat v
223+
in putStrLn (unwords [show v,"->",show $ encode v,"->",show bs,show $ sum bs])
217224

218225
pf v = putStrLn (unwords [show v,"->",show $ flat v])
219226

@@ -225,61 +232,61 @@ ll = flat [True,False,True]
225232

226233
f3 = doubleToWord 1
227234

228-
f2 =flat (1::Double)
235+
f2 =flat(1::Double)
229236

230-
kk = flat [(55::Word64,18::Word16),(5599::Word64,1122::Word16)]
237+
kk = flat[(55::Word64,18::Word16),(5599::Word64,1122::Word16)]
231238
-- tt = encode [(55::Word64,18::Word16,True,AA)]
232239
e1 = encode (True,False) -- (AA,BB)
233240
e2 = encode [AA,BB]
234241
e3 = encode [True,False]
235242
e4 = encode $ DD True True
236243
-- e5 = encode $ ZZ (ZZ (ZZ (DD True True)))
237244

238-
m = runGetStrict getChunksInfo $ B.pack [3,11,22,33,2,11,22,0]
245+
-- m = runGetStrict getChunksInfo $ B.pack [3,11,22,33,2,11,22,0]
239246

240-
m2 = B.unpack <$> runGetStrict dByteString_ (B.pack [3,11,22,33,2,11,22,0])
241-
m3 = SBS.unpack <$> runGetStrict dShortByteString_ (B.pack [3,11,22,33,2,11,22,0])
242-
m4 = unflat (flat (T.pack "abc")) :: Decoded T.Text
247+
-- m2 = B.unpack <$> runGetStrict dByteString_ (B.pack [3,11,22,33,2,11,22,0])
248+
-- m3 = SBS.unpack <$> runGetStrict dShortByteString_ (B.pack [3,11,22,33,2,11,22,0])
249+
-- m4 = unflat (flat(T.pack "abc")) :: Decoded T.Text
243250
m5 = unflat (flat '经') :: Decoded Char
244251

245252
{-# NOINLINE tup2 #-}
246-
tup2 a b = flat (a,b)
253+
tup2 a b = flat(a,b)
247254
--t = coerce (1::Word32) :: Float
248-
-- b = L.unpack $ flat $ B.pack $ replicate 250 33
249-
b = L.unpack $ flat $ (11::Word8,SBS.toShort $ B.replicate 400 33) -- [1..255] B.pack bs
255+
-- b = L.unpack $ flat$ B.pack $ replicate 250 33
256+
-- b = L.unpack $ flat$ (11::Word8,SBS.toShort $ B.replicate 400 33) -- [1..255] B.pack bs
250257

251-
w = L.unpack $ flat (2::Word16,2::Word32,2::Word64)
258+
-- w = L.unpack $ flat(2::Word16,2::Word32,2::Word64)
252259

253-
l = L.unpack $ flat [1::Word16,1,1]
260+
-- l = L.unpack $ flat[1::Word16,1,1]
254261

255-
j = L.unpack $ flat "abc"
262+
-- j = L.unpack $ flat "abc"
256263

257-
js = L.unpack $ flat (S.fromList "abc")
264+
-- js = L.unpack $ flat(S.fromList "abc")
258265

259-
jj = L.unpack $ flat $ (11::Word8,T.pack "\x1F600\&000aaa维维aaa")
266+
-- jj = L.unpack $ flat$ (11::Word8,T.pack "\x1F600\&000aaa维维aaa")
260267

261-
jl = T.length (T.pack "\x1F600\&\x1F600\&")
268+
-- jl = T.length (T.pack "\x1F600\&\x1F600\&")
262269

263270
q :: Decoded T.Text
264-
q = unflat $ flat (T.pack "D\226\FStz\GS3]\n8\149sV\243J\181\181\235\214&y\226\231\&2\239\212\174\DC1J'F\129hpsu\199\178")
271+
q = unflat $ flat(T.pack "D\226\FStz\GS3]\n8\149sV\243J\181\181\235\214&y\226\231\&2\239\212\174\DC1J'F\129hpsu\199\178")
265272

266273
g = let v = (-1::Int16,255::Word8,False,-1::Int16,1::Word8,0::Word8,False)
267274
in (unflat (flat v) == Right v,bits v)
268275

269276
g1 :: Decoded (Bool,Bool,Bool,Bool)
270-
g1 = unflat $ flat (True,False,True,True)
277+
g1 = unflat $ flat(True,False,True,True)
271278

272279
g2 :: Decoded (Bool,Word8,Word8,Bool)
273-
g2 = unflat $ flat (False,1::Word8,1::Word8,False)
280+
g2 = unflat $ flat(False,1::Word8,1::Word8,False)
274281

275282
g3 :: Decoded Word8
276-
g3 = unflat $ flat (0::Word8)
283+
g3 = unflat $ flat(0::Word8)
277284

278285
g4 :: Decoded (Bool,Word8,Bool)
279-
g4 = unflat $ flat (False,0::Word8,False)
286+
g4 = unflat $ flat(False,0::Word8,False)
280287

281288
g5 :: Decoded (Float,Double,Bool,Float,Double,Bool)
282-
g5 = unflat $ flat (8.11E-11::Float,8.11E-11::Double,True,8.11E-11::Float,8.11E-11::Double,True)
289+
g5 = unflat $ flat(8.11E-11::Float,8.11E-11::Double,True,8.11E-11::Float,8.11E-11::Double,True)
283290

284291
f0 = bits ((False,255::Word8,False,255::Word8))
285292
f = bits (False,0::Word8,False)
@@ -296,20 +303,31 @@ d = prettyLBS $ flatRaw (3/0::Double)
296303
s = serRaw (True,False,True)
297304

298305
z = bits $ (True,False,True)
299-
zz = L.unpack . flat $ (True,False,True)
306+
zz = flat (True,False,True)
300307

301308
serRaw :: Flat a => a -> [Word8]
302309
--serRaw = L.unpack . flatRaw
303310
serRaw = asBytes . bits
304311

305312
y :: Decoded Float
306-
y = unflat $ flat (-0.15625::Double)
313+
y = unflat $ flat(-0.15625::Double)
307314

308315
-- i :: Decoded IEEE_754_binary32
309-
-- i = unflat $ flat (-0.15625::Float)
316+
-- i = unflat $ flat(-0.15625::Float)
310317

311318
--t = i == Right (IEEE_754_binary32 {sign = V1, exponent = Bits8 V0 V1 V1 V1 V1 V1 V0 V0, fraction = (Bits7 V0 V1 V0 V0 V0 V0 V0,Bits8 V0 V0 V0 V0 V0 V0 V0 V0,Bits8 V0 V0 V0 V0 V0 V0 V0 V0)})
312319
bb = 0b111
313320

314321
c = (isInfinite (3/0),isNegativeZero (-0::Double),isNaN (0/0::Float),(0/0::Float)==(0/0::Float))
315322

323+
prettyLBS :: L.ByteString -> String
324+
prettyLBS = render . prettyBL . L.unpack
325+
326+
prettyBS :: B.ByteString -> String
327+
prettyBS = render . prettyBL . B.unpack
328+
329+
prettyBL :: [Word8] -> Doc
330+
prettyBL = text . unwords . map prettyWord8
331+
332+
prettyWord8 :: Word8 -> String
333+
prettyWord8 = printf "%08b"

src/Data/ByteString/Convert.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
-- |Convert to/from strict ByteStrings
3+
module Data.ByteString.Convert (AsByteString(..)) where
4+
5+
import qualified Data.ByteString as B
6+
import qualified Data.ByteString.Lazy as L
7+
import Data.Word
8+
9+
class AsByteString a where
10+
toByteString :: a -> B.ByteString
11+
fromByteString :: B.ByteString -> a
12+
13+
instance AsByteString B.ByteString where
14+
toByteString = id
15+
fromByteString = id
16+
17+
instance AsByteString L.ByteString where
18+
toByteString = L.toStrict
19+
fromByteString = L.fromStrict
20+
21+
instance AsByteString [Word8] where
22+
toByteString = B.pack
23+
fromByteString = B.unpack
24+

src/Data/Flat/Bits.hs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
{-# LANGUAGE TypeSynonymInstances #-}
55

66
-- |Utilities to represent and display bit sequences
7-
module Data.Flat.Bits (Bits, toBools, bits, paddedBits, asBytes) where
7+
module Data.Flat.Bits (
8+
Bits,
9+
toBools,
10+
fromBools,
11+
bits,
12+
paddedBits,
13+
asBytes,
14+
asBits,
15+
) where
816

917
import Data.Bits hiding (Bits)
10-
import qualified Data.ByteString as L
11-
import Data.Flat.Decoder
18+
import qualified Data.ByteString as B
1219
import Data.Flat.Class
20+
import Data.Flat.Decoder
1321
import Data.Flat.Filler
1422
import Data.Flat.Run
15-
-- import Data.Int
1623
import qualified Data.Vector.Unboxed as V
1724
import Data.Word
1825
import Text.PrettyPrint.HughesPJClass
@@ -23,19 +30,27 @@ type Bits = V.Vector Bool
2330
toBools :: Bits -> [Bool]
2431
toBools = V.toList
2532

33+
fromBools :: [Bool] -> Bits
34+
fromBools = V.fromList
35+
2636
-- |The sequence of bits corresponding to the serialization of the passed value (without any final byte padding)
2737
bits :: forall a. Flat a => a -> Bits
2838
bits v = let lbs = flat v
2939
Right (PostAligned _ f) = unflatRaw lbs :: Decoded (PostAligned a)
30-
in takeBits (8 * L.length lbs - fillerLength f) lbs
40+
in takeBits (8 * B.length lbs - fillerLength f) lbs
3141

3242
-- |The sequence of bits corresponding to the byte-padded serialization of the passed value
3343
paddedBits :: forall a. Flat a => a -> Bits
3444
paddedBits v = let lbs = flat v
35-
in takeBits (8 * L.length lbs) lbs
45+
in takeBits (8 * B.length lbs) lbs
46+
47+
takeBits :: Int -> B.ByteString -> Bits
48+
takeBits numBits lbs = V.generate (fromIntegral numBits) (\n -> let (bb,b) = n `divMod` 8 in testBit (B.index lbs (fromIntegral bb)) (7-b))
3649

37-
takeBits :: Int -> L.ByteString -> V.Vector Bool
38-
takeBits numBits lbs = V.generate (fromIntegral numBits) (\n -> let (bb,b) = n `divMod` 8 in testBit (L.index lbs (fromIntegral bb)) (7-b))
50+
-- | asBits (5::Word8)
51+
-- | > [False,False,False,False,False,True,False,True]
52+
asBits :: FiniteBits a => a -> Bits
53+
asBits w = let s = finiteBitSize w in V.generate s (testBit w . (s-1-))
3954

4055
-- |Convert a sequence of bits to the corresponding list of bytes
4156
asBytes :: Bits -> [Word8]

src/Data/Flat/Decoder.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ module Data.Flat.Decoder (
2929
dInt32,
3030
dInt64,
3131
dInt,
32+
dBE8,
33+
dBE16,
34+
dBE32,
35+
dBE64,
36+
dBEBits8,
37+
dBEBits16,
38+
dBEBits32,
39+
dBEBits64,
40+
dropBits,
3241
) where
3342

3443
import Data.Flat.Decoder.Prim

0 commit comments

Comments
 (0)