Compare commits

..

No commits in common. "1c906217083ae2cb971f17fc077d514707b9d0b1" and "1f6cb4eb0cb0c5be4714d991fd4a622708e1b5c7" have entirely different histories.

4 changed files with 15 additions and 22 deletions

View file

@ -12,7 +12,7 @@ jobs:
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup Zig - name: Setup Zig
uses: https://codeberg.org/mlugg/setup-zig@v2.2.1 uses: https://github.com/mlugg/setup-zig@v2.0.5
- name: Build project - name: Build project
run: zig build --summary all run: zig build --summary all
- name: Run tests - name: Run tests

View file

@ -1,5 +1,5 @@
[tools] [tools]
zig = "0.15.2" zig = "0.15.1"
zls = "0.15.1" zls = "0.15.0"
prek = "0.3.1" pre-commit = "latest"
"ubi:DonIsaac/zlint" = "0.7.9" "ubi:DonIsaac/zlint" = "latest"

View file

@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks # See https://pre-commit.com/hooks.html for more hooks
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0 rev: v3.2.0
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer

View file

@ -9,7 +9,7 @@ pub fn main() !u8 {
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var stderr_writer = std.fs.File.stdout().writer(&.{}); var stderr_writer = std.fs.File.stdout().writer(&.{});
const stdout = &stdout_writer.interface; const stdout = &stdout_writer.interface;
defer stdout.flush() catch @panic("flush failed!"); defer stdout.flush() catch {};
const stderr = &stderr_writer.interface; const stderr = &stderr_writer.interface;
const args = try std.process.argsAlloc(allocator); const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args); defer std.process.argsFree(allocator, args);
@ -89,17 +89,7 @@ const Substitution = struct {
fn substituteLines(reader: *std.Io.Reader, writer: *std.Io.Writer, substitutions: []const Substitution) !usize { fn substituteLines(reader: *std.Io.Reader, writer: *std.Io.Writer, substitutions: []const Substitution) !usize {
var subs: usize = 0; var subs: usize = 0;
var first_line = true; while (reader.takeDelimiterExclusive('\n')) |line| {
while (reader.takeDelimiter('\n')) |line_opt| {
// takeDelimiter returns null at EOF
const line = line_opt orelse break;
// Write newline before each line except the first
if (!first_line) {
try writer.writeByte('\n');
}
first_line = false;
var substituted = false; var substituted = false;
for (substitutions) |sub| { for (substitutions) |sub| {
if (std.mem.eql(u8, line, sub.original)) { if (std.mem.eql(u8, line, sub.original)) {
@ -111,6 +101,9 @@ fn substituteLines(reader: *std.Io.Reader, writer: *std.Io.Writer, substitutions
} }
if (!substituted) if (!substituted)
try writer.writeAll(line); try writer.writeAll(line);
if (reader.peekByte() != error.EndOfStream)
try writer.writeByte('\n');
} else |err| if (err != error.EndOfStream) return err; } else |err| if (err != error.EndOfStream) return err;
return subs; return subs;
} }
@ -127,7 +120,7 @@ test "substitute lines exact match" {
.{ .original = "replace_me", .replacement = "new_line" }, .{ .original = "replace_me", .replacement = "new_line" },
}; };
_ = try substituteLines( try substituteLines(
&input_stream, &input_stream,
&output_stream, &output_stream,
&substitutions, &substitutions,
@ -148,7 +141,7 @@ test "no match found" {
.{ .original = "nonexistent", .replacement = "replacement" }, .{ .original = "nonexistent", .replacement = "replacement" },
}; };
_ = try substituteLines( try substituteLines(
&input_stream, &input_stream,
&output_stream, &output_stream,
&substitutions, &substitutions,
@ -170,7 +163,7 @@ test "partial match not replaced" {
.{ .original = "match", .replacement = "replaced" }, .{ .original = "match", .replacement = "replaced" },
}; };
_ = try substituteLines( try substituteLines(
&input_stream, &input_stream,
&output_stream, &output_stream,
&substitutions, &substitutions,
@ -193,7 +186,7 @@ test "multiple substitutions" {
.{ .original = "replace_also", .replacement = "also_new" }, .{ .original = "replace_also", .replacement = "also_new" },
}; };
_ = try substituteLines( try substituteLines(
&input_stream, &input_stream,
&output_stream, &output_stream,
&substitutions, &substitutions,