@@ -64,6 +64,11 @@ module Data.Text.Lazy
6464 , toLower
6565 , toUpper
6666
67+ -- ** Justification
68+ , justifyLeft
69+ , justifyRight
70+ , center
71+
6772 -- * Folds
6873 , foldl
6974 , foldl'
@@ -156,7 +161,7 @@ module Data.Text.Lazy
156161import Prelude (Char, Bool(..), Int, Maybe(..), String,
157162 Eq(..), Ord(..), Read(..), Show(..),
158163 (&&), (+), (-), (.), ($), (++),
159− flip, fromIntegral, not, otherwise)
164+ div, flip, fromIntegral, not, otherwise)
160165import qualified Prelude as P
161166import Data.Int (Int64)
162167import qualified Data.List as L
@@ -365,6 +370,51 @@ intersperse :: Char -> Text -> Text
365370intersperse c t = unstream (S.intersperse c (stream t))
366371{-# INLINE intersperse #-}
367372
373+-- | /O(n)/ Left-justify a string to the given length, using the
374+-- specified fill character on the right. Subject to fusion. Examples:
375+--
376+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
377+-- > justifyLeft 3 'x' "foobar" == "foobar"
378+justifyLeft :: Int64 -> Char -> Text -> Text
379+justifyLeft k c t
380+ | len >= k = t
381+ | otherwise = t `append` replicate (k-len) c
382+ where len = length t
383+{-# INLINE [1] justifyLeft #-}
384+
385+{-# RULES
386+"TEXT justifyLeft -> fused" [~1] forall k c t.
387+ justifyLeft k c t = unstream (S.justifyLeftI k c (stream t))
388+"TEXT justifyLeft -> unfused" [1] forall k c t.
389+ unstream (S.justifyLeftI k c (stream t)) = justifyLeft k c t
390+ #-}
391+
392+-- | /O(n)/ Right-justify a string to the given length, using the
393+-- specified fill character on the left. Examples:
394+--
395+-- > justifyRight 7 'x' "bar" == "xxxxbar"
396+-- > justifyRight 3 'x' "foobar" == "foobar"
397+justifyRight :: Int64 -> Char -> Text -> Text
398+justifyRight k c t
399+ | len >= k = t
400+ | otherwise = replicate (k-len) c `append` t
401+ where len = length t
402+{-# INLINE justifyRight #-}
403+
404+-- | /O(n)/ Center a string to the given length, using the
405+-- specified fill character on either side. Examples:
406+--
407+-- > center 8 'x' "HS" = "xxxHSxxx"
408+center :: Int64 -> Char -> Text -> Text
409+center k c t
410+ | len >= k = t
411+ | otherwise = replicate l c `append` t `append` replicate r c
412+ where len = length t
413+ d = k - len
414+ r = d `div` 2
415+ l = d - r
416+{-# INLINE center #-}
417+
368418-- | /O(n)/ The 'transpose' function transposes the rows and columns
369419-- of its 'Text' argument. Note that this function uses 'pack',
370420-- 'unpack', and the list version of transpose, and is thus not very
@@ -565,8 +615,8 @@ mapAccumR f s t = case uncons t of
565615
566616-- | /O(n)/ 'replicate' @n@ @c@ is a 'Text' of length @n@ with @c@ the
567617-- value of every element.
568−replicate :: Int -> Char -> Text
569−replicate n c = unstream (S.replicate n c)
618+replicate :: Int64 -> Char -> Text
619+replicate n c = unstream (S.replicateI n c)
570620{-# INLINE replicate #-}
571621
572622-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'