module Control.Monad.Ox.Text.Lazy
( prefix
, suffix
, substr
, shape
, pack
) where
import qualified Data.Char as C
import qualified Data.Text.Lazy as L
prefix :: Int -> L.Text -> Maybe L.Text
prefix k xs
| k > 0 && k <= n = Just $ takeL k xs
| k <= 0 && n + k > 0 = Just $ takeL (n + k) xs
| otherwise = Nothing
where
n = lengthL xs
suffix :: Int -> L.Text -> Maybe L.Text
suffix k xs
| k > 0 && k <= n = Just . takeR k $ xs
| k <= 0 && n + k > 0 = Just . takeR (n + k) $ xs
| otherwise = Nothing
where
takeR i = L.reverse . takeL i . L.reverse
n = lengthL xs
substr :: Int -> L.Text -> [L.Text]
substr k xs
| lengthL x < k = []
| otherwise = x : substr k (L.tail xs)
where
x = takeL k xs
shape :: L.Text -> L.Text
shape = L.map translate
where
translate char
| C.isLower char = 'l'
| C.isUpper char = 'u'
| C.isDigit char = 'd'
| otherwise = 'x'
pack :: L.Text -> L.Text
pack = L.pack . map L.head . L.group
lengthL :: L.Text -> Int
lengthL = fromIntegral . L.length
takeL :: Int -> L.Text -> L.Text
takeL = L.take . fromIntegral