@@ -21,9 +21,12 @@ module Data.Text.Lazy.Internal
2121 , foldrChunks
2222 , foldlChunks
2323 -- * Data type invariant and abstraction functions
24− , invariant
25− , checkInvariant
24+
25+ -- $invariant
26+ , strictInvariant
27+ , lazyInvariant
2628 , showStructure
29+
2730 -- * Chunk allocation sizes
2831 , defaultChunkSize
2932 , smallChunkSize
@@ -40,27 +43,35 @@ data Text = Empty
4043 | Chunk {-# UNPACK #-} !T.Text Text
4144 deriving (Typeable)
4245
43−-- | The data type invariant: Every 'Text' is either 'Empty' or
46+-- $invariant
47+--
48+-- The data type invariant for lazy 'Text': Every 'Text' is either 'Empty' or
4449-- consists of non-null 'T.Text's. All functions must preserve this,
4550-- and the QC properties must check this.
46−invariant :: Text -> Bool
47−invariant Empty = True
48−invariant (Chunk (T.Text _ _ len) cs) = len > 0 && invariant cs
4951
52+-- | Check the invariant strictly.
53+strictInvariant :: Text -> Bool
54+strictInvariant Empty = True
55+strictInvariant x@(Chunk (T.Text _ _ len) cs)
56+ | len > 0 = strictInvariant cs
57+ | otherwise = error $ "Data.Text.Lazy: invariant violation: "
58+ ++ showStructure x
59+
60+-- | Check the invariant lazily.
61+lazyInvariant :: Text -> Text
62+lazyInvariant Empty = Empty
63+lazyInvariant x@(Chunk c@(T.Text _ _ len) cs)
64+ | len > 0 = Chunk c (lazyInvariant cs)
65+ | otherwise = error $ "Data.Text.Lazy: invariant violation: "
66+ ++ showStructure x
67+
68+-- | Display the internal structure of a lazy 'Text'.
5069showStructure :: Text -> String
5170showStructure Empty = "Empty"
5271showStructure (Chunk t Empty) = "Chunk " ++ show t ++ " Empty"
5372showStructure (Chunk t ts) =
5473 "Chunk " ++ show t ++ " (" ++ showStructure ts ++ ")"
5574
56−-- | In a form that checks the invariant lazily.
57−checkInvariant :: Text -> Text
58−checkInvariant Empty = Empty
59−checkInvariant (Chunk c@(T.Text _ _ len) cs)
60− | len > 0 = Chunk c (checkInvariant cs)
61− | otherwise = error $ "Data.Text.Lazy: invariant violation: "
62− ++ showStructure (Chunk c cs)
63−
6475-- | Smart constructor for 'Chunk'. Guarantees the data type invariant.
6576chunk :: T.Text -> Text -> Text
6677{-# INLINE chunk #-}