on master help sign in
Define and export toLazyTextWith.
05b6475Bryan O'Sullivan
Data/Text/Lazy/Builder.hs+15−7
@@ -18,6 +18,7 @@ module Data.Text.Lazy.Builder
18 ( -- * The Builder type
18 ( -- * The Builder type
19 Builder
19 Builder
20 , toLazyText
20 , toLazyText
21+ , toLazyTextWith
21
22
22 -- * Constructing Builders
23 -- * Constructing Builders
23 , singleton
24 , singleton
@@ -134,14 +135,21 @@ data Buffer s = Buffer {-# UNPACK #-} !(A.MArray s Word16)
134
135
135------------------------------------------------------------------------
136------------------------------------------------------------------------
136
137
137−-- | /O(n)./ Extract a lazy 'L.T' from a 'Builder'. The construction
138+-- | /O(n)./ Extract a lazy 'L.Text' from a 'Builder' with a default
138−-- work takes place if and when the relevant part of the lazy 'L.T' is
139+-- buffer size. The construction work takes place if and when the
139−-- demanded.
140+-- relevant part of the lazy 'L.Text' is demanded.
140−--
141toLazyText :: Builder -> L.Text
141toLazyText :: Builder -> L.Text
142−toLazyText m = L.fromChunks $ runST $ do
142+toLazyText = toLazyTextWith defaultChunkSize
143− buf <- newBuffer defaultChunkSize
143+
144− runBuilder (m `append` flush) (const (return [])) buf
144+-- | /O(n)./ Extract a lazy 'L.Text' from a 'Builder', using the given
145+-- size for the initial buffer. The construction work takes place if
146+-- and when the relevant part of the lazy 'L.Text' is demanded.
147+--
148+-- If the initial buffer is too small to hold all data, subsequent
149+-- buffers will be the default buffer size.
150+toLazyTextWith :: Int -> Builder -> L.Text
151+toLazyTextWith chunkSize m = L.fromChunks . runST $
152+ newBuffer chunkSize >>= runBuilder (m `append` flush) (const (return []))
145
153
146-- | /O(1)./ Pop the 'S.Text' we have constructed so far, if any,
154-- | /O(1)./ Pop the 'S.Text' we have constructed so far, if any,
147-- yielding a new chunk in the result lazy 'L.Text'.
155-- yielding a new chunk in the result lazy 'L.Text'.