Skip to content

Commit

Permalink
globset: polishing
Browse files Browse the repository at this point in the history
This brings the code in line with my current style. It also inlines the
dozen or so lines of code for FNV hashing instead of bringing in a
micro-crate for it. Finally, it drops the dependency on regex in favor
of using regex-syntax and regex-automata directly.
  • Loading branch information
BurntSushi committed Sep 26, 2023
1 parent 09ff320 commit b440b99
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 152 deletions.
10 changes: 2 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions crates/globset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@ repository = "https://github.com/BurntSushi/ripgrep/tree/master/crates/globset"
readme = "README.md"
keywords = ["regex", "glob", "multiple", "set", "pattern"]
license = "Unlicense OR MIT"
edition = "2018"
edition = "2021"

[lib]
name = "globset"
bench = false

[dependencies]
aho-corasick = "1.0.2"
bstr = { version = "1.6.0", default-features = false, features = ["std"] }
fnv = "1.0.6"
log = { version = "0.4.5", optional = true }
regex = { version = "1.8.3", default-features = false, features = ["perf", "std"] }
serde = { version = "1.0.104", optional = true }
aho-corasick = "1.1.1"
bstr = { version = "1.6.2", default-features = false, features = ["std"] }
log = { version = "0.4.20", optional = true }
serde = { version = "1.0.188", optional = true }

[dependencies.regex-syntax]
version = "0.7.5"
default-features = false
features = ["std"]

[dependencies.regex-automata]
version = "0.3.8"
default-features = false
features = ["std", "perf", "syntax", "meta", "nfa", "hybrid"]

[dev-dependencies]
glob = "0.3.0"
glob = "0.3.1"
lazy_static = "1"
serde_json = "1.0.45"
serde_json = "1.0.107"

[features]
default = ["log"]
Expand Down
30 changes: 30 additions & 0 deletions crates/globset/src/fnv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// A convenience alias for creating a hash map with an FNV hasher.
pub(crate) type HashMap<K, V> =
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<Hasher>>;

/// A hasher that implements the Fowler–Noll–Vo (FNV) hash.
pub(crate) struct Hasher(u64);

impl Hasher {
const OFFSET_BASIS: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
}

impl Default for Hasher {
fn default() -> Hasher {
Hasher(Hasher::OFFSET_BASIS)
}
}

impl std::hash::Hasher for Hasher {
fn finish(&self) -> u64 {
self.0
}

fn write(&mut self, bytes: &[u8]) {
for &byte in bytes.iter() {
self.0 = self.0 ^ u64::from(byte);
self.0 = self.0.wrapping_mul(Hasher::PRIME);
}
}
}
Loading

0 comments on commit b440b99

Please sign in to comment.