Add ascii(2|3|4|5|6)

This commit is contained in:
Andrew Martin 2020-02-26 12:00:52 -05:00
parent 1f2aa9b110
commit 4c7227e7e9
3 changed files with 86 additions and 0 deletions

View file

@ -4,6 +4,7 @@
* Add `wordPaddedDec4`.
* Add `reversedOnto` and `commitsOntoChunks`.
* Add `ascii(2|3|4|5|6)`.
## 0.3.3.0 -- 2020-02-10

View file

@ -53,6 +53,11 @@ module Data.ByteArray.Builder
, word8PaddedUpperHex
, word8LowerHex
, ascii
, ascii2
, ascii3
, ascii4
, ascii5
, ascii6
, char
-- ** Machine-Readable
-- *** One
@ -781,6 +786,31 @@ word8PaddedUpperHex w =
ascii :: Char -> Builder
ascii c = fromBoundedOne (Bounded.ascii c)
-- | Encode two ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
ascii2 :: Char -> Char -> Builder
ascii2 a b = fromBounded Nat.constant (Bounded.ascii2 a b)
-- | Encode three ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
ascii3 :: Char -> Char -> Char -> Builder
ascii3 a b c = fromBounded Nat.constant (Bounded.ascii3 a b c)
-- | Encode four ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
ascii4 :: Char -> Char -> Char -> Char -> Builder
ascii4 a b c d = fromBounded Nat.constant (Bounded.ascii4 a b c d)
-- | Encode five ASCII characters.
-- Precondition: Must be an ASCII characters. This is not checked.
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.
-- 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 a UTF-8 char. This only uses as much space as is required.
char :: Char -> Builder
char c = fromBounded Nat.constant (Bounded.char c)

View file

@ -58,6 +58,11 @@ module Data.ByteArray.Builder.Bounded
, word8PaddedUpperHex
, word8LowerHex
, ascii
, ascii2
, ascii3
, ascii4
, ascii5
, ascii6
, char
-- ** Native
, wordPaddedDec2
@ -697,6 +702,56 @@ ascii (C# c) = Unsafe.construct $ \(MutableByteArray arr) (I# off) -> do
primitive_ (writeCharArray# arr off c)
pure (I# (off +# 1# ))
-- | Encode two ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii2 :: Char -> Char -> Builder 2
ascii2 (C# c0) (C# c1) = Unsafe.construct $ \(MutableByteArray arr) (I# off) -> do
primitive_ (writeCharArray# arr off c0)
primitive_ (writeCharArray# arr (off +# 1# ) c1)
pure (I# (off +# 2# ))
-- | Encode three ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii3 :: Char -> Char -> Char -> Builder 3
ascii3 (C# c0) (C# c1) (C# c2) = Unsafe.construct $ \(MutableByteArray arr) (I# off) -> do
primitive_ (writeCharArray# arr off c0)
primitive_ (writeCharArray# arr (off +# 1# ) c1)
primitive_ (writeCharArray# arr (off +# 2# ) c2)
pure (I# (off +# 3# ))
-- | Encode four ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii4 :: Char -> Char -> Char -> Char -> Builder 4
ascii4 (C# c0) (C# c1) (C# c2) (C# c3) = 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)
pure (I# (off +# 4# ))
-- | Encode five ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii5 :: Char -> Char -> Char -> Char -> Char -> Builder 5
ascii5 (C# c0) (C# c1) (C# c2) (C# c3) (C# c4) = 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)
pure (I# (off +# 5# ))
-- | Encode six ASCII characters. Precondition: Must be an ASCII characters.
-- This is not checked.
ascii6 :: Char -> Char -> Char -> Char -> Char -> Char -> Builder 6
ascii6 (C# c0) (C# c1) (C# c2) (C# c3) (C# c4) (C# c5) = 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)
pure (I# (off +# 6# ))
-- | Encode a character as UTF-8. This only uses as much space as is required.
char :: Char -> Builder 4
char c