1{-# LANGUAGE BangPatterns, DeriveDataTypeable #-}
3-- Module : Data.Text.Lazy.Internal
4-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
7-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
9-- Stability : experimental
12-- A module containing private 'Text' internals. This exposes the
13-- 'Text' representation and low level construction functions.
14-- Modules which extend the 'Text' system may need to use this module.
16-- You should not use this module unless you are determined to monkey
17-- with the internals, as the functions here do just about nothing to
18-- preserve data invariants. You have been warned!
20module Data.Text.Lazy.Internal
27 -- * Data type invariant and abstraction functions
34 -- * Chunk allocation sizes
41import Data.Text.UnsafeShift (shiftL)
42import Data.Typeable (Typeable)
43import Foreign.Storable (sizeOf)
44import qualified Data.Text.Internal as T
47 | Chunk {-# UNPACK #-} !T.Text Text
52-- The data type invariant for lazy 'Text': Every 'Text' is either 'Empty' or
53-- consists of non-null 'T.Text's. All functions must preserve this,
54-- and the QC properties must check this.
56-- | Check the invariant strictly.
57strictInvariant :: Text -> Bool
58strictInvariant Empty = True
59strictInvariant x@(Chunk (T.Text _ _ len) cs)
60 | len > 0 = strictInvariant cs
61 | otherwise = error $ "Data.Text.Lazy: invariant violation: "
64-- | Check the invariant lazily.
65lazyInvariant :: Text -> Text
66lazyInvariant Empty = Empty
67lazyInvariant x@(Chunk c@(T.Text _ _ len) cs)
68 | len > 0 = Chunk c (lazyInvariant cs)
69 | otherwise = error $ "Data.Text.Lazy: invariant violation: "
72-- | Display the internal structure of a lazy 'Text'.
73showStructure :: Text -> String
74showStructure Empty = "Empty"
75showStructure (Chunk t Empty) = "Chunk " ++ show t ++ " Empty"
76showStructure (Chunk t ts) =
77 "Chunk " ++ show t ++ " (" ++ showStructure ts ++ ")"
79-- | Smart constructor for 'Chunk'. Guarantees the data type invariant.
80chunk :: T.Text -> Text -> Text
82chunk t@(T.Text _ _ len) ts | len == 0 = ts
83 | otherwise = Chunk t ts
85-- | Smart constructor for 'Empty'.
87{-# INLINE [0] empty #-}
90-- | Consume the chunks of a lazy 'Text' with a natural right fold.
91foldrChunks :: (T.Text -> a -> a) -> a -> Text -> a
94 go (Chunk c cs) = f c (go cs)
95{-# INLINE foldrChunks #-}
97-- | Consume the chunks of a lazy 'Text' with a strict, tail-recursive,
98-- accumulating left fold.
99foldlChunks :: (a -> T.Text -> a) -> a -> Text -> a
100foldlChunks f z = go z
101 where go !a Empty = a
102 go !a (Chunk c cs) = go (f a c) cs
103{-# INLINE foldlChunks #-}
105-- | Currently set to 16 KiB, less the memory management overhead.
106defaultChunkSize :: Int
107defaultChunkSize = 16384 - chunkOverhead
108{-# INLINE defaultChunkSize #-}
110-- | Currently set to 128 bytes, less the memory management overhead.
112smallChunkSize = 128 - chunkOverhead
113{-# INLINE smallChunkSize #-}
115-- | The memory management overhead. Currently this is tuned for GHC only.
117chunkOverhead = sizeOf (undefined :: Int) `shiftL` 1
118{-# INLINE chunkOverhead #-}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list