-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for padding and char replication #14
Conversation
e7c1d4e
to
ad82069
Compare
Rebased. |
-- "xxxxAAAxxx" | ||
-- >>> runBuffer (\b -> center 5 'x' (appendChars 6 'A' b)) | ||
-- "AAAAAA" | ||
center ∷ Word → Char → Buffer ⊸ Buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, do you have a use case for such functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the padding functions specifically? Mainly pretty-printing. For example, I generate some C files with lots of arrays, but I want them to be well presented to facilitate review, so padding with 0 for hex values and spaces for general text comes quite handy.
This also bring the API closer to the text
one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you possibly share a snippet using the proposed API? What happens when you already have some data in a Buffer
and want to append a padded hexadecimal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After try using the API with non-trivial examples, I see what you mean 😄
The immediate solution I came with is to add the following function to the API:
newEmptyBuffer ∷ Buffer ⊸ (# Buffer, Buffer #)
newEmptyBuffer (Buffer t@(Text arr _ _)) =
(# Buffer t, Buffer (if isPinned arr then memptyPinned else mempty) #)
then an example of the use of justifyRight
is:
-- | Convenient function to append padded hex
infixl 6 |>&&
(|>&&) :: (Integral a, FiniteBits a) => Buffer ⊸ (Word, a) -> Buffer
b |>&& (w, n) = case newEmptyBuffer b of
(# b', empty #) -> (b' |># "0x"#) >< justifyRight w '0' (empty |>& n)
-- Example
runBuffer \b -> (b |># "Foo "#) |>&& (4, 0xf :: Word) |># "; "# |>&& (4, 0xabc :: Word)
-- "Foo 0x000f; 0x0abc"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's add newEmptyBuffer
than. Not sure about the name though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. About naming: I did not named simply emptyBuffer
because it requires an input. But I have not strong opinion about this. Other candidates: makeEmptyBuffer
or the shorter mkEmptyBuffer
.
ad82069
to
2cba8f6
Compare
@Bodigrim Rebased, with the dependency to |
I think all we need is fooInt :: (Int → a) → (Int ⊸ a)
fooInt = unsafeCoerce
fooWord :: (Word → a) → (Word ⊸ a)
fooWord = unsafeCoerce There is nothing unsafe about them, so I'd suggest just define them in the module where they are used. |
60a8b9b
to
b2d3c84
Compare
@Bodigrim done. |
There is still a conflict in |
- 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.
Some situations may require a fresh empty buffer. For example, if one want to define a function that append padded hexadecimal numbers, the use of a mere `justifyRight` requires the input `Buffer` to be empty. We introduce `newEmptyBuffer` to solve this issue. It has the caveat to require an existing `Buffer`, because we need to to know if the array is pinned or not. Working example for padded hexadecimal numbers: -- | Convenient function to append padded hex infixl 6 |>&& (|>&&) ∷ (Integral a, FiniteBits a) ⇒ Buffer ⊸ (Word, a) → Buffer b |>&& (w, n) = case newEmptyBuffer b of (# b', empty #) → (b' |># "0x"#) >< justifyRight w '0' (empty |>& n) -- Example runBuffer \b → (b |># "Foo "#) |>&& (4, 0xf ∷ Word) |># "; "# |>&& (4, 0xabc ∷ Word) -- "Foo 0x000f; 0x0abc"
b2d3c84
to
2c4bb34
Compare
@Bodigrim Sorry for that, I did not see it. Fixed by rebasing. Also, congrats for the release of |
prependExact | ||
totalLen | ||
(\dst dstOff → unsafeWrite dst dstOff ch *> unsafeTile dst dstOff totalLen cLen) | ||
buff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's reduce code duplication here: in both cases you can run prependExact (utf8Length ch * fromIntegral count) fun buff
, it's only fun
which depends on isAscii ch
.
utf8Length
is a fast function, there is little to save by not calling it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
-- >>> runBuffer (\b -> justifyRight 10 'x' (appendChars 3 'A' b)) | ||
-- "xxxxxxxAAA" | ||
-- >>> runBuffer (\b -> justifyRight 5 'x' (appendChars 6 'A' b)) | ||
-- "AAAAAA" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a usage example with newEmptyBuffer
, it's untrivial to come up with for a new user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
-- The first 'Buffer' is the input and the second is a new empty 'Buffer'. | ||
-- | ||
-- Note: a previous buffer is necessary in order to create an empty buffer with | ||
-- the same characteristics. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a usage example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thanks, great! |
Char
replication functionsprependChars
andappendChars
justifyLeft
,justifyRight
andcenter
.MArray
manipulation to their own moduleData.Text.Builder.Linear.Array
.See also #13, which propose a similar interface for padding.
Note that it adds a dependency on
linear-base
to usemove
andUr
.