From 904bd77ec7a8460971bec4a592629ee74c8aec3f Mon Sep 17 00:00:00 2001 From: Jaro Date: Thu, 9 Mar 2023 20:12:45 +0100 Subject: [PATCH] Add trivial instances of text containers. --- .../Text/ParagraphLayout/Internal/TextContainer.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs b/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs index 1405f17..4fe13d3 100644 --- a/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs +++ b/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs @@ -11,13 +11,17 @@ where import Data.List.NonEmpty (NonEmpty((:|))) import Data.Text (Text) import qualified Data.Text as Text -import Data.Text.Foreign (lengthWord8) +import Data.Text.Foreign (dropWord8, lengthWord8, takeWord8) -- | Class of data types containing `Text` that can be accessed. class TextContainer a where -- | Extract a `Text` from its container. getText :: a -> Text +-- | As a trivial instance, each `Text` contains itself. +instance TextContainer Text where + getText = id + -- | 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 @@ -26,6 +30,13 @@ class TextContainer a => SeparableTextContainer a where -- constraints the instance requires. splitTextAt8 :: Int -> a -> (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 + -- | 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 -- of this sequence. -- 2.30.2