From 7bc4ab57fb1fa08d10db5c12909785a553cf1cc6 Mon Sep 17 00:00:00 2001 From: Jaro Date: Tue, 7 Mar 2023 02:17:45 +0100 Subject: [PATCH] Make ParagraphConstruction more efficient. --- .../ParagraphLayout/ParagraphConstruction.hs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs b/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs index 6a1465d..5bcd291 100644 --- a/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs +++ b/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs @@ -1,12 +1,10 @@ --- | Infix operators for readable construction of paragraphs as testing input. +-- | Infix operators for construction of paragraphs with readable code. -- -- Example construction: -- @"ignored prefix" |< "en"~"one two " >|< "ja"~"三四" >| "ignored suffix"@ -- -- Special syntax for paragraphs with no contents: -- @"ignored prefix" |<>| "ignored suffix"@ --- --- Please note that this form of construction is inefficient for longer text. module Data.Text.ParagraphLayout.ParagraphConstruction ((>|) ,(>|<) @@ -16,9 +14,12 @@ module Data.Text.ParagraphLayout.ParagraphConstruction ) where -import Data.Text (append, pack) +import Data.Text (pack) import Data.Text.Foreign (lengthWord8) import Data.Text.Internal (Text(Text)) +import Data.Text.Internal.Lazy (chunk, empty) +import qualified Data.Text.Internal.Lazy as Lazy +import Data.Text.Lazy (toStrict) import Data.Text.ParagraphLayout.Plain (Paragraph(Paragraph) ,ParagraphOptions @@ -27,38 +28,38 @@ import Data.Text.ParagraphLayout.Plain -- | Create first span with optional ignored suffix. infixr 5 >| -(>|) :: (String, String) -> String -> (Text, [Span]) +(>|) :: (String, String) -> String -> (Lazy.Text, [Span]) (spanLanguage, spanText) >| suffix = (newText, newSpans) where newSpans = [newSpan] newSpan = Span (lengthWord8 packedSpanText) spanLanguage - newText = append packedSpanText packedSuffix + newText = chunk packedSpanText (chunk packedSuffix empty) packedSpanText = pack spanText packedSuffix = pack suffix -- | Create next span. infixr 5 >|< -(>|<) :: (String, String) -> (Text, [Span]) -> (Text, [Span]) +(>|<) :: (String, String) -> (Lazy.Text, [Span]) -> (Lazy.Text, [Span]) (spanLanguage, spanText) >|< (oldText, oldSpans) = (newText, newSpans) where newSpans = newSpan:oldSpans newSpan = Span (lengthWord8 packedSpanText) spanLanguage - newText = append packedSpanText oldText + newText = chunk packedSpanText oldText packedSpanText = pack spanText -- | Add optional ignored prefix and wrap in a `Paragraph`. infixr 5 |< -(|<) :: String -> (Text, [Span]) -> ParagraphOptions -> Paragraph +(|<) :: String -> (Lazy.Text, [Span]) -> ParagraphOptions -> Paragraph prefix |< (oldText, spans) = Paragraph arr afterPrefix spans where - (Text arr beforePrefix _) = append packedPrefix oldText + (Text arr beforePrefix _) = toStrict $ chunk packedPrefix oldText afterPrefix = beforePrefix + lengthWord8 packedPrefix packedPrefix = pack prefix -- | Create a `Paragraph` with no spans, just two ignored texts. infixr 5 |<>| (|<>|) :: String -> String -> ParagraphOptions -> Paragraph -prefix |<>| suffix = prefix |< (pack suffix, []) +prefix |<>| suffix = prefix |< (chunk (pack suffix) empty, []) -- | Combine language with text. infix 6 ~ -- 2.30.2