{-# LANGUAGE Trustworthy #-}
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 (String -> String) -> (String -> String) -> String -> String
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 Char -> String -> Bool
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 = String -> String
forall a. [a] -> [a]
reverse (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
lstrip (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
forall a. [a] -> [a]
reverse
splitWs :: String -> [String]
splitWs :: String -> [String]
splitWs = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (\String
x -> String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= []) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
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 Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'\'', Char
'`'] = Char
x Char -> String -> String
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 Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'<', Char
'>']
= Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
escapeRe String
xs
| Bool
otherwise = Char
'\\' Char -> String -> String
forall a. a -> [a] -> [a]
: Char
x Char -> String -> String
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 = ((a, String) -> a) -> Maybe (a, String) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, String) -> a
forall a b. (a, b) -> a
fst (Maybe (a, String) -> Maybe a)
-> (String -> Maybe (a, String)) -> String -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(a, String)] -> Maybe (a, String)
forall a. [a] -> Maybe a
listToMaybe ([(a, String)] -> Maybe (a, String))
-> (String -> [(a, String)]) -> String -> Maybe (a, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [(a, String)]
forall a. Read a => ReadS a
reads