~jaro/balkon

ref: 1555a8cc7b5b6ad8c7f365564876dc6f5649f19c balkon/src/Data/Text/ParagraphLayout/Internal/TextContainer.hs -rw-r--r-- 1.4 KiB
1555a8ccJaro Move non-public modules into Internal namespace. 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Data.Text.ParagraphLayout.Internal.TextContainer
    (SeparableTextContainer
    ,TextContainer
    ,getText
    ,splitTextAt8
    ,splitTextsAt8
    )
where

import Data.Text (Text)
import Data.Text.Foreign (lengthWord8)

-- | Class of data types containing `Text` that can be accessed.
class TextContainer a where
    getText :: a -> Text

-- | 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 :: Int -> a -> (a, a)

-- | Treat a list of text containers as a contiguous sequence,
-- and make a split at the given number of `Word8` from the beginning
-- of this sequence.
--
-- 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 :: SeparableTextContainer a => Int -> [a] -> ([a], [a])
splitTextsAt8 n rs = (pre, post)
    where
        pre = reverse rpre
        (rpre, post) = splitTextsAt8' n [] rs

splitTextsAt8' :: SeparableTextContainer a => Int -> [a] -> [a] -> ([a], [a])
splitTextsAt8' _ rpre [] = (rpre, [])
splitTextsAt8' n rpre (r:rs)
    | n <= 0 = (rpre, r:rs)
    | n >= l = splitTextsAt8' (n - l) (r:rpre) (rs)
    | otherwise = let (r1, r2) = splitTextAt8 n r in (r1:rpre, r2:rs)
    where
        l = lengthWord8 $ getText r