2014-02-21 22:34:34 +00:00
|
|
|
{- file globbing
|
|
|
|
-
|
|
|
|
- This uses TDFA when available, with a fallback to regex-compat.
|
|
|
|
- TDFA is less buggy in its support for non-unicode characters.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-02-21 22:34:34 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2014-02-21 22:34:34 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.Glob (
|
|
|
|
Glob,
|
|
|
|
GlobCase(..),
|
|
|
|
compileGlob,
|
|
|
|
matchGlob
|
|
|
|
) where
|
|
|
|
|
|
|
|
import System.Path.WildMatch
|
|
|
|
|
|
|
|
#ifdef WITH_TDFA
|
2015-05-30 06:03:09 +00:00
|
|
|
import "regex-tdfa" Text.Regex.TDFA
|
|
|
|
import "regex-tdfa" Text.Regex.TDFA.String
|
2014-02-21 22:34:34 +00:00
|
|
|
#else
|
|
|
|
import Text.Regex
|
2014-03-05 03:49:46 +00:00
|
|
|
import Data.Maybe
|
2014-02-21 22:34:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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 $
|
|
|
|
#ifdef WITH_TDFA
|
|
|
|
case compile (defaultCompOpt {caseSensitive = casesentitive}) defaultExecOpt regex of
|
|
|
|
Right r -> r
|
|
|
|
Left _ -> error $ "failed to compile regex: " ++ regex
|
|
|
|
#else
|
|
|
|
mkRegexWithOpts regex casesentitive True
|
|
|
|
#endif
|
|
|
|
where
|
|
|
|
regex = '^':wildToRegex glob
|
|
|
|
casesentitive = case globcase of
|
|
|
|
CaseSensative -> True
|
|
|
|
CaseInsensative -> False
|
|
|
|
|
|
|
|
matchGlob :: Glob -> String -> Bool
|
|
|
|
matchGlob (Glob regex) val =
|
|
|
|
#ifdef WITH_TDFA
|
|
|
|
case execute regex val of
|
|
|
|
Right (Just _) -> True
|
|
|
|
_ -> False
|
|
|
|
#else
|
|
|
|
isJust $ matchRegex regex val
|
|
|
|
#endif
|