{-# 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 Text
Empty = Bool
True
strictInvariant x :: Text
x@(Chunk (T.Text Array
_ Int
_ Int
len) Text
cs)
| Int
len forall a. Ord a => a -> a -> Bool
> Int
0 = Text -> Bool
strictInvariant Text
cs
| Bool
otherwise = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Data.Text.Lazy: invariant violation: "
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
x
lazyInvariant :: Text -> Text
lazyInvariant :: Text -> Text
lazyInvariant Text
Empty = Text
Empty
lazyInvariant x :: Text
x@(Chunk c :: Text
c@(T.Text Array
_ Int
_ Int
len) Text
cs)
| Int
len forall a. Ord a => a -> a -> Bool
> Int
0 = Text -> Text -> Text
Chunk Text
c (Text -> Text
lazyInvariant Text
cs)
| Bool
otherwise = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Data.Text.Lazy: invariant violation: "
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
x
showStructure :: Text -> String
showStructure :: Text -> [Char]
showStructure Text
Empty = [Char]
"Empty"
showStructure (Chunk Text
t Text
Empty) = [Char]
"Chunk " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Text
t forall a. [a] -> [a] -> [a]
++ [Char]
" Empty"
showStructure (Chunk Text
t Text
ts) =
[Char]
"Chunk " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Text
t forall a. [a] -> [a] -> [a]
++ [Char]
" (" forall a. [a] -> [a] -> [a]
++ Text -> [Char]
showStructure Text
ts forall a. [a] -> [a] -> [a]
++ [Char]
")"
chunk :: T.Text -> Text -> Text
{-# INLINE chunk #-}
chunk :: Text -> Text -> Text
chunk t :: Text
t@(T.Text Array
_ Int
_ Int
len) Text
ts | Int
len forall a. Eq a => a -> a -> Bool
== Int
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 :: forall a. (Text -> a -> a) -> a -> Text -> a
foldrChunks Text -> a -> a
f a
z = Text -> a
go
where go :: Text -> a
go Text
Empty = a
z
go (Chunk Text
c Text
cs) = Text -> a -> a
f Text
c (Text -> a
go Text
cs)
{-# INLINE foldrChunks #-}
foldlChunks :: (a -> T.Text -> a) -> a -> Text -> a
foldlChunks :: forall a. (a -> Text -> a) -> a -> Text -> a
foldlChunks a -> Text -> a
f a
z = a -> Text -> a
go a
z
where go :: a -> Text -> a
go !a
a Text
Empty = a
a
go !a
a (Chunk Text
c Text
cs) = a -> Text -> a
go (a -> Text -> a
f a
a Text
c) Text
cs
{-# INLINE foldlChunks #-}
defaultChunkSize :: Int
defaultChunkSize :: Int
defaultChunkSize = Int
16384 forall a. Num a => a -> a -> a
- Int
chunkOverhead
{-# INLINE defaultChunkSize #-}
smallChunkSize :: Int
smallChunkSize :: Int
smallChunkSize = Int
128 forall a. Num a => a -> a -> a
- Int
chunkOverhead
{-# INLINE smallChunkSize #-}
chunkOverhead :: Int
chunkOverhead :: Int
chunkOverhead = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Int) forall a. UnsafeShift a => a -> Int -> a
`shiftL` Int
1
{-# INLINE chunkOverhead #-}