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
uses: actions/checkout@v4
- 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
run: zig build --summary all
- name: Run tests

View file

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

View file

@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
rev: v3.2.0
hooks:
- id: trailing-whitespace
- 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 stderr_writer = std.fs.File.stdout().writer(&.{});
const stdout = &stdout_writer.interface;
defer stdout.flush() catch @panic("flush failed!");
defer stdout.flush() catch {};
const stderr = &stderr_writer.interface;
const args = try std.process.argsAlloc(allocator);
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 {
var subs: usize = 0;
var first_line = true;
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;
while (reader.takeDelimiterExclusive('\n')) |line| {
var substituted = false;
for (substitutions) |sub| {
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)
try writer.writeAll(line);
if (reader.peekByte() != error.EndOfStream)
try writer.writeByte('\n');
} else |err| if (err != error.EndOfStream) return err;
return subs;
}
@ -127,7 +120,7 @@ test "substitute lines exact match" {
.{ .original = "replace_me", .replacement = "new_line" },
};
_ = try substituteLines(
try substituteLines(
&input_stream,
&output_stream,
&substitutions,
@ -148,7 +141,7 @@ test "no match found" {
.{ .original = "nonexistent", .replacement = "replacement" },
};
_ = try substituteLines(
try substituteLines(
&input_stream,
&output_stream,
&substitutions,
@ -170,7 +163,7 @@ test "partial match not replaced" {
.{ .original = "match", .replacement = "replaced" },
};
_ = try substituteLines(
try substituteLines(
&input_stream,
&output_stream,
&substitutions,
@ -193,7 +186,7 @@ test "multiple substitutions" {
.{ .original = "replace_also", .replacement = "also_new" },
};
_ = try substituteLines(
try substituteLines(
&input_stream,
&output_stream,
&substitutions,