@@ -56,12 +56,17 @@ module Data.Text
5656 , reverse
5757 , replace
5858
59− -- * Case conversion
59+ -- ** Case conversion
6060 -- $case
6161 , toCaseFold
6262 , toLower
6363 , toUpper
6464
65+ -- ** Justification
66+ , justifyLeft
67+ , justifyRight
68+ , center
69+
6570 -- * Folds
6671 , foldl
6772 , foldl'
@@ -163,7 +168,7 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
163168 Eq(..), Ord(..), (++),
164169 Read(..), Show(..),
165170 (&&), (||), (+), (-), (.), ($),
166− not, return, otherwise)
171+ div, not, return, otherwise)
167172import Control.Exception (assert)
168173import Data.Char (isSpace)
169174import Control.Monad.ST (ST)
@@ -452,6 +457,52 @@ toUpper :: Text -> Text
452457toUpper t = unstream (S.toUpper (stream t))
453458{-# INLINE toUpper #-}
454459
460+-- | /O(n)/ Left-justify a string to the given length, using the
461+-- specified fill character on the right. This function is subject to
462+-- array fusion. Examples:
463+--
464+-- > justifyLeft 7 'x' "foo" == "fooxxxx"
465+-- > justifyLeft 3 'x' "foobar" == "foobar"
466+justifyLeft :: Int -> Char -> Text -> Text
467+justifyLeft k c t
468+ | len >= k = t
469+ | otherwise = t `append` replicate (k-len) c
470+ where len = length t
471+{-# INLINE [1] justifyLeft #-}
472+
473+{-# RULES
474+"TEXT justifyLeft -> fused" [~1] forall k c t.
475+ justifyLeft k c t = unstream (S.justifyLeft k c (stream t))
476+"TEXT justifyLeft -> unfused" [1] forall k c t.
477+ unstream (S.justifyLeft k c (stream t)) = justifyLeft k c t
478+ #-}
479+
480+-- | /O(n)/ Right-justify a string to the given length, using the
481+-- specified fill character on the left. Examples:
482+--
483+-- > justifyRight 7 'x' "bar" == "xxxxbar"
484+-- > justifyRight 3 'x' "foobar" == "foobar"
485+justifyRight :: Int -> Char -> Text -> Text
486+justifyRight k c t
487+ | len >= k = t
488+ | otherwise = replicate (k-len) c `append` t
489+ where len = length t
490+{-# INLINE justifyRight #-}
491+
492+-- | /O(n)/ Center a string to the given length, using the
493+-- specified fill character on either side. Examples:
494+--
495+-- > center 8 'x' "HS" = "xxxHSxxx"
496+center :: Int -> Char -> Text -> Text
497+center k c t
498+ | len >= k = t
499+ | otherwise = replicate l c `append` t `append` replicate r c
500+ where len = length t
501+ d = k - len
502+ r = d `div` 2
503+ l = d - r
504+{-# INLINE center #-}
505+
455506-- | /O(n)/ The 'transpose' function transposes the rows and columns
456507-- of its 'Text' argument. Note that this function uses 'pack',
457508-- 'unpack', and the list version of transpose, and is thus not very