{-# LANGUAGE BangPatterns, DeriveDataTypeable #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.Text.Internal.Lazy
(
Text(..)
, chunk
, empty
, foldrChunks
, foldlChunks
, strictInvariant
, lazyInvariant
, showStructure
, defaultChunkSize
, smallChunkSize
, chunkOverhead
) where
import Data.Text ()
import Data.Text.Internal.Unsafe.Shift (shiftL)
import Data.Typeable (Typeable)
import Foreign.Storable (sizeOf)
import qualified Data.Text.Internal as T
data Text = Empty
| Chunk {-# UNPACK #-} !T.Text Text
deriving (Typeable)
strictInvariant :: Text -> Bool
strictInvariant :: Text -> Bool
strictInvariant Empty = Bool
True
strictInvariant x :: Text
x@(Chunk (T.Text _ _ len :: Int
len) cs :: Text
cs)
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = Text -> Bool
strictInvariant Text
cs
| Bool
otherwise = [Char] -> Bool
forall a. HasCallStack => [Char] -> a
error ([Char] -> Bool) -> [Char] -> Bool
forall a b. (a -> b) -> a -> b
$ "Data.Text.Lazy: invariant violation: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
x
lazyInvariant :: Text -> Text
lazyInvariant :: Text -> Text
lazyInvariant Empty = Text
Empty
lazyInvariant x :: Text
x@(Chunk c :: Text
c@(T.Text _ _ len :: Int
len) cs :: Text
cs)
| Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = Text -> Text -> Text
Chunk Text
c (Text -> Text
lazyInvariant Text
cs)
| Bool
otherwise = [Char] -> Text
forall a. HasCallStack => [Char] -> a
error ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ "Data.Text.Lazy: invariant violation: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
x
showStructure :: Text -> String
showStructure :: Text -> [Char]
showStructure Empty = "Empty"
showStructure (Chunk t :: Text
t Empty) = "Chunk " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
t [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ " Empty"
showStructure (Chunk t :: Text
t ts :: Text
ts) =
"Chunk " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
t [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ " (" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
ts [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ")"
chunk :: T.Text -> Text -> Text
{-# INLINE chunk #-}
chunk :: Text -> Text -> Text
chunk t :: Text
t@(T.Text _ _ len :: Int
len) ts :: Text
ts | Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = Text
ts
| Bool
otherwise = Text -> Text -> Text
Chunk Text
t Text
ts
empty :: Text
{-# INLINE [0] empty #-}
empty :: Text
empty = Text
Empty
foldrChunks :: (T.Text -> a -> a) -> a -> Text -> a
foldrChunks :: (Text -> a -> a) -> a -> Text -> a
foldrChunks f :: Text -> a -> a
f z :: a
z = Text -> a
go
where go :: Text -> a
go Empty = a
z
go (Chunk c :: Text
c cs :: Text
cs) = Text -> a -> a
f Text
c (Text -> a
go Text
cs)
{-# INLINE foldrChunks #-}
foldlChunks :: (a -> T.Text -> a) -> a -> Text -> a
foldlChunks :: (a -> Text -> a) -> a -> Text -> a
foldlChunks f :: a -> Text -> a
f z :: a
z = a -> Text -> a
go a
z
where go :: a -> Text -> a
go !a
a Empty = a
a
go !a
a (Chunk c :: Text
c cs :: Text
cs) = a -> Text -> a
go (a -> Text -> a
f a
a Text
c) Text
cs
{-# INLINE foldlChunks #-}
defaultChunkSize :: Int
defaultChunkSize :: Int
defaultChunkSize = 16384 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
chunkOverhead
{-# INLINE defaultChunkSize #-}
smallChunkSize :: Int
smallChunkSize :: Int
smallChunkSize = 128 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
chunkOverhead
{-# INLINE smallChunkSize #-}
chunkOverhead :: Int
chunkOverhead :: Int
chunkOverhead = Int -> Int
forall a. Storable a => a -> Int
sizeOf (Int
forall a. HasCallStack => a
undefined :: Int) Int -> Int -> Int
forall a. UnsafeShift a => a -> Int -> a
`shiftL` 1
{-# INLINE chunkOverhead #-}