Skip to content

Commit 2cb14ac

Browse files
committed
Limit regex match to one line, accept identicals
This commit makes two small tweaks, each likely to improve the behavior of the library. The first is to prevent any newlines in a regex match. They're invalid in a regex and would indicate an overbroad matched region. The second is to accept a diff if the matched regex is identical to the text it should 'match'. Doing otherwise would be creating issues where there were none, since the point is to match the old and the new.
1 parent fbcf6cf commit 2cb14ac

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/ohsnap.zig

+8-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn snapfmt(ohsnap: OhSnap, location: SourceLocation, text: []const u8) Snap
8787
}
8888

8989
// Regex for detecting embedded regexen
90-
const ignore_regex_string = "<\\^.+?\\$>";
90+
const ignore_regex_string = "<\\^[^\n]+?\\$>";
9191
const regex_finder = mvzr.compile(ignore_regex_string).?;
9292

9393
pub const Snap = struct {
@@ -216,12 +216,17 @@ pub const Snap = struct {
216216
const snap_end = found.end;
217217
const got_start = diffz.diffIndex(diffs.*, snap_start);
218218
const got_end = diffz.diffIndex(diffs.*, snap_end);
219+
// Check if these are identical (use/mention distinction!)
220+
if (std.mem.eql(u8, found.slice, got[got_start..got_end])) {
221+
// That's fine then
222+
continue :regex_while;
223+
}
219224
// Trim the angle brackets off the regex.
220225
const exclude_regex = found.slice[1 .. found.slice.len - 1];
221226
const maybe_matcher = UserRegex.compile(exclude_regex);
222227
if (maybe_matcher == null) {
223-
std.debug.print("issue with mvzr or regex, hard to say.\n", .{});
224-
break :regex_while;
228+
std.debug.print("Issue with mvzr or regex, hard to say. Regex string: {s}\n", .{exclude_regex});
229+
continue :regex_while;
225230
}
226231
const matcher = maybe_matcher.?;
227232
const maybe_match = matcher.match(got[got_start..got_end]);

0 commit comments

Comments
 (0)