From bce84f37c6ff86025c3449415924a15914c733dd Mon Sep 17 00:00:00 2001 From: Jaro Date: Wed, 1 Mar 2023 04:32:27 +0100 Subject: [PATCH] Define SeparableTextContainer as its own class. --- src/Data/Text/ParagraphLayout/Plain.hs | 2 ++ src/Data/Text/ParagraphLayout/Run.hs | 2 ++ src/Data/Text/ParagraphLayout/TextContainer.hs | 15 ++++++++------- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Plain.hs b/src/Data/Text/ParagraphLayout/Plain.hs index 1fab9c7..8f7e8d7 100644 --- a/src/Data/Text/ParagraphLayout/Plain.hs +++ b/src/Data/Text/ParagraphLayout/Plain.hs @@ -101,6 +101,8 @@ instance Functor WithSpan where instance TextContainer a => TextContainer (WithSpan a) where getText (WithSpan _ c) = getText c + +instance SeparableTextContainer a => SeparableTextContainer (WithSpan a) where splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2) where (c1, c2) = splitTextAt8 n c diff --git a/src/Data/Text/ParagraphLayout/Run.hs b/src/Data/Text/ParagraphLayout/Run.hs index ad14e8a..fbd6677 100644 --- a/src/Data/Text/ParagraphLayout/Run.hs +++ b/src/Data/Text/ParagraphLayout/Run.hs @@ -30,6 +30,8 @@ data Run = Run instance TextContainer Run where getText = runText + +instance SeparableTextContainer Run where splitTextAt8 n r = ( r { runText = t1 } , r { runText = t2, runOffsetInSpan = runOffsetInSpan r + l1 } diff --git a/src/Data/Text/ParagraphLayout/TextContainer.hs b/src/Data/Text/ParagraphLayout/TextContainer.hs index 171274b..03c927f 100644 --- a/src/Data/Text/ParagraphLayout/TextContainer.hs +++ b/src/Data/Text/ParagraphLayout/TextContainer.hs @@ -1,5 +1,6 @@ module Data.Text.ParagraphLayout.TextContainer - (TextContainer + (SeparableTextContainer + ,TextContainer ,getText ,splitTextAt8 ,splitTextsAt8 @@ -9,13 +10,13 @@ where import Data.Text (Text) import Data.Text.Foreign (I8, lengthWord8) +-- | Class of data types containing `Text` that can be accessed. class TextContainer a where - - -- | Unwrap text from the container. getText :: a -> Text - -- | Split a text container at the given number of `Word8` units - -- from its beginning. +-- | Class of data types containing `Text` that can be split at a given number +-- of `Word8` units from the start of the text. +class TextContainer a => SeparableTextContainer a where splitTextAt8 :: I8 -> a -> (a, a) splitTextAt8 _ _ = error "container cannot be split" @@ -25,13 +26,13 @@ class TextContainer a where -- -- If @n@ falls on a container boundary, the total number of output containers -- will equal the number of input containers; otherwise, it will be one larger. -splitTextsAt8 :: TextContainer a => I8 -> [a] -> ([a], [a]) +splitTextsAt8 :: SeparableTextContainer a => I8 -> [a] -> ([a], [a]) splitTextsAt8 n rs = (pre, post) where pre = reverse rpre (rpre, post) = splitTextsAt8' n [] rs -splitTextsAt8' :: TextContainer a => I8 -> [a] -> [a] -> ([a], [a]) +splitTextsAt8' :: SeparableTextContainer a => I8 -> [a] -> [a] -> ([a], [a]) splitTextsAt8' _ rpre [] = (rpre, []) splitTextsAt8' n rpre (r:rs) | n <= 0 = (rpre, r:rs) -- 2.30.2