From 7f62b6d8ac2377b83ed8ac6cba392709fd8b5e67 Mon Sep 17 00:00:00 2001 From: Jaro Date: Fri, 10 Mar 2023 02:54:09 +0100 Subject: [PATCH] Add dropWhileEnd to SeparableTextContainer. --- src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs | 1 + src/Data/Text/ParagraphLayout/Internal/Run.hs | 3 ++- src/Data/Text/ParagraphLayout/Internal/TextContainer.hs | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs b/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs index 99ce7de..26e3f1d 100644 --- a/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs +++ b/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs @@ -43,6 +43,7 @@ instance TextContainer a => TextContainer (WithSpan a) where instance SeparableTextContainer a => SeparableTextContainer (WithSpan a) where splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2) where (c1, c2) = splitTextAt8 n c + dropWhileEnd p (WithSpan rs c) = WithSpan rs (dropWhileEnd p c) splitBySpanIndex :: [WithSpan a] -> [[a]] splitBySpanIndex xs = [getBySpanIndex i xs | i <- [0..]] diff --git a/src/Data/Text/ParagraphLayout/Internal/Run.hs b/src/Data/Text/ParagraphLayout/Internal/Run.hs index 0f764b9..4448cf1 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Run.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Run.hs @@ -4,6 +4,7 @@ where import Data.List (mapAccumL) import Data.List.NonEmpty (NonEmpty((:|))) import Data.Text (Text) +import qualified Data.Text as Text import Data.Text.Foreign (dropWord8, lengthWord8, takeWord8) import Data.Text.Glyphize (Direction(..)) import qualified Data.Text.ICU.Char as ICUChar @@ -15,7 +16,6 @@ import Data.Text.ParagraphLayout.Internal.Zipper type ScriptCode = String - -- | Each span can be broken into one or more runs by Balkón. -- -- Each run could have a different script, language, or direction. @@ -40,6 +40,7 @@ instance SeparableTextContainer Run where t1 = takeWord8 (fromIntegral n) t t2 = dropWord8 (fromIntegral n) t t = getText r + dropWhileEnd p r = r { runText = Text.dropWhileEnd p (runText r) } type ProtoRun = (Zipper, Maybe Direction, ScriptCode) diff --git a/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs b/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs index 4fe13d3..0217674 100644 --- a/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs +++ b/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs @@ -2,6 +2,7 @@ module Data.Text.ParagraphLayout.Internal.TextContainer (SeparableTextContainer ,TextContainer ,collapse + ,dropWhileEnd ,getText ,splitTextAt8 ,splitTextsAt8 @@ -25,17 +26,23 @@ instance TextContainer Text where -- | Class of data types containing `Text` that can be split at a given number -- of `Data.Word.Word8` units from the start of the text. class TextContainer a => SeparableTextContainer a where + -- | Split the given `SeparableTextContainer` at the given number of -- `Data.Word.Word8` units from the start of the text, preserving whatever -- constraints the instance requires. splitTextAt8 :: Int -> a -> (a, a) + -- | Return the prefix remaining after dropping characters that satisfy + -- the given predicate from the end of the given `SeparableTextContainer`. + dropWhileEnd :: (Char -> Bool) -> a -> a + -- | As a trivial instance, each `Text` can be split directly. instance SeparableTextContainer Text where splitTextAt8 n t = (t1, t2) where t1 = takeWord8 (fromIntegral n) t t2 = dropWord8 (fromIntegral n) t + dropWhileEnd = Text.dropWhileEnd -- | Treat a list of text containers as a contiguous sequence, -- and make a split at the given number of `Data.Word.Word8` from the beginning -- 2.30.2