summaryrefslogtreecommitdiff
path: root/src/glob.zig
blob: 83d9fcb90f1e872418c61294aca24932f520f9c9 (plain)
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
//! Minimal glob matcher for tool/extension allow- and deny-lists.
//!
//! Patterns and names are dot-delimited segments (e.g. `std.git.status`).
//! Matching operates on whole segments, never on individual characters.
//!
//! Supported segment wildcards:
//!   - `*` matches exactly one segment. `std.*` matches `std.read` but
//!     not `std.git.status` (greedy within a segment, but not
//!     dot-agnostic).
//!   - `**` matches one or more segments. `std.**` matches both
//!     `std.read` and `std.git.status`.
//!
//! A wildcard is only recognized as a whole segment: `std.*` and `std.**`
//! are wildcards, but `std.rea*` is a literal segment (the `*` has no
//! special meaning mid-segment). Everything else is matched literally.

const std = @import("std");

/// Returns true if `name` matches `pattern` under the segment rules above.
pub fn match(pattern: []const u8, name: []const u8) bool {
    var pat_it = std.mem.splitScalar(u8, pattern, '.');
    var name_it = std.mem.splitScalar(u8, name, '.');
    return matchSegments(&pat_it, &name_it);
}

const SegmentIter = std.mem.SplitIterator(u8, .scalar);

fn matchSegments(pat_it: *SegmentIter, name_it: *SegmentIter) bool {
    while (pat_it.next()) |seg| {
        if (std.mem.eql(u8, seg, "**")) {
            // `**` matches one or more remaining segments. Try the
            // shortest match first (one segment), then extend.
            const rest_pat = pat_it.*; // pattern after `**`
            while (name_it.next()) |_| {
                var pat_copy = rest_pat;
                var name_copy = name_it.*;
                if (matchSegments(&pat_copy, &name_copy)) return true;
            }
            // `**` requires at least one segment, and the trailing
            // pattern must have matched the remainder above.
            return false;
        }

        const name_seg = name_it.next() orelse return false;
        if (std.mem.eql(u8, seg, "*")) {
            // Matches exactly one (any) segment.
            continue;
        }
        if (!std.mem.eql(u8, seg, name_seg)) return false;
    }

    // Pattern exhausted: match iff name is also exhausted.
    return name_it.next() == null;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

const testing = std.testing;

test "literal match" {
    try testing.expect(match("std.read", "std.read"));
    try testing.expect(!match("std.read", "std.write"));
    try testing.expect(!match("std.read", "std.read.more"));
    try testing.expect(!match("std.read.more", "std.read"));
}

test "single star matches exactly one segment" {
    try testing.expect(match("std.*", "std.read"));
    try testing.expect(match("std.*", "std.write"));
    try testing.expect(!match("std.*", "std.git.status"));
    try testing.expect(!match("std.*", "std"));
    try testing.expect(!match("std.*", "other.read"));
}

test "double star is dot-agnostic, one or more segments" {
    try testing.expect(match("std.**", "std.read"));
    try testing.expect(match("std.**", "std.git.status"));
    try testing.expect(!match("std.**", "std"));
    try testing.expect(!match("std.**", "other.read"));
}

test "bare single star matches one segment" {
    try testing.expect(match("*", "std"));
    try testing.expect(!match("*", "std.read"));
    // An empty name is a single empty segment, which `*` matches.
    try testing.expect(match("*", ""));
}

test "bare double star matches everything non-empty" {
    try testing.expect(match("**", "std"));
    try testing.expect(match("**", "std.read"));
    try testing.expect(match("**", "std.git.status"));
}

test "star in middle" {
    try testing.expect(match("*.read", "std.read"));
    try testing.expect(match("*.*", "std.read"));
    try testing.expect(!match("*.*", "std.git.status"));
    try testing.expect(match("std.*.status", "std.git.status"));
    try testing.expect(!match("std.*.status", "std.status"));
}

test "double star in middle" {
    try testing.expect(match("std.**.status", "std.git.status"));
    try testing.expect(match("std.**.status", "std.a.b.status"));
    try testing.expect(!match("std.**.status", "std.status"));
    try testing.expect(match("**.status", "std.git.status"));
}

test "wildcard only recognized as whole segment" {
    // A `*` embedded in a segment is a literal, not a wildcard.
    try testing.expect(match("std.rea*", "std.rea*"));
    try testing.expect(!match("std.rea*", "std.read"));
}

test "empty pattern only matches empty name" {
    try testing.expect(match("", ""));
    try testing.expect(!match("", "x"));
}