M src/Data/Text/ParagraphLayout/Plain.hs => src/Data/Text/ParagraphLayout/Plain.hs +2 -0
@@ 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
M src/Data/Text/ParagraphLayout/Run.hs => src/Data/Text/ParagraphLayout/Run.hs +2 -0
@@ 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 }
M src/Data/Text/ParagraphLayout/TextContainer.hs => src/Data/Text/ParagraphLayout/TextContainer.hs +8 -7
@@ 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)