Bryan O'Sullivan avatar Bryan O'Sullivan committed 57e1cec

Drop replicateChar from the public API.

Comments (0)

Files changed (2)

 
     -- ** Generation and unfolding
     , replicate
-    , replicateChar
     , unfoldr
     , unfoldrN
 
 -- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
 -- @t@ repeated @n@ times. Subject to fusion.
 replicate :: Int -> Text -> Text
-replicate n t = unstream (S.replicateI (fromIntegral n) (S.stream t))
+replicate n t
+    | isSingleton t = replicateChar n (unsafeHead t)
+    | otherwise     = unstream (S.replicateI (fromIntegral n) (S.stream t))
 {-# INLINE [1] replicate #-}
 
 {-# RULES
 -- value of every element. Subject to fusion.
 replicateChar :: Int -> Char -> Text
 replicateChar n c = unstream (S.replicateCharI n c)
+{-# INLINE replicateChar #-}
 
 -- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
 -- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a

Data/Text/Lazy.hs

 
     -- ** Generation and unfolding
     , replicate
-    , replicateChar
     , unfoldr
     , unfoldrN
 
     S.null (stream t) = null t
  #-}
 
+-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
+-- Subject to fusion.
+isSingleton :: Text -> Bool
+isSingleton = S.isSingleton . stream
+{-# INLINE isSingleton #-}
+
 -- | /O(1)/ Returns the last character of a 'Text', which must be
 -- non-empty.  Subject to array fusion.
 last :: Text -> Char
 -- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
 -- @t@ repeated @n@ times. Subject to fusion.
 replicate :: Int64 -> Text -> Text
-replicate n t = unstream (S.replicateI n (S.stream t))
+replicate n t
+    | isSingleton t = replicateChar n (head t)
+    | otherwise     = unstream (S.replicateI n (S.stream t))
 {-# INLINE replicate #-}
 
 -- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
 -- value of every element. Subject to fusion.
 replicateChar :: Int64 -> Char -> Text
 replicateChar n c = unstream (S.replicateCharI n c)
+{-# INLINE replicateChar #-}
 
 {-# RULES
 "LAZY TEXT replicate/singleton -> replicateChar" [~1] forall n c.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.