| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
System.FilePattern
Contents
Description
A module for matching files using patterns such as "src/**/*.png" for all .png files
recursively under the src directory. See ?== for the semantics of
FilePattern values. Features:
- All matching is O(n). Most functions precompute some information given only one argument.
- Use
matchandsubstituteto extract suitable strings from the*and**matches, and substitute them back into other patterns. - Use
stepandmatchManyto perform bulk matching of many patterns against many paths simultaneously. - Use System.FilePattern.Directory to perform optimised directory traverals using patterns.
Synopsis
- type FilePattern = String
- (?==) :: FilePattern -> FilePath -> Bool
- match :: FilePattern -> FilePath -> Maybe [String]
- substitute :: Partial => FilePattern -> [String] -> FilePath
- arity :: FilePattern -> Int
- step :: [(a, FilePattern)] -> Step a
- step_ :: [FilePattern] -> Step ()
- data Step a = Step {}
- data StepNext
- matchMany :: [(a, FilePattern)] -> [(b, FilePath)] -> [(a, b, [String])]
Documentation
type FilePattern = String Source #
A type synonym for file patterns, containing ** and *. For the syntax
and semantics of FilePattern see ?==.
Most FilePath values lacking literal . and .. components are suitable as FilePattern values which match
only that specific file. On Windows \ is treated as equivalent to /.
You can write FilePattern values as a literal string, or build them
up using the operators <.> and </> (but be aware that "" produces </> "foo""./foo").
(?==) :: FilePattern -> FilePath -> Bool Source #
Match a FilePattern against a FilePath. There are two special forms:
*matches part of a path component, excluding any separators.**as a path component matches an arbitrary number of path components.
Some examples:
test.cmatchestest.cand nothing else.*.cmatches all.cfiles in the current directory, sofile.cmatches, butfile.handdir/file.cdon't.**/*.cmatches all.cfiles anywhere on the filesystem, sofile.c,dir/file.c,dir1/dir2/file.cand/path/to/file.call match, butfile.handdir/file.hdon't.dir/*/*matches all files one level belowdir, sodir/one/file.canddir/two/file.hmatch, butfile.c,one/dir/file.c,dir/file.handdir/one/two/file.cdon't.
Patterns with constructs such as foo/../bar will never match
normalised FilePath values, so are unlikely to be correct.
match :: FilePattern -> FilePath -> Maybe [String] Source #
Like ?==, but returns Nothing on if there is no match, otherwise Just with the list
of fragments matching each wildcard. For example:
isJust (matchp x) == (p?==x)match"**/*.c" "test.txt" == Nothingmatch"**/*.c" "foo.c" == Just ["","foo"]match"**/*.c" "bar/baz/foo.c" == Just ["bar/baz/","foo"]
On Windows any \ path separators will be replaced by /.
substitute :: Partial => FilePattern -> [String] -> FilePath Source #
Given a successful match, substitute it back in to a pattern with the same arity.
Raises an error if the number of parts does not match the arity of the pattern.
p?==x ==>substitute(fromJust $matchp x) p == xsubstitute"**/*.c" ["dir","file"] == "dir/file.c"
arity :: FilePattern -> Int Source #
Multiple patterns and paths
step :: [(a, FilePattern)] -> Step a Source #
Efficient matching of a set of FilePatterns against a set of FilePaths.
First call step passing in all the FilePatterns, with a tag for each one.
Next call the methods of Step, providing the components of the FilePaths in turn.
Useful for efficient bulk searching, particularly directory scanning, where you can avoid descending into directories which cannot match.
step_ :: [FilePattern] -> Step () Source #
Like step but using () as the tag for each FilePattern.
Constructors
| Step | |
Fields | |
What we know about the next step values.
Constructors
| StepOnly [String] | All components not listed will result in dull |
| StepEverything | All calls to |
| StepUnknown | We have no additional information about the output from |
matchMany :: [(a, FilePattern)] -> [(b, FilePath)] -> [(a, b, [String])] Source #
Efficiently match many FilePatterns against many FilePaths in a single operation.
Note that the returned matches are not guaranteed to be in any particular order.
matchMany [(a, pat)] [(b, path)] == maybeToList (map (a,b,) (match pat path))