Skip to content

xcaeser/glob.zig

Repository files navigation

📂 glob.zig - a powerful glob matcher

Fast and reliable glob pattern matching in pure zig.

glob.zig reference docs

Tests Zig Version License: MIT Built by xcaeser Version

🚀 Features

  • Fast glob pattern matching with *, ?, and character classes [abc], [a-z]
  • Negation support with ! prefix
  • Pattern validation for common errors
  • Multiple pattern matching (matchAny, matchAll)
  • No dependencies, pure Zig
  • Comprehensive test suite

📦 Installation

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"));

🧪 Example

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;
    };
}

📚 API

match(pattern: []const u8, text: []const u8) bool

Matches 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) !void

Validates a pattern for syntax errors.

matchAny(patterns: []const []const u8, text: []const u8) bool

Returns true if text matches any of the patterns.

matchAll(patterns: []const []const u8, text: []const u8) bool

Returns true if text matches all of the patterns.

📝 License

MIT. See LICENSE. Contributions welcome.

About

Fast and reliable glob pattern matching in pure zig.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published