2014-02-21 18:34:34 -04:00
|
|
|
{- file globbing
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-02-21 18:34:34 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2014-02-21 18:34:34 -04:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.Glob (
|
|
|
|
Glob,
|
|
|
|
GlobCase(..),
|
|
|
|
compileGlob,
|
|
|
|
matchGlob
|
|
|
|
) where
|
|
|
|
|
2016-11-15 21:29:54 -04:00
|
|
|
import Utility.Exception
|
|
|
|
|
2014-02-21 18:34:34 -04:00
|
|
|
import System.Path.WildMatch
|
|
|
|
|
2015-05-30 02:03:09 -04:00
|
|
|
import "regex-tdfa" Text.Regex.TDFA
|
|
|
|
import "regex-tdfa" Text.Regex.TDFA.String
|
2014-02-21 18:34:34 -04:00
|
|
|
|
|
|
|
newtype Glob = Glob Regex
|
|
|
|
|
|
|
|
data GlobCase = CaseSensative | CaseInsensative
|
|
|
|
|
|
|
|
{- Compiles a glob to a regex, that can be repeatedly used. -}
|
|
|
|
compileGlob :: String -> GlobCase -> Glob
|
|
|
|
compileGlob glob globcase = Glob $
|
|
|
|
case compile (defaultCompOpt {caseSensitive = casesentitive}) defaultExecOpt regex of
|
|
|
|
Right r -> r
|
2016-11-15 21:29:54 -04:00
|
|
|
Left _ -> giveup $ "failed to compile regex: " ++ regex
|
2014-02-21 18:34:34 -04:00
|
|
|
where
|
|
|
|
regex = '^':wildToRegex glob
|
|
|
|
casesentitive = case globcase of
|
|
|
|
CaseSensative -> True
|
|
|
|
CaseInsensative -> False
|
|
|
|
|
|
|
|
matchGlob :: Glob -> String -> Bool
|
|
|
|
matchGlob (Glob regex) val =
|
|
|
|
case execute regex val of
|
|
|
|
Right (Just _) -> True
|
|
|
|
_ -> False
|