Add ascii7 and ascii8

This commit is contained in:
Andrew Martin 2020-10-26 12:30:55 -04:00
parent 04ab7f046b
commit 0c1b4da583
4 changed files with 45 additions and 3 deletions

View file

@ -5,9 +5,10 @@ Note: Prior to version 0.3.4.0, this library was named
`small-bytearray-builder` is now just a compatibility shim `small-bytearray-builder` is now just a compatibility shim
to ease the migration process. to ease the migration process.
## 0.3.6.1 -- 2020-??-?? ## 0.3.7.0 -- 2020-??-??
* Fix build error in test suite. * Fix build error in test suite.
* Add `ascii7` and `ascii8`.
## 0.3.6.0 -- 2020-06-30 ## 0.3.6.0 -- 2020-06-30

View file

@ -1,6 +1,6 @@
cabal-version: 2.2 cabal-version: 2.2
name: bytebuild name: bytebuild
version: 0.3.6.0 version: 0.3.7.0
synopsis: Serialize to a small byte arrays synopsis: Serialize to a small byte arrays
description: description:
This is similar to the builder facilities provided by This is similar to the builder facilities provided by

View file

@ -64,6 +64,8 @@ module Data.Bytes.Builder
, ascii4 , ascii4
, ascii5 , ascii5
, ascii6 , ascii6
, ascii7
, ascii8
, char , char
-- ** Machine-Readable -- ** Machine-Readable
-- *** One -- *** One
@ -860,11 +862,21 @@ ascii4 a b c d = fromBounded Nat.constant (Bounded.ascii4 a b c d)
ascii5 :: Char -> Char -> Char -> Char -> Char -> Builder ascii5 :: Char -> Char -> Char -> Char -> Char -> Builder
ascii5 a b c d e = fromBounded Nat.constant (Bounded.ascii5 a b c d e) ascii5 a b c d e = fromBounded Nat.constant (Bounded.ascii5 a b c d e)
-- | Encode five ASCII characters. -- | Encode six ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked. -- Precondition: Must be an ASCII characters. This is not checked.
ascii6 :: Char -> Char -> Char -> Char -> Char -> Char -> Builder ascii6 :: Char -> Char -> Char -> Char -> Char -> Char -> Builder
ascii6 a b c d e f = fromBounded Nat.constant (Bounded.ascii6 a b c d e f) ascii6 a b c d e f = fromBounded Nat.constant (Bounded.ascii6 a b c d e f)
-- | Encode seven ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
ascii7 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder
ascii7 a b c d e f g = fromBounded Nat.constant (Bounded.ascii7 a b c d e f g)
-- | Encode eight ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
ascii8 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder
ascii8 a b c d e f g h = fromBounded Nat.constant (Bounded.ascii8 a b c d e f g h)
-- | Encode a UTF-8 char. This only uses as much space as is required. -- | Encode a UTF-8 char. This only uses as much space as is required.
char :: Char -> Builder char :: Char -> Builder
char c = fromBounded Nat.constant (Bounded.char c) char c = fromBounded Nat.constant (Bounded.char c)

View file

@ -65,6 +65,8 @@ module Data.Bytes.Builder.Bounded
, ascii4 , ascii4
, ascii5 , ascii5
, ascii6 , ascii6
, ascii7
, ascii8
, char , char
-- ** Native -- ** Native
, wordPaddedDec2 , wordPaddedDec2
@ -795,6 +797,33 @@ ascii6 (C# c0) (C# c1) (C# c2) (C# c3) (C# c4) (C# c5) = Unsafe.construct $ \(Mu
primitive_ (writeCharArray# arr (off +# 5# ) c5) primitive_ (writeCharArray# arr (off +# 5# ) c5)
pure (I# (off +# 6# )) pure (I# (off +# 6# ))
-- | Encode seven ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii7 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder 7
ascii7 (C# c0) (C# c1) (C# c2) (C# c3) (C# c4) (C# c5) (C# c6) = Unsafe.construct $ \(MutableByteArray arr) (I# off) -> do
primitive_ (writeCharArray# arr off c0)
primitive_ (writeCharArray# arr (off +# 1# ) c1)
primitive_ (writeCharArray# arr (off +# 2# ) c2)
primitive_ (writeCharArray# arr (off +# 3# ) c3)
primitive_ (writeCharArray# arr (off +# 4# ) c4)
primitive_ (writeCharArray# arr (off +# 5# ) c5)
primitive_ (writeCharArray# arr (off +# 6# ) c6)
pure (I# (off +# 7# ))
-- | Encode eight ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii8 :: Char -> Char -> Char -> Char -> Char -> Char -> Char -> Char -> Builder 8
ascii8 (C# c0) (C# c1) (C# c2) (C# c3) (C# c4) (C# c5) (C# c6) (C# c7) = Unsafe.construct $ \(MutableByteArray arr) (I# off) -> do
primitive_ (writeCharArray# arr off c0)
primitive_ (writeCharArray# arr (off +# 1# ) c1)
primitive_ (writeCharArray# arr (off +# 2# ) c2)
primitive_ (writeCharArray# arr (off +# 3# ) c3)
primitive_ (writeCharArray# arr (off +# 4# ) c4)
primitive_ (writeCharArray# arr (off +# 5# ) c5)
primitive_ (writeCharArray# arr (off +# 6# ) c6)
primitive_ (writeCharArray# arr (off +# 7# ) c7)
pure (I# (off +# 8# ))
-- | Encode a machine-sized word with LEB-128. -- | Encode a machine-sized word with LEB-128.
wordLEB128 :: Word -> Builder 10 wordLEB128 :: Word -> Builder 10
wordLEB128 (W# w) = lebCommon (W# w) wordLEB128 (W# w) = lebCommon (W# w)