2014-02-21 22:34:34 +00:00
|
|
|
{- file globbing
|
|
|
|
-
|
2020-12-15 16:39:34 +00:00
|
|
|
- Copyright 2014-2020 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
|
|
|
-}
|
|
|
|
|
2020-12-15 16:39:34 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE PackageImports #-}
|
|
|
|
|
2014-02-21 22:34:34 +00:00
|
|
|
module Utility.Glob (
|
|
|
|
Glob,
|
|
|
|
GlobCase(..),
|
2020-12-15 16:39:34 +00:00
|
|
|
GlobFilePath(..),
|
2014-02-21 22:34:34 +00:00
|
|
|
compileGlob,
|
|
|
|
matchGlob
|
|
|
|
) where
|
|
|
|
|
2016-11-16 01:29:54 +00:00
|
|
|
import Utility.Exception
|
|
|
|
|
2015-05-30 06:03:09 +00:00
|
|
|
import "regex-tdfa" Text.Regex.TDFA
|
|
|
|
import "regex-tdfa" Text.Regex.TDFA.String
|
2017-05-16 03:32:17 +00:00
|
|
|
import Data.Char
|
2014-02-21 22:34:34 +00:00
|
|
|
|
|
|
|
newtype Glob = Glob Regex
|
|
|
|
|
2023-03-13 23:06:23 +00:00
|
|
|
data GlobCase = CaseSensitive | CaseInsensitive
|
2014-02-21 22:34:34 +00:00
|
|
|
|
2020-12-15 16:39:34 +00:00
|
|
|
-- Is the glob being used to match filenames?
|
|
|
|
--
|
|
|
|
-- When matching filenames,
|
|
|
|
-- a single path separator (eg /) in the glob will match any
|
|
|
|
-- number of path separators in the filename.
|
|
|
|
-- And on Windows, both / and \ are used as path separators, so compile
|
|
|
|
-- the glob to a regexp that matches either path separator.
|
|
|
|
newtype GlobFilePath = GlobFilePath Bool
|
|
|
|
|
2014-02-21 22:34:34 +00:00
|
|
|
{- Compiles a glob to a regex, that can be repeatedly used. -}
|
2020-12-15 16:39:34 +00:00
|
|
|
compileGlob :: String -> GlobCase -> GlobFilePath -> Glob
|
|
|
|
compileGlob glob globcase globfilepath = Glob $
|
2014-02-21 22:34:34 +00:00
|
|
|
case compile (defaultCompOpt {caseSensitive = casesentitive}) defaultExecOpt regex of
|
|
|
|
Right r -> r
|
2016-11-16 01:29:54 +00:00
|
|
|
Left _ -> giveup $ "failed to compile regex: " ++ regex
|
2014-02-21 22:34:34 +00:00
|
|
|
where
|
2020-12-15 16:39:34 +00:00
|
|
|
regex = '^' : wildToRegex globfilepath glob ++ "$"
|
2014-02-21 22:34:34 +00:00
|
|
|
casesentitive = case globcase of
|
2023-03-13 23:06:23 +00:00
|
|
|
CaseSensitive -> True
|
|
|
|
CaseInsensitive -> False
|
2014-02-21 22:34:34 +00:00
|
|
|
|
2020-12-15 16:39:34 +00:00
|
|
|
wildToRegex :: GlobFilePath -> String -> String
|
|
|
|
wildToRegex (GlobFilePath globfile) = concat . go
|
2017-05-16 03:32:17 +00:00
|
|
|
where
|
|
|
|
go [] = []
|
|
|
|
go ('*':xs) = ".*" : go xs
|
|
|
|
go ('?':xs) = "." : go xs
|
|
|
|
go ('[':'!':xs) = "[^" : inpat xs
|
|
|
|
go ('[':xs) = "[" : inpat xs
|
2020-12-15 16:39:34 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
go ('/':xs) | globfile = "[/\\]+" : go xs
|
|
|
|
go ('\\':xs) | globfile = "[/\\]+" : go xs
|
|
|
|
#else
|
|
|
|
go ('/':xs) | globfile = "[/]+" : go xs
|
|
|
|
go ('\\':xs) | globfile = "[\\]+" : go xs
|
|
|
|
#endif
|
2017-05-16 03:32:17 +00:00
|
|
|
go (x:xs)
|
|
|
|
| isDigit x || isAlpha x = [x] : go xs
|
|
|
|
| otherwise = esc x : go xs
|
|
|
|
|
|
|
|
inpat [] = []
|
|
|
|
inpat (x:xs) = case x of
|
|
|
|
']' -> "]" : go xs
|
|
|
|
'\\' -> esc x : inpat xs
|
|
|
|
_ -> [x] : inpat xs
|
|
|
|
|
|
|
|
esc c = ['\\', c]
|
|
|
|
|
2014-02-21 22:34:34 +00:00
|
|
|
matchGlob :: Glob -> String -> Bool
|
|
|
|
matchGlob (Glob regex) val =
|
|
|
|
case execute regex val of
|
|
|
|
Right (Just _) -> True
|
|
|
|
_ -> False
|