Skip to content

Commit

Permalink
refactor (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 8, 2022
1 parent 027869d commit f285ca0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 6 additions & 12 deletions git-glob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use bstr::BString;

pub struct Pattern {
/// the actual pattern bytes
_inner: BString,
_mode: pattern::Mode,
pub text: BString,
/// Additional information to help accelerate pattern matching.
pub mode: pattern::Mode,
}

pub mod pattern {
use crate::Pattern;
use bitflags::bitflags;
use bstr::{BStr, BString};
use bstr::BStr;

bitflags! {
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -28,15 +29,8 @@ pub mod pattern {
}

impl Pattern {
pub fn new(pattern: impl Into<BString>, mode: Mode) -> Self {
Pattern {
_inner: pattern.into(),
_mode: mode,
}
}

pub fn from_bytes(pattern: &BStr) -> Option<Self> {
crate::parse(pattern).map(|(pattern, mode)| Self::new(pattern, mode))
pub fn from_bytes(text: &BStr) -> Option<Self> {
crate::parse::pattern(text).map(|(text, mode)| Pattern { text, mode })
}

pub fn matches(&self, _value: &BStr) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion git-glob/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ pub fn pattern(mut pat: &[u8]) -> Option<(BString, pattern::Mode)> {
if !line.contains(&b'/') {
mode |= Mode::NO_SUB_DIR;
}
if line.first() == Some(&b'*') && line[1..].find_byteset(GLOB_CHARACTERS).is_none() {
if line.first() == Some(&b'*') && no_wildcard_len(&line[1..]).is_none() {
mode |= Mode::ENDS_WITH;
}
Some((line, mode))
}

fn no_wildcard_len(pat: &[u8]) -> Option<usize> {
pat.find_byteset(GLOB_CHARACTERS)
}

const GLOB_CHARACTERS: &[u8] = br"*?[\";

/// We always copy just because that's ultimately needed anyway, not because we always have to.
Expand Down

0 comments on commit f285ca0

Please sign in to comment.