Skip to content
Myr

README.md

This file

Path

crates/globset/README.md

119 lines · 3.8 KiB · Markdown

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 5f5da48 point at it.

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 1–119why, one step back
1globset
2=======3Cross platform single glob and glob set matching. Glob set matching is the4process of matching one or more glob patterns against a single candidate path5simultaneously, and returning all of the globs that matched.67[![Build status](https://github.com/BurntSushi/ripgrep/workflows/ci/badge.svg)](https://github.com/BurntSushi/ripgrep/actions)8[![](https://img.shields.io/crates/v/globset.svg)](https://crates.io/crates/globset)910Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org/).1112### Documentation1314[https://docs.rs/globset](https://docs.rs/globset)1516### Usage1718Add this to your `Cargo.toml`:1920```toml21[dependencies]22globset = "0.4"23```2425### Features2627* `serde1`: Enables implementing Serde traits on the `Glob` type.2829### Example: one glob3031This example shows how to match a single glob against a single file path.3233```rust34use globset::Glob;3536let glob = Glob::new("*.rs")?.compile_matcher();3738assert!(glob.is_match("foo.rs"));39assert!(glob.is_match("foo/bar.rs"));40assert!(!glob.is_match("Cargo.toml"));41```4243### Example: configuring a glob matcher4445This example shows how to use a `GlobBuilder` to configure aspects of match46semantics. In this example, we prevent wildcards from matching path separators.4748```rust49use globset::GlobBuilder;5051let glob = GlobBuilder::new("*.rs")52 .literal_separator(true).build()?.compile_matcher();5354assert!(glob.is_match("foo.rs"));55assert!(!glob.is_match("foo/bar.rs")); // no longer matches56assert!(!glob.is_match("Cargo.toml"));57```5859### Example: match multiple globs at once6061This example shows how to match multiple glob patterns at once.6263```rust64use globset::{Glob, GlobSetBuilder};6566let mut builder = GlobSetBuilder::new();67// A GlobBuilder can be used to configure each glob's match semantics68// independently.69builder.add(Glob::new("*.rs")?);70builder.add(Glob::new("src/lib.rs")?);71builder.add(Glob::new("src/**/foo.rs")?);72let set = builder.build()?;7374assert_eq!(set.matches("src/bar/baz/foo.rs"), vec![0, 2]);75```7677### Performance7879This crate implements globs by converting them to regular expressions, and80executing them with the81[`regex`](https://github.com/rust-lang/regex)82crate.8384For single glob matching, performance of this crate should be roughly on par85with the performance of the86[`glob`](https://github.com/rust-lang/glob)87crate. (`*_regex` correspond to benchmarks for this library while `*_glob`88correspond to benchmarks for the `glob` library.)89Optimizations in the `regex` crate may propel this library past `glob`,90particularly when matching longer paths.9192```93test ext_glob ... bench: 425 ns/iter (+/- 21)94test ext_regex ... bench: 175 ns/iter (+/- 10)95test long_glob ... bench: 182 ns/iter (+/- 11)96test long_regex ... bench: 173 ns/iter (+/- 10)97test short_glob ... bench: 69 ns/iter (+/- 4)98test short_regex ... bench: 83 ns/iter (+/- 2)99```100101The primary performance advantage of this crate is when matching multiple102globs against a single path. With the `glob` crate, one must match each glob103synchronously, one after the other. In this crate, many can be matched104simultaneously. For example:105106```107test many_short_glob ... bench: 1,063 ns/iter (+/- 47)108test many_short_regex_set ... bench: 186 ns/iter (+/- 11)109```110111### Comparison with the [`glob`](https://github.com/rust-lang/glob) crate112113* Supports alternate "or" globs, e.g., `*.{foo,bar}`.114* Can match non-UTF-8 file paths correctly.115* Supports matching multiple globs at once.116* Doesn't provide a recursive directory iterator of matching file paths,117 although I believe this crate should grow one eventually.118* Supports case insensitive and require-literal-separator match options, but119 **doesn't** support the require-literal-leading-dot option.