autofix: tighten up getCaptureLoc()

* reject missing opening '|'
* reject capture group of only spaces
* add tests for these cases
This commit is contained in:
Travis Staloch 2023-05-26 23:37:34 -07:00
parent 32dc6c32ea
commit 58bc245ba3
No known key found for this signature in database
GPG Key ID: 9726F5C64475E635

View File

@ -478,7 +478,8 @@ fn getCaptureLoc(text: []const u8, loc: offsets.Loc) ?offsets.Loc {
const start_pipe_position = blk: { const start_pipe_position = blk: {
var i = loc.start; var i = loc.start;
while (true) : (i -= 1) { while (true) : (i -= 1) {
if (text[i] == '|' or i == 0) break; if (text[i] == '|') break;
if (i == 0) return null;
} }
break :blk i; break :blk i;
}; };
@ -486,6 +487,9 @@ fn getCaptureLoc(text: []const u8, loc: offsets.Loc) ?offsets.Loc {
const end_pipe_position = (std.mem.indexOfScalarPos(u8, text, start_pipe_position + 1, '|') orelse const end_pipe_position = (std.mem.indexOfScalarPos(u8, text, start_pipe_position + 1, '|') orelse
return null) + 1; return null) + 1;
const trimmed = std.mem.trim(u8, text[start_pipe_position + 1 .. end_pipe_position - 1], &std.ascii.whitespace);
if (trimmed.len == 0) return null;
return .{ .start = start_pipe_position, .end = end_pipe_position }; return .{ .start = start_pipe_position, .end = end_pipe_position };
} }
@ -504,10 +508,13 @@ test "getCaptureLoc" {
const captext = text[caploc.start..caploc.end]; const captext = text[caploc.start..caploc.end];
try std.testing.expectEqualStrings(text, captext); try std.testing.expectEqualStrings(text, captext);
} }
{
const caploc = getCaptureLoc("||", .{ .start = 1, .end = 2 }); try std.testing.expect(getCaptureLoc("||", .{ .start = 1, .end = 2 }) == null);
try std.testing.expect(caploc == null); try std.testing.expect(getCaptureLoc(" |", .{ .start = 1, .end = 2 }) == null);
} try std.testing.expect(getCaptureLoc("| ", .{ .start = 1, .end = 2 }) == null);
try std.testing.expect(getCaptureLoc("||", .{ .start = 1, .end = 1 }) == null);
try std.testing.expect(getCaptureLoc("| |", .{ .start = 1, .end = 3 }) == null);
try std.testing.expect(getCaptureLoc("| |", .{ .start = 1, .end = 6 }) == null);
} }
fn isSymbolChar(char: u8) bool { fn isSymbolChar(char: u8) bool {