Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Feb 8, 2025
1 parent ff60e8b commit 6e7bdb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
17 changes: 15 additions & 2 deletions src/cli/filter_arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,23 @@ pub const FilterSet = struct {
var codepointer_iter = strings.UnsignedCodepointIterator.init(filter_utf8);
var cursor = strings.UnsignedCodepointIterator.Cursor{};
while (codepointer_iter.next(&cursor)) {
if (cursor.c == @as(u32, '\\')) {
if (bun.Environment.isWindows and cursor.c == @as(u32, '\\')) dont_forward_slash: {
const prev = cursor;
const is_separator = if (codepointer_iter.next(&cursor)) |next| switch (next) {
'[', '{', '!', '*', '?' => false,
else => true,
} else true;
cursor = prev;

if (!is_separator) {
break :dont_forward_slash;
}

try filter_utf32.append(self.allocator, '/');
continue;
} else {
try filter_utf32.append(self.allocator, cursor.c);
}
try filter_utf32.append(self.allocator, cursor.c);
}
self.has_name_filters = self.has_name_filters or !is_path;
try list.append(.{
Expand Down
41 changes: 15 additions & 26 deletions src/glob/ascii.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub fn match(alloc: Allocator, glob: []const u8, path: []const u8) MatchResult {
}

inline fn globMatchImpl(state: *State, glob: []const u8, path: []const u8) bool {
debugDisallowWindowsBackslashPatterns(glob);
while (state.glob_index < glob.len or state.path_index < path.len) {
if (state.glob_index < glob.len) {
switch (glob[state.glob_index]) {
Expand Down Expand Up @@ -270,6 +271,20 @@ inline fn globMatchImpl(state: *State, glob: []const u8, path: []const u8) bool
return true;
}

fn debugDisallowWindowsBackslashPatterns(glob: []const u8) void {
if (comptime bun.Environment.isWindows) {
var i: usize = 0;
while (i < glob.len) : (i += 1) {
if (glob[i] == '\\' and i + 1 < glob.len and switch (glob[i + 1]) {
'[', '{', '!', '*', '?' => false,
else => true,
}) {
@panic("Please do not use Windows backslashes in glob patterns.");
}
}
}
}

fn matchBrace(state: *State, glob: []const u8, path: []const u8) bool {
if (state.depth + 1 > BRACE_DEPTH_MAX) {
return false; // Invalid pattern! Too many nested braces
Expand Down Expand Up @@ -479,29 +494,3 @@ const BraceIndex = struct {
start: u32 = 0,
end: u32 = 0,
};

// TODO: Seems like this is unused, consider removing
pub fn preprocess_glob(glob: []const u8, brace_indices: *[10]BraceIndex, brace_indices_len: *u8, search_count: *u8, i: *u32) ?u32 {
while (i.* < glob.len) {
const c = glob[i];
switch (c) {
'{' => {
if (brace_indices_len.* == brace_indices.len) continue;
const stack_idx = brace_indices_len.*;
if (i == glob.len - 1) continue;
const matching_idx = preprocess_glob(glob[i + 1 ..], brace_indices, brace_indices_len, search_count + 1);
if (matching_idx) |idx| {
if (brace_indices_len.* == brace_indices.len) continue;
brace_indices[stack_idx].start = @intCast(i);
brace_indices[stack_idx].end = @as(u32, @intCast(i)) + idx + 1;
brace_indices_len.* += 1;
}
},
'}' => {
if (search_count > 0) return @intCast(i);
},
else => {},
}
}
return null;
}

0 comments on commit 6e7bdb0

Please sign in to comment.