on master help sign in
Rename invariant checks to be more descriptive.
0321489Bryan O'Sullivan
Data/Text/Lazy/Internal.hs+25−14
@@ -21,9 +21,12 @@ module Data.Text.Lazy.Internal
21 , foldrChunks
21 , foldrChunks
22 , foldlChunks
22 , foldlChunks
23 -- * Data type invariant and abstraction functions
23 -- * Data type invariant and abstraction functions
24+
24− , invariant
25+ -- $invariant
25− , checkInvariant
26+ , strictInvariant
27+ , lazyInvariant
26 , showStructure
28 , showStructure
29+
27 -- * Chunk allocation sizes
30 -- * Chunk allocation sizes
28 , defaultChunkSize
31 , defaultChunkSize
29 , smallChunkSize
32 , smallChunkSize
@@ -40,27 +43,35 @@ data Text = Empty
40 | Chunk {-# UNPACK #-} !T.Text Text
43 | Chunk {-# UNPACK #-} !T.Text Text
41 deriving (Typeable)
44 deriving (Typeable)
42
45
46+-- $invariant
47+--
43−-- | The data type invariant: Every 'Text' is either 'Empty' or
48+-- The data type invariant for lazy 'Text': Every 'Text' is either 'Empty' or
44-- consists of non-null 'T.Text's. All functions must preserve this,
49-- consists of non-null 'T.Text's. All functions must preserve this,
45-- and the QC properties must check this.
50-- 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
49
51
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'.
50showStructure :: Text -> String
69showStructure :: Text -> String
51showStructure Empty = "Empty"
70showStructure Empty = "Empty"
52showStructure (Chunk t Empty) = "Chunk " ++ show t ++ " Empty"
71showStructure (Chunk t Empty) = "Chunk " ++ show t ++ " Empty"
53showStructure (Chunk t ts) =
72showStructure (Chunk t ts) =
54 "Chunk " ++ show t ++ " (" ++ showStructure ts ++ ")"
73 "Chunk " ++ show t ++ " (" ++ showStructure ts ++ ")"
55
74
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−
64-- | Smart constructor for 'Chunk'. Guarantees the data type invariant.
75-- | Smart constructor for 'Chunk'. Guarantees the data type invariant.
65chunk :: T.Text -> Text -> Text
76chunk :: T.Text -> Text -> Text
66{-# INLINE chunk #-}
77{-# INLINE chunk #-}