Skip to content

Commit

Permalink
fix: fix variable substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Jun 24, 2023
1 parent 089893a commit 901a0da
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/parser.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const std = @import("std");
const testing = std.testing;
const Error = @import("./error.zig").Error;

const testing = std.testing;

const SubstitutionMode = enum {
none,
block,
Expand Down Expand Up @@ -162,6 +163,7 @@ pub const LineParser = struct {
substitution_mode = .escaped_block;
} else {
try applySubstitution(&self.ctx, name_buf.items, output);
name_buf.clearRetainingCapacity();
if (c == '$') {
if (!strong_quote and !escaped) {
substitution_mode = .block;
Expand All @@ -178,6 +180,7 @@ pub const LineParser = struct {
if (c == '}') {
substitution_mode = .none;
try applySubstitution(&self.ctx, name_buf.items, output);
name_buf.clearRetainingCapacity();
} else {
try substitution_name.writeByte(c);
}
Expand Down Expand Up @@ -215,6 +218,7 @@ pub const LineParser = struct {
return Error.InvalidValue;
} else {
try applySubstitution(&self.ctx, name_buf.items, output);
name_buf.clearRetainingCapacity();
return output_buf.toOwnedSlice();
}
}
Expand Down Expand Up @@ -284,6 +288,7 @@ test "test parse" {
\\KEY7= # foo
\\KEY8 ="whitespace before ="
\\KEY9= "whitespace after ="
\\KEY10=${KEY0}?${KEY1}
;

const expect = [_]?[]const u8{
Expand All @@ -297,6 +302,7 @@ test "test parse" {
null,
"whitespace before =",
"whitespace after =",
"0?1",
};

var parser = LineParser.init(allocator);
Expand All @@ -320,7 +326,7 @@ test "test parse" {
try testing.expect(value == null);
} else {
const value = parser.ctx.get(key).?.?;
try testing.expectEqualStrings(value, expect[i].?);
try testing.expectEqualStrings(expect[i].?, value);
}

i += 1;
Expand Down

0 comments on commit 901a0da

Please sign in to comment.