Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
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
match
andsubstitute
to extract suitable strings from the*
and**
matches, and substitute them back into other patterns. - Use
step
andmatchMany
to 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.c
matchestest.c
and nothing else.*.c
matches all.c
files in the current directory, sofile.c
matches, butfile.h
anddir/file.c
don't.**/*.c
matches all.c
files anywhere on the filesystem, sofile.c
,dir/file.c
,dir1/dir2/file.c
and/path/to/file.c
all match, butfile.h
anddir/file.h
don't.dir/*/*
matches all files one level belowdir
, sodir/one/file.c
anddir/two/file.h
match, butfile.c
,one/dir/file.c
,dir/file.h
anddir/one/two/file.c
don'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 (match
p 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 $match
p 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 FilePattern
s against a set of FilePath
s.
First call step
passing in all the FilePattern
s, with a tag for each one.
Next call the methods of Step
, providing the components of the FilePath
s 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
.
What we know about the next step values.
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 FilePattern
s against many FilePath
s 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))