~jaro/balkon

7f62b6d8ac2377b83ed8ac6cba392709fd8b5e67 — Jaro 1 year, 8 months ago 904bd77
Add dropWhileEnd to SeparableTextContainer.
M src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs => src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs +1 -0
@@ 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..]]

M src/Data/Text/ParagraphLayout/Internal/Run.hs => src/Data/Text/ParagraphLayout/Internal/Run.hs +2 -1
@@ 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)


M src/Data/Text/ParagraphLayout/Internal/TextContainer.hs => src/Data/Text/ParagraphLayout/Internal/TextContainer.hs +7 -0
@@ 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