module Yi.String (isBlank,
chomp,
capitalize,
capitalizeFirst,
dropSpace,
fillText,
onLines,
mapLines,
lines',
unlines',
padLeft, padRight
) where
import Data.List.Split
import Data.List (isSuffixOf, intercalate)
import Data.Char (toUpper, toLower, isSpace, isAlphaNum)
capitalize :: String -> String
capitalize [] = []
capitalize (c:cs) = toUpper c : map toLower cs
capitalizeFirst :: String -> String
capitalizeFirst [] = []
capitalizeFirst (c:cs)
| isAlphaNum c = toUpper c : map toLower cs
| otherwise = c : capitalizeFirst cs
chomp :: String -> String -> String
chomp irs st
| irs `isSuffixOf` st
= let st' = reverse $ drop (length irs) (reverse st) in chomp irs st'
| otherwise = st
dropSpace :: String -> String
dropSpace = let f = reverse . dropWhile isSpace in f . f
isBlank :: String -> Bool
isBlank = all isSpace
fillText :: Int -> String -> [String]
fillText margin = map (unwords . reverse) . fill 0 [] . words
where fill _ acc [] = [acc]
fill n acc (w:ws)
| n + length w >= margin = acc : fill (length w) [w] ws
| otherwise = fill (n + 1 + length w) (w:acc) ws
unlines' :: [String] -> String
unlines' = intercalate "\n"
lines' :: String -> [String]
lines' = unintercalate "\n"
mapLines :: (String -> String) -> String -> String
mapLines transform = onLines (fmap transform)
onLines :: ([String] -> [String]) -> String -> String
onLines transform = unlines' . transform . lines'
padLeft, padRight :: Int -> String -> String
padLeft n [] = replicate n ' '
padLeft n (x:xs) = x : padLeft (n1) xs
padRight n = reverse . padLeft n . reverse