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