pub struct Mode { /* private fields */ }
Expand description
The match mode employed in Pattern::matches()
.
Implementations§
source§impl Mode
impl Mode
sourcepub const NO_MATCH_SLASH_LITERAL: Self = _
pub const NO_MATCH_SLASH_LITERAL: Self = _
Let globs like *
and ?
not match the slash /
literal, which is useful when matching paths.
sourcepub const IGNORE_CASE: Self = _
pub const IGNORE_CASE: Self = _
Match case insensitively for ascii characters only.
sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Returns an empty set of flags.
Examples found in repository?
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
pub fn matches_repo_relative_path<'a>(
&self,
path: impl Into<&'a BStr>,
basename_start_pos: Option<usize>,
is_dir: Option<bool>,
case: Case,
) -> bool {
let is_dir = is_dir.unwrap_or(false);
if !is_dir && self.mode.contains(pattern::Mode::MUST_BE_DIR) {
return false;
}
let flags = wildmatch::Mode::NO_MATCH_SLASH_LITERAL
| match case {
Case::Fold => wildmatch::Mode::IGNORE_CASE,
Case::Sensitive => wildmatch::Mode::empty(),
};
let path = path.into();
debug_assert_eq!(
basename_start_pos,
path.rfind_byte(b'/').map(|p| p + 1),
"BUG: invalid cached basename_start_pos provided"
);
debug_assert!(!path.starts_with(b"/"), "input path must be relative");
if self.mode.contains(pattern::Mode::NO_SUB_DIR) && !self.mode.contains(pattern::Mode::ABSOLUTE) {
let basename = &path[basename_start_pos.unwrap_or_default()..];
self.matches(basename, flags)
} else {
self.matches(path, flags)
}
}
sourcepub const fn from_bits(bits: u8) -> Option<Self>
pub const fn from_bits(bits: u8) -> Option<Self>
Convert from underlying bit representation, unless that representation contains bits that do not correspond to a flag.
sourcepub const fn from_bits_truncate(bits: u8) -> Self
pub const fn from_bits_truncate(bits: u8) -> Self
Convert from underlying bit representation, dropping any bits that do not correspond to flags.
sourcepub const unsafe fn from_bits_unchecked(bits: u8) -> Self
pub const unsafe fn from_bits_unchecked(bits: u8) -> Self
Convert from underlying bit representation, preserving all bits (even those not corresponding to a defined flag).
Safety
The caller of the bitflags!
macro can chose to allow or
disallow extra bits for their bitflags type.
The caller of from_bits_unchecked()
has to ensure that
all bits correspond to a defined flag or that extra bits
are valid for this bitflags type.
sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Returns true
if there are flags common to both self
and other
.
sourcepub const fn contains(&self, other: Self) -> bool
pub const fn contains(&self, other: Self) -> bool
Returns true
if all of the flags in other
are contained within self
.
Examples found in repository?
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
fn matches<'a>(&self, value: impl Into<&'a BStr>, mode: wildmatch::Mode) -> bool {
let value = value.into();
match self.first_wildcard_pos {
// "*literal" case, overrides starts-with
Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) && !value.contains(&b'/') => {
let text = &self.text[pos + 1..];
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
value
.len()
.checked_sub(text.len())
.map(|start| text.eq_ignore_ascii_case(&value[start..]))
.unwrap_or(false)
} else {
value.ends_with(text.as_ref())
}
}
Some(pos) => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
if !value
.get(..pos)
.map_or(false, |value| value.eq_ignore_ascii_case(&self.text[..pos]))
{
return false;
}
} else if !value.starts_with(&self.text[..pos]) {
return false;
}
crate::wildmatch(self.text.as_bstr(), value, mode)
}
None => {
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
self.text.eq_ignore_ascii_case(value)
} else {
self.text == value
}
}
}
}
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
fn match_recursive(pattern: &BStr, text: &BStr, mode: Mode) -> Result {
use self::Result::*;
let possibly_lowercase = |c: &u8| {
if mode.contains(Mode::IGNORE_CASE) {
c.to_ascii_lowercase()
} else {
*c
}
};
let mut p = pattern.iter().map(possibly_lowercase).enumerate().peekable();
let mut t = text.iter().map(possibly_lowercase).enumerate();
while let Some((mut p_idx, mut p_ch)) = p.next() {
let (mut t_idx, mut t_ch) = match t.next() {
Some(c) => c,
None if p_ch != STAR => return AbortAll,
None => (text.len(), 0),
};
if p_ch == BACKSLASH {
match p.next() {
Some((_p_idx, p_ch)) => {
if p_ch != t_ch {
return NoMatch;
} else {
continue;
}
}
None => return NoMatch,
};
}
match p_ch {
b'?' => {
if mode.contains(Mode::NO_MATCH_SLASH_LITERAL) && t_ch == SLASH {
return NoMatch;
} else {
continue;
}
}
STAR => {
let mut match_slash = mode
.contains(Mode::NO_MATCH_SLASH_LITERAL)
.then(|| false)
.unwrap_or(true);
match p.next() {
Some((next_p_idx, next_p_ch)) => {
let next;
if next_p_ch == STAR {
let leading_slash_idx = p_idx.checked_sub(1);
while p.next_if(|(_, c)| *c == STAR).is_some() {}
next = p.next();
if !mode.contains(Mode::NO_MATCH_SLASH_LITERAL) {
match_slash = true;
} else if leading_slash_idx.map_or(true, |idx| pattern[idx] == SLASH)
&& next.map_or(true, |(_, c)| {
c == SLASH || (c == BACKSLASH && p.peek().map(|t| t.1) == Some(SLASH))
})
{
if next.map_or(NoMatch, |(idx, _)| {
match_recursive(pattern[idx + 1..].as_bstr(), text[t_idx..].as_bstr(), mode)
}) == Match
{
return Match;
}
match_slash = true;
} else {
match_slash = false;
}
} else {
next = Some((next_p_idx, next_p_ch));
}
match next {
None => {
return if !match_slash && text[t_idx..].contains(&SLASH) {
NoMatch
} else {
Match
};
}
Some((next_p_idx, next_p_ch)) => {
p_idx = next_p_idx;
p_ch = next_p_ch;
if !match_slash && p_ch == SLASH {
match text[t_idx..].find_byte(SLASH) {
Some(distance_to_slash) => {
for _ in t.by_ref().take(distance_to_slash) {}
continue;
}
None => return NoMatch,
}
}
}
}
}
None => {
return if !match_slash && text[t_idx..].contains(&SLASH) {
NoMatch
} else {
Match
}
}
}
return loop {
if !crate::parse::GLOB_CHARACTERS.contains(&p_ch) {
loop {
if (!match_slash && t_ch == SLASH) || t_ch == p_ch {
break;
}
match t.next() {
Some(t) => {
t_idx = t.0;
t_ch = t.1;
}
None => break,
};
}
if t_ch != p_ch {
return NoMatch;
}
}
let res = match_recursive(pattern[p_idx..].as_bstr(), text[t_idx..].as_bstr(), mode);
if res != NoMatch {
if !match_slash || res != AbortToStarStar {
return res;
}
} else if !match_slash && t_ch == SLASH {
return AbortToStarStar;
}
match t.next() {
Some(t) => {
t_idx = t.0;
t_ch = t.1;
}
None => break AbortAll,
};
};
}
BRACKET_OPEN => {
match p.next() {
Some(t) => {
p_idx = t.0;
p_ch = t.1;
}
None => return AbortAll,
};
if p_ch == b'^' {
p_ch = NEGATE_CLASS;
}
let negated = p_ch == NEGATE_CLASS;
let mut next = if negated { p.next() } else { Some((p_idx, p_ch)) };
let mut prev_p_ch = 0;
let mut matched = false;
loop {
match next {
None => return AbortAll,
Some((p_idx, mut p_ch)) => match p_ch {
BACKSLASH => match p.next() {
Some((_, p_ch)) => {
if p_ch == t_ch {
matched = true
} else {
prev_p_ch = p_ch;
}
}
None => return AbortAll,
},
b'-' if prev_p_ch != 0
&& p.peek().is_some()
&& p.peek().map(|t| t.1) != Some(BRACKET_CLOSE) =>
{
p_ch = p.next().expect("peeked").1;
if p_ch == BACKSLASH {
p_ch = match p.next() {
Some(t) => t.1,
None => return AbortAll,
};
}
if t_ch <= p_ch && t_ch >= prev_p_ch {
matched = true;
} else if mode.contains(Mode::IGNORE_CASE) && t_ch.is_ascii_lowercase() {
let t_ch_upper = t_ch.to_ascii_uppercase();
if (t_ch_upper <= p_ch.to_ascii_uppercase()
&& t_ch_upper >= prev_p_ch.to_ascii_uppercase())
|| (t_ch_upper <= prev_p_ch.to_ascii_uppercase()
&& t_ch_upper >= p_ch.to_ascii_uppercase())
{
matched = true;
}
}
prev_p_ch = 0;
}
BRACKET_OPEN if matches!(p.peek(), Some((_, COLON))) => {
p.next();
while p.peek().map_or(false, |t| t.1 != BRACKET_CLOSE) {
p.next();
}
let closing_bracket_idx = match p.next() {
Some((idx, _)) => idx,
None => return AbortAll,
};
const BRACKET__COLON__BRACKET: usize = 3;
if closing_bracket_idx - p_idx < BRACKET__COLON__BRACKET
|| pattern[closing_bracket_idx - 1] != COLON
{
if t_ch == BRACKET_OPEN {
matched = true
}
p = pattern[p_idx + 1..]
.iter()
.map(possibly_lowercase)
.enumerate()
.peekable();
} else {
let class = &pattern.as_ref()[p_idx + 2..closing_bracket_idx - 1];
match class {
b"alnum" => {
if t_ch.is_ascii_alphanumeric() {
matched = true;
}
}
b"alpha" => {
if t_ch.is_ascii_alphabetic() {
matched = true;
}
}
b"blank" => {
if t_ch.is_ascii_whitespace() {
matched = true;
}
}
b"cntrl" => {
if t_ch.is_ascii_control() {
matched = true;
}
}
b"digit" => {
if t_ch.is_ascii_digit() {
matched = true;
}
}
b"graph" => {
if t_ch.is_ascii_graphic() {
matched = true;
}
}
b"lower" => {
if t_ch.is_ascii_lowercase() {
matched = true;
}
}
b"print" => {
if (0x20u8..=0x7e).contains(&t_ch) {
matched = true;
}
}
b"punct" => {
if t_ch.is_ascii_punctuation() {
matched = true;
}
}
b"space" => {
if t_ch == b' ' {
matched = true;
}
}
b"upper" => {
if t_ch.is_ascii_uppercase()
|| mode.contains(Mode::IGNORE_CASE) && t_ch.is_ascii_lowercase()
{
matched = true;
}
}
b"xdigit" => {
if t_ch.is_ascii_hexdigit() {
matched = true;
}
}
_ => return AbortAll,
};
prev_p_ch = 0;
}
}
_ => {
prev_p_ch = p_ch;
if p_ch == t_ch {
matched = true;
}
}
},
};
next = p.next();
if let Some((_, BRACKET_CLOSE)) = next {
break;
}
}
if matched == negated || mode.contains(Mode::NO_MATCH_SLASH_LITERAL) && t_ch == SLASH {
return NoMatch;
}
continue;
}
non_glob_ch => {
if non_glob_ch != t_ch {
return NoMatch;
} else {
continue;
}
}
}
}
t.next().map(|_| NoMatch).unwrap_or(Match)
}
sourcepub fn set(&mut self, other: Self, value: bool)
pub fn set(&mut self, other: Self, value: bool)
Inserts or removes the specified flags depending on the passed value.
sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
Returns the intersection between the flags in self
and
other
.
Specifically, the returned set contains only the flags which are
present in both self
and other
.
This is equivalent to using the &
operator (e.g.
ops::BitAnd
), as in flags & other
.
sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
Returns the union of between the flags in self
and other
.
Specifically, the returned set contains all flags which are
present in either self
or other
, including any which are
present in both (see Self::symmetric_difference
if that
is undesirable).
This is equivalent to using the |
operator (e.g.
ops::BitOr
), as in flags | other
.
sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
Returns the difference between the flags in self
and other
.
Specifically, the returned set contains all flags present in
self
, except for the ones present in other
.
It is also conceptually equivalent to the “bit-clear” operation:
flags & !other
(and this syntax is also supported).
This is equivalent to using the -
operator (e.g.
ops::Sub
), as in flags - other
.
sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
Returns the symmetric difference between the flags
in self
and other
.
Specifically, the returned set contains the flags present which
are present in self
or other
, but that are not present in
both. Equivalently, it contains the flags present in exactly
one of the sets self
and other
.
This is equivalent to using the ^
operator (e.g.
ops::BitXor
), as in flags ^ other
.
sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
Returns the complement of this set of flags.
Specifically, the returned set contains all the flags which are
not set in self
, but which are allowed for this type.
Alternatively, it can be thought of as the set difference
between Self::all()
and self
(e.g. Self::all() - self
)
This is equivalent to using the !
operator (e.g.
ops::Not
), as in !flags
.
Trait Implementations§
source§impl BitAndAssign<Mode> for Mode
impl BitAndAssign<Mode> for Mode
source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
Disables all flags disabled in the set.
source§impl BitOrAssign<Mode> for Mode
impl BitOrAssign<Mode> for Mode
source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
Adds the set of flags.
source§impl BitXorAssign<Mode> for Mode
impl BitXorAssign<Mode> for Mode
source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
Toggles the set of flags.
source§impl<'de> Deserialize<'de> for Mode
impl<'de> Deserialize<'de> for Mode
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Extend<Mode> for Mode
impl Extend<Mode> for Mode
source§fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl FromIterator<Mode> for Mode
impl FromIterator<Mode> for Mode
source§fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
source§impl Ord for Mode
impl Ord for Mode
source§impl PartialEq<Mode> for Mode
impl PartialEq<Mode> for Mode
source§impl PartialOrd<Mode> for Mode
impl PartialOrd<Mode> for Mode
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Mode> for Mode
impl SubAssign<Mode> for Mode
source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Disables all flags enabled in the set.