Struct git_glob::wildmatch::Mode

source ·
pub struct Mode { /* private fields */ }
Expand description

The match mode employed in Pattern::matches().

Implementations§

Let globs like * and ? not match the slash / literal, which is useful when matching paths.

Match case insensitively for ascii characters only.

Returns an empty set of flags.

Examples found in repository?
src/pattern.rs (line 84)
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)
        }
    }

Returns the set containing all flags.

Returns the raw value of the flags currently stored.

Convert from underlying bit representation, unless that representation contains bits that do not correspond to a flag.

Convert from underlying bit representation, dropping any bits that do not correspond to flags.

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.

Returns true if no flags are currently stored.

Returns true if all flags are currently set.

Returns true if there are flags common to both self and other.

Returns true if all of the flags in other are contained within self.

Examples found in repository?
src/pattern.rs (line 114)
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
Hide additional examples
src/wildmatch.rs (line 38)
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)
    }

Inserts the specified flags in-place.

Removes the specified flags in-place.

Toggles the specified flags in-place.

Inserts or removes the specified flags depending on the passed value.

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.

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.

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.

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.

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§

Formats the value using the given formatter.

Returns the intersection between the two sets of flags.

The resulting type after applying the & operator.

Disables all flags disabled in the set.

Returns the union of the two sets of flags.

The resulting type after applying the | operator.

Adds the set of flags.

Returns the left flags, but with all the right flags toggled.

The resulting type after applying the ^ operator.

Toggles the set of flags.

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Formats the value using the given formatter.

Returns the complement of this set of flags.

The resulting type after applying the ! operator.
Formats the value using the given formatter.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

Returns the set difference of the two sets of flags.

The resulting type after applying the - operator.

Disables all flags enabled in the set.

Formats the value using the given formatter.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.