From 35b89dcf42fedb4911c9083596f1c0a2df40e6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Mon, 27 Nov 2023 11:52:58 +0100 Subject: [PATCH 1/2] Add type synonyms for lazy and strict text flavours closes #317 --- src/Data/Text.hs | 3 ++- src/Data/Text/Internal.hs | 4 ++++ src/Data/Text/Internal/Lazy.hs | 4 ++++ src/Data/Text/Lazy.hs | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Data/Text.hs b/src/Data/Text.hs index 60b1aedae..fd04db564 100644 --- a/src/Data/Text.hs +++ b/src/Data/Text.hs @@ -53,6 +53,7 @@ module Data.Text -- * Types Text + , StrictText -- * Creation and elimination , pack @@ -238,7 +239,7 @@ import qualified Data.Text.Internal.Fusion.Common as S import Data.Text.Encoding (decodeUtf8', encodeUtf8) import Data.Text.Internal.Fusion (stream, reverseStream, unstream) import Data.Text.Internal.Private (span_) -import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text, append, pack) +import Data.Text.Internal (Text(..), StrictText, empty, firstf, mul, safe, text, append, pack) import Data.Text.Internal.Unsafe.Char (unsafeWrite, unsafeChr8) import Data.Text.Show (singleton, unpack, unpackCString#, unpackCStringAscii#) import qualified Prelude as P diff --git a/src/Data/Text/Internal.hs b/src/Data/Text/Internal.hs index 3fc271ea8..305d8a3af 100644 --- a/src/Data/Text/Internal.hs +++ b/src/Data/Text/Internal.hs @@ -29,6 +29,7 @@ module Data.Text.Internal -- * Types -- $internals Text(..) + , StrictText -- * Construction , text , textP @@ -68,6 +69,9 @@ data Text = Text {-# UNPACK #-} !Int -- ^ length in bytes (not in Char!), pointing to an end of UTF-8 sequence deriving (Typeable) +-- | Type synonym for the strict flavour of 'Text'. +type StrictText = Text + -- | Smart constructor. text_ :: #if defined(ASSERTS) diff --git a/src/Data/Text/Internal/Lazy.hs b/src/Data/Text/Internal/Lazy.hs index 928e5cfcc..12c106994 100644 --- a/src/Data/Text/Internal/Lazy.hs +++ b/src/Data/Text/Internal/Lazy.hs @@ -21,6 +21,7 @@ module Data.Text.Internal.Lazy ( Text(..) + , LazyText , chunk , empty , foldrChunks @@ -51,6 +52,9 @@ data Text = Empty | Chunk {-# UNPACK #-} !T.Text Text deriving (Typeable) +-- | Type synonym for the lazy flavour of 'Text'. +type LazyText = Text + -- $invariant -- -- The data type invariant for lazy 'Text': Every 'Text' is either 'Empty' or diff --git a/src/Data/Text/Lazy.hs b/src/Data/Text/Lazy.hs index 2195793a5..0480329b2 100644 --- a/src/Data/Text/Lazy.hs +++ b/src/Data/Text/Lazy.hs @@ -46,6 +46,7 @@ module Data.Text.Lazy -- * Types Text + , LazyText -- * Creation and elimination , pack @@ -230,7 +231,7 @@ import qualified Data.Text.Internal.Lazy.Fusion as S import Data.Text.Internal.Fusion.Types (PairS(..)) import Data.Text.Internal.Lazy.Fusion (stream, unstream) import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks, - foldrChunks, smallChunkSize, defaultChunkSize, equal) + foldrChunks, smallChunkSize, defaultChunkSize, equal, LazyText) import Data.Text.Internal (firstf, safe, text) import Data.Text.Lazy.Encoding (decodeUtf8', encodeUtf8) import Data.Text.Internal.Lazy.Search (indices) From e9300db422665bb75130c3176b04374c0a975a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Mon, 27 Nov 2023 12:57:45 +0100 Subject: [PATCH 2/2] Change the type signatures of toStrict and fromStrict --- src/Data/Text/Lazy.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/Text/Lazy.hs b/src/Data/Text/Lazy.hs index 0480329b2..e388a0348 100644 --- a/src/Data/Text/Lazy.hs +++ b/src/Data/Text/Lazy.hs @@ -442,12 +442,12 @@ toChunks :: Text -> [T.Text] toChunks cs = foldrChunks (:) [] cs -- | /O(n)/ Convert a lazy 'Text' into a strict 'T.Text'. -toStrict :: Text -> T.Text +toStrict :: LazyText -> T.StrictText toStrict t = T.concat (toChunks t) {-# INLINE [1] toStrict #-} -- | /O(c)/ Convert a strict 'T.Text' into a lazy 'Text'. -fromStrict :: T.Text -> Text +fromStrict :: T.StrictText -> LazyText fromStrict t = chunk t Empty {-# INLINE [1] fromStrict #-}