zlob 1.4.1

SIMD optimized glob pattern matching library faster than glob crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::error::ZlobError;
use crate::ffi;
use crate::flags::ZlobFlags;
use crate::indicies::ZlobIndicies;
use crate::match_paths::ZlobMatch;
use std::ffi::c_char;
use std::ptr::NonNull;

/// A compiled glob pattern. Reusable for any number of path matches.
/// Captures all the information about pattern at compile time, gaurantees 0 allocation during matching
///
/// # Example: chunked batching
///
/// ```
/// use zlob::{ZlobPattern, ZlobFlags};
/// let paths_owned: Vec<String> = (0..2000)
///      .map(|i| format!("path/{}/file.rs", i))
///      .collect();
///
/// let paths: Vec<_> = paths_owned.iter().map(|s| s.as_str()).collect();
/// let pattern = ZlobPattern::compile("**/*.rs", ZlobFlags::empty())?;
/// let mut total = 0;
///
/// // for max performance & SIMD utilization it is recomended to supply results in batches
/// for chunk in paths.chunks(512) {
///     let hits = pattern.match_indices(chunk, ZlobFlags::empty())?;
///     total += hits.len();
/// }
///
/// // though it is still pretty fast to match the paths sequentially
/// let hits: Vec<_> = paths.iter().filter(|p| pattern.matches(p, ZlobFlags::empty())).collect();
/// # assert_eq!(total, 2000);
/// # Ok::<(), zlob::ZlobError>(())
/// ```
pub struct ZlobPattern {
    inner: NonNull<ffi::zlob_pattern_t>,
    /// Flags this pattern was compiled under. Stored so [`Self::matches_default`]
    /// can pass the same flags without forcing the caller to remember them.
    flags: ZlobFlags,
}

impl ZlobPattern {
    /// Compile a glob pattern. The pattern string is copied internally; the
    /// caller may drop it immediately after this call returns.
    pub fn compile(pattern: &str, flags: ZlobFlags) -> Result<Self, ZlobError> {
        // SAFETY: `zlob_pattern_compile_slice` accepts a non-null-terminated
        // (ptr, len) slice. zlob_slice_t and &str have identical layout — same
        // transmute trick used elsewhere in the crate.
        let slice: &ffi::zlob_slice_t = unsafe { std::mem::transmute(&pattern) };
        let raw = unsafe { ffi::zlob_pattern_compile_slice(slice, flags.bits()) };
        match NonNull::new(raw) {
            Some(p) => Ok(ZlobPattern { inner: p, flags }),
            None => Err(ZlobError::NoSpace),
        }
    }

    /// The flags this pattern was compiled under.
    #[inline]
    pub fn flags(&self) -> ZlobFlags {
        self.flags
    }

    /// Match a single path against this compiled pattern. Allocation-free.
    ///
    /// Use this in any caller-driven loop, iterator, or chunked pipeline.
    ///
    /// `flags` must agree with the compile-time flags on the structural bits
    /// (BRACE / DOUBLESTAR_RECURSIVE / EXTGLOB); debug builds assert this.
    /// Behavioral bits (PERIOD / NOESCAPE / etc.) may be flipped per call.
    #[inline]
    pub fn matches(&self, path: &str, flags: ZlobFlags) -> bool {
        unsafe {
            ffi::zlob_pattern_matches(
                self.inner.as_ptr(),
                path.as_ptr() as *const c_char,
                path.len(),
                flags.bits(),
            ) != 0
        }
    }

    /// Match a single path using the same flags this pattern was compiled
    /// under. Equivalent to `matches(path, self.flags())`.
    #[inline]
    pub fn matches_default(&self, path: &str) -> bool {
        self.matches(path, self.flags)
    }

    /// Batch match: return references to paths that match.
    ///
    /// The returned `ZlobMatch` borrows path slices from the input — the
    /// caller must keep `paths` alive until the result is dropped.
    pub fn match_paths<'a>(
        &self,
        paths: &'a [&str],
        flags: ZlobFlags,
    ) -> Result<Option<ZlobMatch<'a>>, ZlobError> {
        let path_slices: &[ffi::zlob_slice_t] = unsafe { std::mem::transmute(paths) };
        let mut inner = ffi::zlob_t::default();

        let result = unsafe {
            ffi::zlob_pattern_match_paths_slice(
                self.inner.as_ptr(),
                path_slices.as_ptr(),
                path_slices.len(),
                flags.bits(),
                &mut inner,
            )
        };

        match ZlobError::from_code(result) {
            Ok(true) => Ok(Some(unsafe { ZlobMatch::from_raw(inner) })),
            Ok(false) => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// At-flow variant of [`Self::match_paths`].
    pub fn match_paths_at<'a>(
        &self,
        base_path: &str,
        paths: &'a [&str],
        flags: ZlobFlags,
    ) -> Result<Option<ZlobMatch<'a>>, ZlobError> {
        let base_slice: &ffi::zlob_slice_t = unsafe { std::mem::transmute(&base_path) };
        let path_slices: &[ffi::zlob_slice_t] = unsafe { std::mem::transmute(paths) };
        let mut inner = ffi::zlob_t::default();

        let result = unsafe {
            ffi::zlob_pattern_match_paths_at_slice(
                self.inner.as_ptr(),
                base_slice,
                path_slices.as_ptr(),
                path_slices.len(),
                flags.bits(),
                &mut inner,
            )
        };

        match ZlobError::from_code(result) {
            Ok(true) => Ok(Some(unsafe { ZlobMatch::from_raw(inner) })),
            Ok(false) => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// Batch match: return the indices into `paths` of all matching paths,
    /// in input order. Borrows the C-allocated buffer directly — no copy.
    ///
    /// Returns an empty [`ZlobIndicies`] for the no-match case (no `Option`
    /// wrapper — empty is the natural representation for callers that iterate).
    pub fn match_indices(
        &self,
        paths: &[&str],
        flags: ZlobFlags,
    ) -> Result<ZlobIndicies, ZlobError> {
        let path_slices: &[ffi::zlob_slice_t] = unsafe { std::mem::transmute(paths) };
        let mut out = ffi::zlob_indices_t {
            indices: std::ptr::null_mut(),
            count: 0,
        };

        let code = unsafe {
            ffi::zlob_pattern_match_paths_indices_slice(
                self.inner.as_ptr(),
                path_slices.as_ptr(),
                path_slices.len(),
                flags.bits(),
                &mut out,
            )
        };

        ZlobIndicies::from_ffi(code, out)
    }

    /// At-flow variant of [`Self::match_indices`].
    pub fn match_indices_at(
        &self,
        base_path: &str,
        paths: &[&str],
        flags: ZlobFlags,
    ) -> Result<ZlobIndicies, ZlobError> {
        let base_slice: &ffi::zlob_slice_t = unsafe { std::mem::transmute(&base_path) };
        let path_slices: &[ffi::zlob_slice_t] = unsafe { std::mem::transmute(paths) };
        let mut out = ffi::zlob_indices_t {
            indices: std::ptr::null_mut(),
            count: 0,
        };

        let code = unsafe {
            ffi::zlob_pattern_match_paths_indices_at_slice(
                self.inner.as_ptr(),
                base_slice,
                path_slices.as_ptr(),
                path_slices.len(),
                flags.bits(),
                &mut out,
            )
        };

        ZlobIndicies::from_ffi(code, out)
    }
}

impl std::fmt::Debug for ZlobPattern {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZlobPattern")
            .field("flags", &self.flags)
            .finish()
    }
}

impl Drop for ZlobPattern {
    fn drop(&mut self) {
        unsafe { ffi::zlob_pattern_free(self.inner.as_ptr()) };
    }
}

// SAFETY: ZlobPattern is read-only after compile and owns its memory.
unsafe impl Send for ZlobPattern {}
// SAFETY: matches() / match_*() take &self and the underlying CompiledPattern
// is read-only (the only write happens during compile / drop).
unsafe impl Sync for ZlobPattern {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{zlob_match_paths_indices, zlob_match_paths_indices_at};

    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}

    #[test]
    fn pattern_is_send_and_sync() {
        assert_send::<ZlobPattern>();
        assert_sync::<ZlobPattern>();
    }

    #[test]
    fn compile_literal() {
        let p = ZlobPattern::compile("src/main.rs", ZlobFlags::empty()).unwrap();
        assert!(p.matches("src/main.rs", ZlobFlags::empty()));
        assert!(!p.matches("src/lib.rs", ZlobFlags::empty()));
    }

    #[test]
    fn compile_simple_suffix() {
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        assert!(p.matches("foo.rs", ZlobFlags::empty()));
        assert!(p.matches("a/b/c.rs", ZlobFlags::empty()));
        assert!(!p.matches("foo.txt", ZlobFlags::empty()));
    }

    #[test]
    fn compile_brace_multi_suffix() {
        let p = ZlobPattern::compile("*.{c,h}", ZlobFlags::BRACE).unwrap();
        assert!(p.matches("foo.c", ZlobFlags::BRACE));
        assert!(p.matches("foo.h", ZlobFlags::BRACE));
        assert!(!p.matches("foo.txt", ZlobFlags::BRACE));
    }

    #[test]
    fn compile_general_doublestar() {
        let p = ZlobPattern::compile("src/**/*.rs", ZlobFlags::DOUBLESTAR_RECURSIVE).unwrap();
        let f = ZlobFlags::DOUBLESTAR_RECURSIVE;
        assert!(p.matches("src/foo.rs", f));
        assert!(p.matches("src/sub/dir/bar.rs", f));
        assert!(!p.matches("test/foo.rs", f));
    }

    #[test]
    fn matches_default_uses_compile_flags() {
        let p = ZlobPattern::compile("*.{c,h}", ZlobFlags::BRACE).unwrap();
        assert!(p.matches_default("foo.c"));
        assert!(!p.matches_default("foo.txt"));
    }

    #[test]
    fn match_paths_basic() {
        let paths = ["foo.rs", "bar.txt", "baz.rs"];
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        let result = p.match_paths(&paths, ZlobFlags::empty()).unwrap().unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn match_indices_basic() {
        let paths = ["foo.rs", "bar.txt", "baz.rs", "qux.md"];
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        let hits = p.match_indices(&paths, ZlobFlags::empty()).unwrap();
        assert_eq!(hits.as_slice(), &[0, 2]);
    }

    #[test]
    fn match_indices_no_match_is_empty_vec() {
        let paths = ["foo.txt", "bar.md"];
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        let hits = p.match_indices(&paths, ZlobFlags::empty()).unwrap();
        assert!(hits.is_empty());
    }

    #[test]
    fn match_indices_input_order_preserved() {
        // NOSORT is implicitly the only behavior — input order regardless.
        let paths = ["z.rs", "a.rs", "m.rs"];
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        let hits = p.match_indices(&paths, ZlobFlags::empty()).unwrap();
        assert_eq!(hits.as_slice(), &[0, 1, 2]);
    }

    #[test]
    fn match_indices_at() {
        let paths = ["/proj/src/lib.rs", "/proj/src/main.rs", "/proj/README.md"];
        let p = ZlobPattern::compile("src/*.rs", ZlobFlags::empty()).unwrap();
        let hits = p
            .match_indices_at("/proj", &paths, ZlobFlags::empty())
            .unwrap();
        assert_eq!(hits.as_slice(), &[0, 1]);
    }

    #[test]
    fn pattern_reused_across_chunks() {
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        let chunk1 = ["a.rs", "b.txt"];
        let chunk2 = ["c.rs", "d.rs", "e.md"];
        let h1 = p.match_indices(&chunk1, ZlobFlags::empty()).unwrap();
        let h2 = p.match_indices(&chunk2, ZlobFlags::empty()).unwrap();
        assert_eq!(h1.as_slice(), &[0]);
        assert_eq!(h2.as_slice(), &[0, 1]);
    }

    #[test]
    fn free_fn_match_paths_indices() {
        let paths = ["a.rs", "b.txt", "c.rs"];
        let hits = zlob_match_paths_indices("*.rs", &paths, ZlobFlags::empty()).unwrap();
        assert_eq!(hits.as_slice(), &[0, 2]);
    }

    #[test]
    fn free_fn_match_paths_indices_at() {
        let paths = ["/proj/src/lib.rs", "/proj/src/main.rs", "/proj/README.md"];
        let hits =
            zlob_match_paths_indices_at("/proj", "src/*.rs", &paths, ZlobFlags::empty()).unwrap();
        assert_eq!(hits.as_slice(), &[0, 1]);
    }

    #[test]
    fn drop_does_not_double_free() {
        // Compile + drop in a tight loop to surface any double-free regressions.
        for _ in 0..100 {
            let p = ZlobPattern::compile("**/*.rs", ZlobFlags::DOUBLESTAR_RECURSIVE).unwrap();
            assert!(p.matches("a/b/c.rs", ZlobFlags::DOUBLESTAR_RECURSIVE));
        }
    }

    #[test]
    fn flags_returns_compile_flags() {
        let p = ZlobPattern::compile("*.{c,h}", ZlobFlags::BRACE).unwrap();
        assert_eq!(p.flags(), ZlobFlags::BRACE);
    }

    #[test]
    fn match_paths_at_basic() {
        let paths = ["/proj/src/lib.rs", "/proj/src/main.rs", "/proj/README.md"];
        let p = ZlobPattern::compile("src/*.rs", ZlobFlags::empty()).unwrap();
        let result = p
            .match_paths_at("/proj", &paths, ZlobFlags::empty())
            .unwrap()
            .unwrap();
        assert_eq!(result.len(), 2);
        let strs = result.as_strs();
        assert!(strs.contains(&"/proj/src/lib.rs"));
        assert!(strs.contains(&"/proj/src/main.rs"));
    }

    #[test]
    fn match_paths_at_no_match_is_none() {
        let paths = ["/proj/README.md"];
        let p = ZlobPattern::compile("src/*.rs", ZlobFlags::empty()).unwrap();
        let result = p
            .match_paths_at("/proj", &paths, ZlobFlags::empty())
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn match_paths_no_match_is_none() {
        let paths = ["foo.txt", "bar.md"];
        let p = ZlobPattern::compile("*.rs", ZlobFlags::empty()).unwrap();
        assert!(p.match_paths(&paths, ZlobFlags::empty()).unwrap().is_none());
    }

    #[test]
    fn match_indices_at_no_match_is_empty() {
        let paths = ["/proj/README.md"];
        let p = ZlobPattern::compile("src/*.rs", ZlobFlags::empty()).unwrap();
        let hits = p
            .match_indices_at("/proj", &paths, ZlobFlags::empty())
            .unwrap();
        assert!(hits.is_empty());
    }

    #[test]
    fn matches_per_call_period_flag() {
        // Behavioral flag (PERIOD) flipped per call without recompiling. Uses a
        // doublestar pattern so the per-component hidden-file logic engages.
        let f = ZlobFlags::DOUBLESTAR_RECURSIVE;
        let p = ZlobPattern::compile("dir/**", f).unwrap();
        assert!(!p.matches("dir/.hidden", f));
        assert!(p.matches("dir/.hidden", f | ZlobFlags::PERIOD));
    }
}