xcaeser/glob.zig
Fast and reliable glob pattern matching in pure zig.
Fast and reliable glob pattern matching in pure zig.
*, ?, and character classes [abc], [a-z]! prefixmatchAny, matchAll)zig fetch --save=glob https://github.com/xcaeser/glob.zig/archive/v0.1.0.tar.gz
Add to your build.zig:
const glob_dep = b.dependency("glob", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("glob", glob_dep.module("glob"));
const std = @import("std");
const glob = @import("glob");
pub fn main() !void {
// Simple match
const matches = glob.match("*.zig", "main.zig");
std.debug.print("Matches: {}\n", .{matches}); // true
// Character class
const class_match = glob.match("test_[0-9].txt", "test_5.txt");
std.debug.print("Class match: {}\n", .{class_match}); // true
// Negation
const negated = glob.match("!*.tmp", "file.txt");
std.debug.print("Negated: {}\n", .{negated}); // true
// Multiple patterns
const patterns = &[_][]const u8{ "*.zig", "*.c", "*.h" };
const multi = glob.matchAny(patterns, "main.zig");
std.debug.print("Any match: {}\n", .{multi}); // true
// Validate pattern
glob.validate("[a-z]*") catch |err| {
std.debug.print("Invalid pattern: {}\n", .{err});
return;
};
}
match(pattern: []const u8, text: []const u8) boolMatches text against a glob pattern.
Supported wildcards:
* โ matches any number of characters? โ matches any single character[abc] โ matches one character from the set[a-z] โ matches one character from the range! โ negates the pattern (must be first character)validate(pattern: []const u8) !voidValidates a pattern for syntax errors.
matchAny(patterns: []const []const u8, text: []const u8) boolReturns true if text matches any of the patterns.
matchAll(patterns: []const []const u8, text: []const u8) boolReturns true if text matches all of the patterns.
MIT. See LICENSE. Contributions welcome.