From 4c7227e7e96096660f8eccdd492a48a7287b2537 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Wed, 26 Feb 2020 12:00:52 -0500 Subject: [PATCH] Add ascii(2|3|4|5|6) --- CHANGELOG.md | 1 + src/Data/ByteArray/Builder.hs | 30 +++++++++++++++ src/Data/ByteArray/Builder/Bounded.hs | 55 +++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 956bd71..5e95810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add `wordPaddedDec4`. * Add `reversedOnto` and `commitsOntoChunks`. +* Add `ascii(2|3|4|5|6)`. ## 0.3.3.0 -- 2020-02-10 diff --git a/src/Data/ByteArray/Builder.hs b/src/Data/ByteArray/Builder.hs index 5cc9b9c..d1f8264 100644 --- a/src/Data/ByteArray/Builder.hs +++ b/src/Data/ByteArray/Builder.hs @@ -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) diff --git a/src/Data/ByteArray/Builder/Bounded.hs b/src/Data/ByteArray/Builder/Bounded.hs index 1b52be3..1411b31 100644 --- a/src/Data/ByteArray/Builder/Bounded.hs +++ b/src/Data/ByteArray/Builder/Bounded.hs @@ -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