-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for padding and char replication
- Add `Char` replication functions `prependChars` and `appendChars` - Add padding functions `justifyLeft`, `justifyRight` and `center`. - Move low-level `MArray` manipulation to their own module `Data.Text.Builder.Linear.Array`. - Add corresponding tests and benchmarks.
- Loading branch information
Showing
7 changed files
with
410 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
-- | | ||
-- Copyright: (c) 2022 Andrew Lelechenko | ||
-- Licence: BSD3 | ||
-- Maintainer: Andrew Lelechenko <[email protected]> | ||
-- | ||
-- Low-level routines for 'A.MArray' manipulations. | ||
module Data.Text.Builder.Linear.Array ( | ||
unsafeThaw, | ||
sizeofByteArray, | ||
isPinned, | ||
unsafeTile, | ||
unsafeReplicate, | ||
) where | ||
|
||
import Data.Text.Array qualified as A | ||
import GHC.Exts (Int (..), isByteArrayPinned#, isTrue#, setByteArray#, sizeofByteArray#, unsafeCoerce#) | ||
import GHC.ST (ST (..)) | ||
|
||
unsafeThaw ∷ A.Array → ST s (A.MArray s) | ||
unsafeThaw (A.ByteArray a) = ST $ \s# → | ||
(# s#, A.MutableByteArray (unsafeCoerce# a) #) | ||
|
||
sizeofByteArray ∷ A.Array → Int | ||
sizeofByteArray (A.ByteArray a) = I# (sizeofByteArray# a) | ||
|
||
isPinned ∷ A.Array → Bool | ||
isPinned (A.ByteArray a) = isTrue# (isByteArrayPinned# a) | ||
|
||
-- | Replicate an ASCII character | ||
-- | ||
-- __Warning:__ it is the responsibility of the caller to ensure that the 'Int' | ||
-- is a valid ASCII character. | ||
unsafeReplicate | ||
∷ A.MArray s | ||
-- ^ Mutable array | ||
→ Int | ||
-- ^ Offset | ||
→ Int | ||
-- ^ Count | ||
→ Int | ||
-- ^ ASCII character | ||
→ ST s () | ||
unsafeReplicate (A.MutableByteArray dst#) (I# dstOff#) (I# count#) (I# w#) = | ||
ST (\s# → (# setByteArray# dst# dstOff# count# w# s#, () #)) | ||
{-# INLINE unsafeReplicate #-} | ||
|
||
-- | Duplicate a portion of an array in-place. | ||
-- | ||
-- Example of use: | ||
-- | ||
-- @ | ||
-- -- Write @count@ times the char @c@ | ||
-- let cLen = utf8Length c; totalLen = cLen * count | ||
-- in unsafeWrite dst dstOff ch *> 'unsafeTile' dst dstOff totalLen cLen | ||
-- @ | ||
unsafeTile | ||
∷ A.MArray s | ||
-- ^ Mutable array | ||
→ Int | ||
-- ^ Start of the portion to duplicate | ||
→ Int | ||
-- ^ Total length of the duplicate | ||
→ Int | ||
-- ^ Length of the portion to duplicate | ||
→ ST s () | ||
unsafeTile dest destOff totalLen = go | ||
where | ||
-- Adapted from Data.Text.Array.tile | ||
go l | ||
| 2 * l > totalLen = A.copyM dest (destOff + l) dest destOff (totalLen - l) | ||
| otherwise = A.copyM dest (destOff + l) dest destOff l *> go (2 * l) | ||
{-# INLINE unsafeTile #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.