module Util.String where
import Data.Char
import Data.List
import Text.Printf ( printf, PrintfType )
trim :: String -> String
trim = dropWhileEnd isSpace . dropWhile isSpace
split :: Eq a => a -> [a] -> [[a]]
split _ [] = []
split delimiter string =
let (a,b) = span (/= delimiter) string
in a:split delimiter (drop 1 b)
wrap :: String -> String -> String -> [String] -> String
wrap before between after elems =
before ++ intercalate between elems ++ after
strToLower :: String -> String
strToLower = map toLower
strToUpper :: String -> String
strToUpper = map toUpper
splitOn :: Eq a => [a] -> [a] -> ([a],[a])
splitOn = flip splitOn' []
splitOn' :: Eq a => [a] -> [a] -> [a] -> ([a],[a])
splitOn' [] zs _ = (reverse zs, [])
splitOn' yss@(y:ys) zs xs | xs `isPrefixOf` yss = (reverse zs, drop (length xs) yss)
| otherwise = splitOn' ys (y:zs) xs
breaksOn :: (a -> Bool) -> [a] -> [[a]]
breaksOn _ [] = []
breaksOn p xs | (a,ys) <- break p xs = a : breaksOn p (case ys of
[] -> []
_:zs -> zs)
indent :: String -> String
indent = unlines . map ('\t':) . lines
replace :: String -> String -> String -> String
replace _ _ [] = []
replace x y oz@(z:zs) | Just r <- stripPrefix x oz = y ++ replace x y r
| otherwise = z : replace x y zs
normalize :: String -> String
normalize xs = [ toLower x | x <- xs, isAlphaNum x ]
percentage :: Int -> Double -> String
percentage i = printf ("%." ++ show i ++ "f%%") . (* 100)
(//) :: (Real a, Real b) => a -> b -> Double
a // b = realToFrac a / realToFrac b