Skip to content

Commit

Permalink
Merge pull request #48 from Dinastyh/fix-windows-segmentation-error
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
sam701 authored Sep 13, 2024
2 parents 3fc7d85 + 45a67a9 commit f6925c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
15 changes: 14 additions & 1 deletion src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ test "long option" {

try runOptions(&.{ "cmd", "--aa", "val" }, &.{opt});
try std.testing.expectEqualStrings("val", aa);
alloc.free(aa);

try runOptions(&.{ "cmd", "--aa=bb" }, &.{opt});
try std.testing.expectEqualStrings("bb", aa);
alloc.free(aa);
}

test "short option" {
Expand All @@ -89,9 +91,11 @@ test "short option" {

try runOptions(&.{ "abc", "-a", "val" }, &.{opt});
try std.testing.expectEqualStrings("val", aa);
alloc.free(aa);

try runOptions(&.{ "abc", "-a=bb" }, &.{opt});
try std.testing.expectEqualStrings("bb", aa);
alloc.free(aa);
}

test "concatenated aliases" {
Expand All @@ -114,6 +118,7 @@ test "concatenated aliases" {

try runOptions(&.{ "abc", "-ba", "val" }, &.{ opt, bbopt });
try std.testing.expectEqualStrings("val", aa);
alloc.free(aa);
try expect(bb);
}

Expand Down Expand Up @@ -231,10 +236,13 @@ test "string list" {
try runOptions(&.{ "abc", "--aa=a1", "--aa", "a2", "-a", "a3", "-a=a4" }, &.{aa_opt});
try expect(aa.len == 4);
try std.testing.expectEqualStrings("a1", aa[0]);
alloc.free(aa[0]);
try std.testing.expectEqualStrings("a2", aa[1]);
alloc.free(aa[1]);
try std.testing.expectEqualStrings("a3", aa[2]);
alloc.free(aa[2]);
try std.testing.expectEqualStrings("a4", aa[3]);

alloc.free(aa[3]);
alloc.free(aa);
}

Expand Down Expand Up @@ -271,12 +279,17 @@ test "mix positional arguments and options" {
defer std.testing.allocator.free(args);

try std.testing.expectEqualStrings("val", aav);
alloc.free(aav);
try std.testing.expectEqualStrings("tt", bbv);
alloc.free(bbv);
try std.testing.expect(arg1 == 178);
try std.testing.expectEqual(@as(usize, 3), args.len);
try std.testing.expectEqualStrings("arg2", args[0]);
alloc.free(args[0]);
try std.testing.expectEqualStrings("--arg3", args[1]);
alloc.free(args[1]);
try std.testing.expectEqualStrings("-arg4", args[2]);
alloc.free(args[2]);
}

test "parse enums" {
Expand Down
22 changes: 14 additions & 8 deletions src/value_parser.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");
const Allocator = std.mem.Allocator;

pub const ValueParseError = error{
InvalidValue,
};
pub const ValueParser = *const fn (dest: *anyopaque, value: []const u8) ValueParseError!void;
} || std.mem.Allocator.Error;
pub const ValueParser = *const fn (dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void;

pub const ValueData = struct {
value_size: usize,
Expand Down Expand Up @@ -35,7 +36,8 @@ fn intData(comptime ValueType: type, comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @alignCast(@ptrCast(dest));
dt.* = std.fmt.parseInt(ValueType, value, 10) catch return error.InvalidValue;
}
Expand All @@ -48,7 +50,8 @@ fn floatData(comptime ValueType: type, comptime DestinationType: type) ValueData
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = std.fmt.parseFloat(ValueType, value) catch return error.InvalidValue;
}
Expand All @@ -65,7 +68,8 @@ fn boolData(comptime DestinationType: type) ValueData {
.value_size = @sizeOf(DestinationType),
.is_bool = true,
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
const dt: *DestinationType = @ptrCast(@alignCast(dest));

if (std.mem.eql(u8, value, str_true)) {
Expand All @@ -83,9 +87,10 @@ fn stringData(comptime DestinationType: type) ValueData {
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
const dt: *DestinationType = @ptrCast(@alignCast(dest));
dt.* = value;
const cpy = try alloc.dupe(u8, value);
dt.* = cpy;
}
}.parser,
.type_name = "string",
Expand All @@ -97,7 +102,8 @@ fn enumData(comptime ValueType: type, comptime DestinationType: type) ValueData
return .{
.value_size = @sizeOf(DestinationType),
.value_parser = struct {
fn parser(dest: *anyopaque, value: []const u8) ValueParseError!void {
fn parser(dest: *anyopaque, value: []const u8, alloc: Allocator) ValueParseError!void {
_ = alloc;
inline for (edata.fields) |field| {
if (std.mem.eql(u8, field.name, value)) {
const dt: *DestinationType = @ptrCast(@alignCast(dest));
Expand Down
4 changes: 2 additions & 2 deletions src/value_ref.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub const ValueRef = struct {
self.element_count += 1;
switch (self.value_type) {
.single => {
return self.value_data.value_parser(self.dest, value);
return self.value_data.value_parser(self.dest, value, alloc);
},
.multi => |*list| {
if (list.list_ptr == null) {
list.list_ptr = try list.vtable.createList(alloc);
}
const value_ptr = try list.vtable.addOne(list.list_ptr.?, alloc);
try self.value_data.value_parser(value_ptr, value);
try self.value_data.value_parser(value_ptr, value, alloc);
},
}
}
Expand Down

0 comments on commit f6925c3

Please sign in to comment.