source: splice lines by copying the runs between backslashes

A file with any backslash (most headers: macro continuations) was copied
a byte at a time. The runs between backslashes are now found with
indexOfScalarPos (vectorized) and copied whole.

The hill-climbing set: preprocessing -5 to -8%; output unchanged on the
verify corpus.
This commit is contained in:
peterino2 2026-11-03 09:16:19 -08:00
parent 9f8c46792b
commit 3833ea57d0
1 changed files with 32 additions and 16 deletions

View File

@ -318,30 +318,46 @@ fn decodeUtf16(a: Allocator, raw: []const u8) !?[]const u8 {
} }
fn spliceLines(a: Allocator, raw: []const u8) ![]const u8 { fn spliceLines(a: Allocator, raw: []const u8) ![]const u8 {
if (std.mem.indexOfScalar(u8, raw, '\\') == null) return raw; var i = std.mem.indexOfScalar(u8, raw, '\\') orelse return raw;
var out: std.ArrayList(u8) = .empty; var out: std.ArrayList(u8) = .empty;
try out.ensureTotalCapacity(a, raw.len); try out.ensureTotalCapacity(a, raw.len);
var i: usize = 0; out.appendSliceAssumeCapacity(raw[0..i]);
// At a backslash: a splice, or the backslash kept; then the run to the
// next backslash, copied whole.
while (i < raw.len) { while (i < raw.len) {
if (raw[i] == '\\') {
var j = i + 1; var j = i + 1;
// Clang tolerates whitespace between the backslash and newline. // Clang tolerates whitespace between the backslash and newline.
while (j < raw.len and (raw[j] == ' ' or raw[j] == '\t')) j += 1; while (j < raw.len and (raw[j] == ' ' or raw[j] == '\t')) j += 1;
if (j < raw.len and raw[j] == '\n') { if (j < raw.len and raw[j] == '\n') {
i = j + 1; i = j + 1;
continue; } else if (j + 1 < raw.len and raw[j] == '\r' and raw[j + 1] == '\n') {
}
if (j + 1 < raw.len and raw[j] == '\r' and raw[j + 1] == '\n') {
i = j + 2; i = j + 2;
continue; } else {
} out.appendAssumeCapacity('\\');
}
out.appendAssumeCapacity(raw[i]);
i += 1; i += 1;
} }
const next = std.mem.indexOfScalarPos(u8, raw, i, '\\') orelse raw.len;
out.appendSliceAssumeCapacity(raw[i..next]);
i = next;
}
return out.items; return out.items;
} }
test spliceLines {
const cases = [_][2][]const u8{
.{ "a\\\nb", "ab" },
.{ "a\\ \t\r\nb\\", "ab\\" },
.{ "\\x\\\\\ny", "\\x\\y" },
.{ "no splice", "no splice" },
.{ "#define A 1 \\\n + 2\n", "#define A 1 + 2\n" },
};
for (cases) |c| {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
try std.testing.expectEqualStrings(c[1], try spliceLines(arena.allocator(), c[0]));
}
}
test "line and column lookup" { test "line and column lookup" {
var mfs: MemoryFs = .{}; var mfs: MemoryFs = .{};
var sm = SourceManager.init(std.testing.allocator, mfs.fileSystem()); var sm = SourceManager.init(std.testing.allocator, mfs.fileSystem());