Allocation-free glob matching for byte strings in pure Zig.
*,?, sets ([abc]), ranges ([a-z]), and negated sets ([!abc],[^abc])- Backslash escaping
- Optional pathname, leading-period, ASCII case-insensitive, and no-escape behavior
- Malformed patterns return an error
- Constant stack use, no allocation, and no dependencies
Matching is byte-oriented. ? consumes one byte, not one Unicode scalar.
zig fetch --save=libglob https://github.com/xcaeser/libglob/archive/v0.3.0.tar.gzAdd the module to your executable or library:
const glob_dep = b.dependency("libglob", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("libglob", glob_dep.module("libglob"));const std = @import("std");
const glob = @import("libglob");
pub fn main() !void {
std.debug.assert(try glob.match("src/*.zig", "src/main.zig", .{}));
std.debug.assert(try glob.match("file[0-9].txt", "file7.txt", .{}));
std.debug.assert(!try glob.match("*.tmp", "notes.txt", .{}));
// `*` cannot cross `/`.
std.debug.assert(!try glob.match("src/*.zig", "src/lib/main.zig", .{ .pathname = true }));
std.debug.assert(try glob.match("README.*", "readme.md", .{ .ignore_case = true }));
// Malformed patterns are errors, not non-matches.
std.debug.assert(glob.match("[a-z", "a", .{}) == error.UnclosedBracket);
}pub fn match(pattern: []const u8, text: []const u8, options: Options) ParseError!boolParseError is UnclosedBracket, EmptyBracket, InvalidRange, or
TrailingBackslash. To negate a match, use ! on the result; to match a list
of patterns, loop over it.
| Pattern | Meaning |
|---|---|
* |
Zero or more bytes |
? |
Exactly one byte |
[abc] |
One byte in the set |
[a-z] |
One byte in the inclusive range |
[!abc], [^abc] |
One byte outside the set |
\*, \?, \[ |
Escaped literal byte |
- is literal at either edge of a class or when escaped. A leading ! is an
ordinary byte.
All default to false except escapes.
pathname—*,?, and[...]never match/;/must be matched literallyperiod— a leading.(at the start, or after/withpathname) must be matched by a literal.or\.ignore_case— fold ASCII letter caseescapes = true—\escapes the next pattern byte, inside and outside brackets
zig fmt --check .
zig build test --summary all
zig build docsMIT licensed. See LICENSE and CONTRIBUTING.md.