2
2
{-# LANGUAGE BangPatterns, MagicHash, CPP, TypeFamilies #-}
3
3
{-# LANGUAGE Trustworthy #-}
4
4
{-# LANGUAGE TemplateHaskellQuotes #-}
5
+ {-# LANGUAGE LambdaCase #-}
5
6
6
7
-- |
7
8
-- Module : Data.Text.Lazy
@@ -199,7 +200,7 @@ module Data.Text.Lazy
199
200
200
201
import Prelude (Char , Bool (.. ), Maybe (.. ), String ,
201
202
Eq (.. ), Ord (.. ), Ordering (.. ), Read (.. ), Show (.. ),
202
- (&&) , (||) , ( +) , (-) , (.) , ($) , (++) ,
203
+ (&&) , (+) , (-) , (.) , ($) , (++) ,
203
204
error , flip , fmap , fromIntegral , not , otherwise , quot )
204
205
import qualified Prelude as P
205
206
import Control.DeepSeq (NFData (.. ))
@@ -221,7 +222,7 @@ import qualified Data.Text.Internal.Lazy.Fusion as S
221
222
import Data.Text.Internal.Fusion.Types (PairS (.. ))
222
223
import Data.Text.Internal.Lazy.Fusion (stream , unstream )
223
224
import Data.Text.Internal.Lazy (Text (.. ), chunk , empty , foldlChunks ,
224
- foldrChunks , smallChunkSize , equal )
225
+ foldrChunks , smallChunkSize , defaultChunkSize , equal )
225
226
import Data.Text.Internal (firstf , safe , text )
226
227
import Data.Text.Lazy.Encoding (decodeUtf8' , encodeUtf8 )
227
228
import Data.Text.Internal.Lazy.Search (indices )
@@ -591,7 +592,7 @@ intersperse c t = unstream (S.intersperse (safe c) (stream t))
591
592
justifyLeft :: Int64 -> Char -> Text -> Text
592
593
justifyLeft k c t
593
594
| len >= k = t
594
- | otherwise = t `append` replicateChar (k- len) c
595
+ | otherwise = t `append` replicateChunk (k- len) ( T. singleton c)
595
596
where len = length t
596
597
{-# INLINE [1] justifyLeft #-}
597
598
@@ -606,7 +607,7 @@ justifyLeft k c t
606
607
justifyRight :: Int64 -> Char -> Text -> Text
607
608
justifyRight k c t
608
609
| len >= k = t
609
- | otherwise = replicateChar (k- len) c `append` t
610
+ | otherwise = replicateChunk (k- len) ( T. singleton c) `append` t
610
611
where len = length t
611
612
{-# INLINE justifyRight #-}
612
613
@@ -620,7 +621,7 @@ justifyRight k c t
620
621
center :: Int64 -> Char -> Text -> Text
621
622
center k c t
622
623
| len >= k = t
623
- | otherwise = replicateChar l c `append` t `append` replicateChar r c
624
+ | otherwise = replicateChunk l ( T. singleton c) `append` t `append` replicateChunk r ( T. singleton c)
624
625
where len = length t
625
626
d = k - len
626
627
r = d `quot` 2
@@ -910,14 +911,28 @@ repeat c = let t = Chunk (T.replicate smallChunkSize (T.singleton c)) t
910
911
-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
911
912
-- @t@ repeated @n@ times.
912
913
replicate :: Int64 -> Text -> Text
913
- replicate n t
914
- | null t || n <= 0 = empty
915
- | isSingleton t = replicateChar n (head t)
916
- | otherwise = concat (rep 0 )
917
- where rep ! i | i >= n = []
918
- | otherwise = t : rep (i+ 1 )
914
+ replicate n
915
+ | n <= 0 = P. const Empty
916
+ | otherwise = \ case
917
+ Empty -> Empty
918
+ Chunk t Empty -> replicateChunk n t
919
+ t -> concat (rep n)
920
+ where
921
+ rep 0 = []
922
+ rep i = t : rep (i - 1 )
919
923
{-# INLINE [1] replicate #-}
920
924
925
+ replicateChunk :: Int64 -> T. Text -> Text
926
+ replicateChunk ! n ! t@ (T. Text _ _ len)
927
+ | n <= 0 = Empty
928
+ | otherwise = Chunk headChunk $ P. foldr Chunk Empty (L. genericReplicate q normalChunk)
929
+ where
930
+ perChunk = defaultChunkSize `quot` len
931
+ normalChunk = T. replicate perChunk t
932
+ (q, r) = n `P.quotRem` intToInt64 perChunk
933
+ headChunk = T. replicate (int64ToInt r) t
934
+ {-# INLINE replicateChunk #-}
935
+
921
936
-- | 'cycle' ties a finite, non-empty 'Text' into a circular one, or
922
937
-- equivalently, the infinite repetition of the original 'Text'.
923
938
--
@@ -937,17 +952,6 @@ iterate :: (Char -> Char) -> Char -> Text
937
952
iterate f c = let t c' = Chunk (T. singleton c') (t (f c'))
938
953
in t c
939
954
940
- -- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
941
- -- value of every element.
942
- replicateChar :: Int64 -> Char -> Text
943
- replicateChar n c = unstream (S. replicateCharI n (safe c))
944
- {-# INLINE replicateChar #-}
945
-
946
- {-# RULES
947
- "LAZY TEXT replicate/singleton -> replicateChar" [~1] forall n c.
948
- replicate n (singleton c) = replicateChar n c
949
- #-}
950
-
951
955
-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
952
956
-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
953
957
-- 'Text' from a seed value. The function takes the element and
0 commit comments