summaryrefslogtreecommitdiff
path: root/Utility/Glob.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2014-02-21 18:34:34 -0400
committerGravatar Joey Hess <joey@kitenet.net>2014-02-21 18:34:34 -0400
commita8e8573a721d28ec3168d051d9a04edcbd279800 (patch)
treee8758a1ce00be66757f6dbf358388236f5d8688a /Utility/Glob.hs
parent9d2610f22bc8529f91de758ebc68935272cee46e (diff)
--metadata field=value can now use globs to match, and matches case insensatively, the same as git annex view field=value does.
Also refactored glob code into its own module.
Diffstat (limited to 'Utility/Glob.hs')
-rw-r--r--Utility/Glob.hs57
1 files changed, 57 insertions, 0 deletions
diff --git a/Utility/Glob.hs b/Utility/Glob.hs
new file mode 100644
index 000000000..5291af452
--- /dev/null
+++ b/Utility/Glob.hs
@@ -0,0 +1,57 @@
+{- file globbing
+ -
+ - This uses TDFA when available, with a fallback to regex-compat.
+ - TDFA is less buggy in its support for non-unicode characters.
+ -
+ - Copyright 2014 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+{-# LANGUAGE CPP #-}
+
+module Utility.Glob (
+ Glob,
+ GlobCase(..),
+ compileGlob,
+ matchGlob
+) where
+
+import System.Path.WildMatch
+
+#ifdef WITH_TDFA
+import Text.Regex.TDFA
+import Text.Regex.TDFA.String
+#else
+import Text.Regex
+#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