@@ -101,7 +101,6 @@ module Data.Text.Lazy
101101
102102 -- ** Generation and unfolding
103103 , replicate
104− , replicateChar
105104 , unfoldr
106105 , unfoldrN
107106
@@ -330,6 +329,12 @@ null _ = False
330329 S.null (stream t) = null t
331330 #-}
332331
332+-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
333+-- Subject to fusion.
334+isSingleton :: Text -> Bool
335+isSingleton = S.isSingleton . stream
336+{-# INLINE isSingleton #-}
337+
333338-- | /O(1)/ Returns the last character of a 'Text', which must be
334339-- non-empty. Subject to array fusion.
335340last :: Text -> Char
@@ -631,13 +636,16 @@ mapAccumR f s t = case uncons t of
631636-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
632637-- @t@ repeated @n@ times. Subject to fusion.
633638replicate :: Int64 -> Text -> Text
634−replicate n t = unstream (S.replicateI n (S.stream t))
639+replicate n t
640+ | isSingleton t = replicateChar n (head t)
641+ | otherwise = unstream (S.replicateI n (S.stream t))
635642{-# INLINE replicate #-}
636643
637644-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
638645-- value of every element. Subject to fusion.
639646replicateChar :: Int64 -> Char -> Text
640647replicateChar n c = unstream (S.replicateCharI n c)
648+{-# INLINE replicateChar #-}
641649
642650{-# RULES
643651"LAZY TEXT replicate/singleton -> replicateChar" [~1] forall n c.