{-# LANGUAGE Safe #-}
module System.Path.WildMatch (
wildCheckCase,
wildToRegex)
where
import Data.String.Utils ( escapeRe )
import Text.Regex ( matchRegex, mkRegex )
wildToRegex :: String -> String
wildToRegex :: String -> String
wildToRegex String
i = String -> String
convwild String
i String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"$"
wildCheckCase :: String
-> String
-> Bool
wildCheckCase :: String -> String -> Bool
wildCheckCase String
patt String
name =
case Regex -> String -> Maybe [String]
matchRegex (String -> Regex
mkRegex (String -> Regex) -> String -> Regex
forall a b. (a -> b) -> a -> b
$ String
"^" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
wildToRegex String
patt) String
name of
Maybe [String]
Nothing -> Bool
False
Just [String]
_ -> Bool
True
convwild :: String -> String
convwild :: String -> String
convwild [] = []
convwild (Char
'*':String
xs) = String
".*" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convwild String
xs
convwild (Char
'?':String
xs) = String
"." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convwild String
xs
convwild (Char
'[':Char
'!':String
xs) = String
"[^" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convpat String
xs
convwild (Char
'[':String
xs) = Char
'[' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
convpat String
xs
convwild (Char
'.':String
xs) = String
"\\." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convwild String
xs
convwild (Char
x:String
xs) = String -> String
escapeRe [Char
x] String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convwild String
xs
convpat :: String -> String
convpat :: String -> String
convpat (Char
'\\':String
xs) = String
"\\\\" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
convpat String
xs
convpat (Char
']':String
xs) = Char
']' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
convwild String
xs
convpat (Char
x:String
xs) = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
convpat String
xs
convpat [] = []