/crates/globset/README.md

https://github.com/BurntSushi/ripgrep · Markdown · 126 lines · 92 code · 34 blank · 0 comment · 0 complexity · 5a5d82df1f9c8d23f37363f78f0b0c90 MD5 · raw file

  1. globset
  2. =======
  3. Cross platform single glob and glob set matching. Glob set matching is the
  4. process of matching one or more glob patterns against a single candidate path
  5. simultaneously, and returning all of the globs that matched.
  6. [![Linux build status](https://api.travis-ci.org/BurntSushi/ripgrep.svg)](https://travis-ci.org/BurntSushi/ripgrep)
  7. [![Windows build status](https://ci.appveyor.com/api/projects/status/github/BurntSushi/ripgrep?svg=true)](https://ci.appveyor.com/project/BurntSushi/ripgrep)
  8. [![](https://img.shields.io/crates/v/globset.svg)](https://crates.io/crates/globset)
  9. Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
  10. ### Documentation
  11. [https://docs.rs/globset](https://docs.rs/globset)
  12. ### Usage
  13. Add this to your `Cargo.toml`:
  14. ```toml
  15. [dependencies]
  16. globset = "0.3"
  17. ```
  18. and this to your crate root:
  19. ```rust
  20. extern crate globset;
  21. ```
  22. ### Features
  23. * `serde1`: Enables implementing Serde traits on the `Glob` type.
  24. ### Example: one glob
  25. This example shows how to match a single glob against a single file path.
  26. ```rust
  27. use globset::Glob;
  28. let glob = Glob::new("*.rs")?.compile_matcher();
  29. assert!(glob.is_match("foo.rs"));
  30. assert!(glob.is_match("foo/bar.rs"));
  31. assert!(!glob.is_match("Cargo.toml"));
  32. ```
  33. ### Example: configuring a glob matcher
  34. This example shows how to use a `GlobBuilder` to configure aspects of match
  35. semantics. In this example, we prevent wildcards from matching path separators.
  36. ```rust
  37. use globset::GlobBuilder;
  38. let glob = GlobBuilder::new("*.rs")
  39. .literal_separator(true).build()?.compile_matcher();
  40. assert!(glob.is_match("foo.rs"));
  41. assert!(!glob.is_match("foo/bar.rs")); // no longer matches
  42. assert!(!glob.is_match("Cargo.toml"));
  43. ```
  44. ### Example: match multiple globs at once
  45. This example shows how to match multiple glob patterns at once.
  46. ```rust
  47. use globset::{Glob, GlobSetBuilder};
  48. let mut builder = GlobSetBuilder::new();
  49. // A GlobBuilder can be used to configure each glob's match semantics
  50. // independently.
  51. builder.add(Glob::new("*.rs")?);
  52. builder.add(Glob::new("src/lib.rs")?);
  53. builder.add(Glob::new("src/**/foo.rs")?);
  54. let set = builder.build()?;
  55. assert_eq!(set.matches("src/bar/baz/foo.rs"), vec![0, 2]);
  56. ```
  57. ### Performance
  58. This crate implements globs by converting them to regular expressions, and
  59. executing them with the
  60. [`regex`](https://github.com/rust-lang-nursery/regex)
  61. crate.
  62. For single glob matching, performance of this crate should be roughly on par
  63. with the performance of the
  64. [`glob`](https://github.com/rust-lang-nursery/glob)
  65. crate. (`*_regex` correspond to benchmarks for this library while `*_glob`
  66. correspond to benchmarks for the `glob` library.)
  67. Optimizations in the `regex` crate may propel this library past `glob`,
  68. particularly when matching longer paths.
  69. ```
  70. test ext_glob ... bench: 425 ns/iter (+/- 21)
  71. test ext_regex ... bench: 175 ns/iter (+/- 10)
  72. test long_glob ... bench: 182 ns/iter (+/- 11)
  73. test long_regex ... bench: 173 ns/iter (+/- 10)
  74. test short_glob ... bench: 69 ns/iter (+/- 4)
  75. test short_regex ... bench: 83 ns/iter (+/- 2)
  76. ```
  77. The primary performance advantage of this crate is when matching multiple
  78. globs against a single path. With the `glob` crate, one must match each glob
  79. synchronously, one after the other. In this crate, many can be matched
  80. simultaneously. For example:
  81. ```
  82. test many_short_glob ... bench: 1,063 ns/iter (+/- 47)
  83. test many_short_regex_set ... bench: 186 ns/iter (+/- 11)
  84. ```
  85. ### Comparison with the [`glob`](https://github.com/rust-lang-nursery/glob) crate
  86. * Supports alternate "or" globs, e.g., `*.{foo,bar}`.
  87. * Can match non-UTF-8 file paths correctly.
  88. * Supports matching multiple globs at once.
  89. * Doesn't provide a recursive directory iterator of matching file paths,
  90. although I believe this crate should grow one eventually.
  91. * Supports case insensitive and require-literal-separator match options, but
  92. **doesn't** support the require-literal-leading-dot option.