{-# LANGUAGE Safe #-}
module Data.String.Utils
(
strip, lstrip, rstrip,
startswith, endswith,
join, split, splitWs, replace, escapeRe,
maybeRead
) where
import Data.Char (isAlpha, isAscii, isDigit)
import Data.List.Utils (endswith, join, replace, split, startswith)
import Data.Maybe (listToMaybe)
import Text.Regex (mkRegex, splitRegex)
wschars :: String
wschars :: String
wschars = String
" \t\r\n"
strip :: String -> String
strip :: String -> String
strip = String -> String
lstrip forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
rstrip
lstrip :: String -> String
lstrip :: String -> String
lstrip String
s = case String
s of
[] -> []
(Char
x:String
xs) -> if forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Char
x String
wschars
then String -> String
lstrip String
xs
else String
s
rstrip :: String -> String
rstrip :: String -> String
rstrip = forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
lstrip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
splitWs :: String -> [String]
splitWs :: String -> [String]
splitWs = forall a. (a -> Bool) -> [a] -> [a]
filter (\String
x -> String
x forall a. Eq a => a -> a -> Bool
/= []) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Regex -> String -> [String]
splitRegex (String -> Regex
mkRegex String
"[ \t\n\r\v\f]+")
escapeRe :: String -> String
escapeRe :: String -> String
escapeRe [] = []
escapeRe (Char
x:String
xs)
| Char
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'\'', Char
'`'] = Char
x forall a. a -> [a] -> [a]
: String -> String
escapeRe String
xs
| Char -> Bool
isDigit Char
x Bool -> Bool -> Bool
|| (Char -> Bool
isAscii Char
x Bool -> Bool -> Bool
&& Char -> Bool
isAlpha Char
x) Bool -> Bool -> Bool
|| Char
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'<', Char
'>']
= Char
x forall a. a -> [a] -> [a]
: String -> String
escapeRe String
xs
| Bool
otherwise = Char
'\\' forall a. a -> [a] -> [a]
: Char
x forall a. a -> [a] -> [a]
: String -> String
escapeRe String
xs
maybeRead :: Read a => String -> Maybe a
maybeRead :: forall a. Read a => String -> Maybe a
maybeRead = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => ReadS a
reads