module Data.Text.Lazy.Internal
(
Text(..)
, chunk
, empty
, foldrChunks
, foldlChunks
, invariant
, checkInvariant
, showStructure
, defaultChunkSize
, smallChunkSize
, chunkOverhead
) where
import qualified Data.Text.Internal as T
import Data.Text ()
import Data.Typeable (Typeable)
import Data.Word (Word16)
import Foreign.Storable (sizeOf)
data Text = Empty
| Chunk !T.Text Text
deriving (Typeable)
invariant :: Text -> Bool
invariant Empty = True
invariant (Chunk (T.Text _ _ len) cs) = len > 0 && invariant cs
showStructure :: Text -> String
showStructure Empty = "Empty"
showStructure (Chunk t Empty) = "Chunk " ++ show t ++ " Empty"
showStructure (Chunk t ts) =
"Chunk " ++ show t ++ " (" ++ showStructure ts ++ ")"
checkInvariant :: Text -> Text
checkInvariant Empty = Empty
checkInvariant (Chunk c@(T.Text _ _ len) cs)
| len > 0 = Chunk c (checkInvariant cs)
| otherwise = error $ "Data.Text.Lazy: invariant violation: "
++ showStructure (Chunk c cs)
chunk :: T.Text -> Text -> Text
chunk t@(T.Text _ _ len) ts | len == 0 = ts
| otherwise = Chunk t ts
empty :: Text
empty = Empty
foldrChunks :: (T.Text -> a -> a) -> a -> Text -> a
foldrChunks f z = go
where go Empty = z
go (Chunk c cs) = f c (go cs)
foldlChunks :: (a -> T.Text -> a) -> a -> Text -> a
foldlChunks f z = go z
where go !a Empty = a
go !a (Chunk c cs) = go (f a c) cs
defaultChunkSize :: Int
defaultChunkSize = 32 * k chunkOverhead
where k = 1024 `div` sizeOf (undefined :: Word16)
smallChunkSize :: Int
smallChunkSize = 4 * k chunkOverhead
where k = 1024 `div` sizeOf (undefined :: Word16)
chunkOverhead :: Int
chunkOverhead = 2 * sizeOf (undefined :: Int)