@@ -49,7 +49,7 @@ module Data.Text.Lazy
4949 , tail
5050 , init
5151 , null
52− -- , length
52+ , length
5353
5454 -- * Transformations
5555 -- , map
@@ -150,8 +150,9 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
150150 Eq(..), Ord(..), (++),
151151 Read(..), Show(..),
152152 (&&), (||), (+), (-), (.), ($),
153− not, return, otherwise)
153+ fromIntegral, not, return, otherwise)
154154import qualified Prelude as P
155+import Data.Int (Int64)
155156import Data.String (IsString(..))
156157import qualified Data.Text as T
157158import qualified Data.Text.Fusion as S
@@ -295,6 +296,18 @@ null _ = False
295296 S.null (stream t) = null t
296297 #-}
297298
299+length :: Text -> Int64
300+length = foldlChunks go 0
301+ where go l t = l + fromIntegral (T.length t)
302+{-# INLINE [1] length #-}
303+
304+{-# RULES
305+"LAZY TEXT length -> fused" [~1] forall t.
306+ length t = S.length64 (stream t)
307+"LAZY TEXT length -> unfused" [1] forall t.
308+ S.length64 (stream t) = length t
309+ #-}
310+
298311-- | /O(1)/ Returns the last character of a 'Text', which must be
299312-- non-empty. Subject to array fusion.
300313last :: Text -> Char