~jaro/balkon

4bd95db2a46001cccd030584d3de570b5e9366fa — Jaro 1 year, 8 months ago 0f5dfa9
Clean up ParagraphConstruction.
1 files changed, 20 insertions(+), 18 deletions(-)

M test/Data/Text/ParagraphLayout/ParagraphConstruction.hs
M test/Data/Text/ParagraphLayout/ParagraphConstruction.hs => test/Data/Text/ParagraphLayout/ParagraphConstruction.hs +20 -18
@@ 21,44 21,46 @@ import Data.Text.Foreign (lengthWord8)
import Data.Text.Internal (Text(Text))
import Data.Text.ParagraphLayout.Plain
    (Paragraph(Paragraph)
    ,ParagraphOptions()
    ,ParagraphOptions
    ,Span(Span)
    )

-- | Create first span with optional ignored suffix.
infixr 5 >|
(>|) :: (String, String) -> String -> (Text, [Span])
(spanText, spanLanguage) >| ignoredSuffix = (newText, newSpans)
(spanLanguage, spanText) >| suffix = (newText, newSpans)
    where
        newSpans = [Span (fromIntegral $ lengthWord8 packedSpanText) spanLanguage]
        newSpans = [newSpan]
        newSpan = Span (fromIntegral $ lengthWord8 packedSpanText) spanLanguage
        newText = append packedSpanText packedSuffix
        packedSpanText = pack spanText
        packedSuffix = pack ignoredSuffix
        packedSuffix = pack suffix

-- Create next span.
-- | Create next span.
infixr 5 >|<
(>|<) :: (String, String) -> (Text, [Span]) -> (Text, [Span])
(spanText, spanLanguage) >|< (oldText, oldSpans) = (newText, newSpans)
(spanLanguage, spanText) >|< (oldText, oldSpans) = (newText, newSpans)
    where
        newSpans = Span (fromIntegral $ lengthWord8 packedText) spanLanguage : oldSpans
        newText = append packedText oldText
        packedText = pack spanText
        newSpans = newSpan:oldSpans
        newSpan = Span (fromIntegral $ lengthWord8 packedSpanText) spanLanguage
        newText = append packedSpanText oldText
        packedSpanText = pack spanText

-- Add optional ignored prefix and wrap in a `Paragraph`.
-- | Add optional ignored prefix and wrap in a `Paragraph`.
infixr 5 |<
(|<) :: String -> (Text, [Span]) -> ParagraphOptions -> Paragraph
ignoredPrefix |< (oldText, spans) = Paragraph arr (fromIntegral off) spans
prefix |< (oldText, spans) = Paragraph arr (fromIntegral afterPrefix) spans
    where
        (Text arr offPrefix _) = append packedPrefix oldText
        off = offPrefix + (lengthWord8 packedPrefix)
        packedPrefix = pack ignoredPrefix
        (Text arr beforePrefix _) = append packedPrefix oldText
        afterPrefix = beforePrefix + lengthWord8 packedPrefix
        packedPrefix = pack prefix

-- Create a `Paragraph` with no spans, just two ignored texts.
-- | Create a `Paragraph` with no spans, just two ignored texts.
infixr 5 |<>|
(|<>|) :: String -> String -> ParagraphOptions -> Paragraph
ignoredPrefix |<>| ignoredSuffix = ignoredPrefix |< (pack ignoredSuffix, [])
prefix |<>| suffix = prefix |< (pack suffix, [])

-- Combine language with text.
-- | Combine language with text.
infix 6 ~
(~) :: String -> String -> (String, String)
lang ~ txt = (txt, lang)
lang ~ txt = (lang, txt)