From 0c1b4da5830c874a0964c62fbb37716e1b35615b Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Mon, 26 Oct 2020 12:30:55 -0400 Subject: [PATCH] Add ascii7 and ascii8 --- CHANGELOG.md | 3 ++- bytebuild.cabal | 2 +- src/Data/Bytes/Builder.hs | 14 +++++++++++++- src/Data/Bytes/Builder/Bounded.hs | 29 +++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f82251d..a752c9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 to ease the migration process. -## 0.3.6.1 -- 2020-??-?? +## 0.3.7.0 -- 2020-??-?? * Fix build error in test suite. +* Add `ascii7` and `ascii8`. ## 0.3.6.0 -- 2020-06-30 diff --git a/bytebuild.cabal b/bytebuild.cabal index 2fe2674..2c40f21 100644 --- a/bytebuild.cabal +++ b/bytebuild.cabal @@ -1,6 +1,6 @@ cabal-version: 2.2 name: bytebuild -version: 0.3.6.0 +version: 0.3.7.0 synopsis: Serialize to a small byte arrays description: This is similar to the builder facilities provided by diff --git a/src/Data/Bytes/Builder.hs b/src/Data/Bytes/Builder.hs index 892f1c9..bfe2ddf 100644 --- a/src/Data/Bytes/Builder.hs +++ b/src/Data/Bytes/Builder.hs @@ -64,6 +64,8 @@ module Data.Bytes.Builder , ascii4 , ascii5 , ascii6 + , ascii7 + , ascii8 , char -- ** Machine-Readable -- *** 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 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. 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) +-- | 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. char :: Char -> Builder char c = fromBounded Nat.constant (Bounded.char c) diff --git a/src/Data/Bytes/Builder/Bounded.hs b/src/Data/Bytes/Builder/Bounded.hs index 5a2c876..377c23c 100644 --- a/src/Data/Bytes/Builder/Bounded.hs +++ b/src/Data/Bytes/Builder/Bounded.hs @@ -65,6 +65,8 @@ module Data.Bytes.Builder.Bounded , ascii4 , ascii5 , ascii6 + , ascii7 + , ascii8 , char -- ** Native , 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) 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. wordLEB128 :: Word -> Builder 10 wordLEB128 (W# w) = lebCommon (W# w)