Change ArgIterator.next() return type

Changes the return type of `ArgIterator.next()` from
`?(NextError![:0]u8)` to `NextError!?[:0]u8`.
This commit is contained in:
ominitay 2021-12-19 22:06:43 +00:00 committed by Andrew Kelley
parent 0d09b87c14
commit 7e16bb36d8
4 changed files with 20 additions and 23 deletions

View file

@ -27,18 +27,18 @@ pub fn main() !void {
if (!args_it.skip()) @panic("expected self arg");
const zig_exe = try (args_it.next(allocator) orelse @panic("expected zig exe arg"));
const zig_exe = (try args_it.next(allocator)) orelse @panic("expected zig exe arg");
defer allocator.free(zig_exe);
const in_file_name = try (args_it.next(allocator) orelse @panic("expected input arg"));
const in_file_name = (try args_it.next(allocator)) orelse @panic("expected input arg");
defer allocator.free(in_file_name);
const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg"));
const out_file_name = (try args_it.next(allocator)) orelse @panic("expected output arg");
defer allocator.free(out_file_name);
var do_code_tests = true;
if (args_it.next(allocator)) |arg| {
if (mem.eql(u8, try arg, "--skip-code-tests")) {
if (try args_it.next(allocator)) |arg| {
if (mem.eql(u8, arg, "--skip-code-tests")) {
do_code_tests = false;
} else {
@panic("unrecognized arg");

View file

@ -325,7 +325,7 @@ pub const ArgIteratorWindows = struct {
}
/// You must free the returned memory when done.
pub fn next(self: *ArgIteratorWindows, allocator: Allocator) ?(NextError![:0]u8) {
pub fn next(self: *ArgIteratorWindows, allocator: Allocator) NextError!?[:0]u8 {
// march forward over whitespace
while (true) : (self.index += 1) {
const character = self.getPointAtIndex();
@ -336,7 +336,7 @@ pub const ArgIteratorWindows = struct {
}
}
return self.internalNext(allocator);
return try self.internalNext(allocator);
}
pub fn skip(self: *ArgIteratorWindows) bool {
@ -474,11 +474,11 @@ pub const ArgIterator = struct {
pub const NextError = ArgIteratorWindows.NextError;
/// You must free the returned memory when done.
pub fn next(self: *ArgIterator, allocator: Allocator) ?(NextError![:0]u8) {
pub fn next(self: *ArgIterator, allocator: Allocator) NextError!?[:0]u8 {
if (builtin.os.tag == .windows) {
return self.inner.next(allocator);
} else {
return allocator.dupeZ(u8, self.inner.next() orelse return null);
return try allocator.dupeZ(u8, self.inner.next() orelse return null);
}
}
@ -522,7 +522,7 @@ test "args iterator" {
var it = if (builtin.os.tag == .wasi) try argsWithAllocator(ga) else args();
defer it.deinit(); // no-op unless WASI
const prog_name = try it.next(ga) orelse unreachable;
const prog_name = (try it.next(ga)) orelse unreachable;
defer ga.free(prog_name);
const expected_suffix = switch (builtin.os.tag) {
@ -534,7 +534,7 @@ test "args iterator" {
try testing.expect(mem.eql(u8, expected_suffix, given_suffix));
try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner
try testing.expect(it.next(ga) == null);
try testing.expect((try it.next(ga)) == null);
try testing.expect(!it.skip());
}
@ -550,8 +550,7 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 {
var slice_list = std.ArrayList(usize).init(allocator);
defer slice_list.deinit();
while (it.next(allocator)) |arg_or_err| {
const arg = try arg_or_err;
while (try it.next(allocator)) |arg| {
defer allocator.free(arg);
try contents.appendSlice(arg[0 .. arg.len + 1]);
try slice_list.append(arg.len);
@ -610,11 +609,11 @@ test "windows arg parsing" {
fn testWindowsCmdLine(input_cmd_line: [*]const u16, expected_args: []const []const u8) !void {
var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
for (expected_args) |expected_arg| {
const arg = it.next(std.testing.allocator).? catch unreachable;
const arg = (it.next(std.testing.allocator) catch unreachable).?;
defer std.testing.allocator.free(arg);
try testing.expectEqualStrings(expected_arg, arg);
}
try testing.expect(it.next(std.testing.allocator) == null);
try testing.expect((try it.next(std.testing.allocator)) == null);
}
pub const UserInfo = struct {

View file

@ -18,14 +18,14 @@ pub fn main() !void {
a = arena.allocator();
const zig_exe_rel = try (arg_it.next(a) orelse {
const zig_exe_rel = (try arg_it.next(a)) orelse {
std.debug.print("Expected first argument to be path to zig compiler\n", .{});
return error.InvalidArgs;
});
const cache_root = try (arg_it.next(a) orelse {
};
const cache_root = (try arg_it.next(a)) orelse {
std.debug.print("Expected second argument to be cache root directory path\n", .{});
return error.InvalidArgs;
});
};
const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});
const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });

View file

@ -362,8 +362,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ const stdout = io.getStdOut().writer();
\\ var index: usize = 0;
\\ _ = args_it.skip();
\\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {
\\ const arg = try arg_or_err;
\\ while (try args_it.next(allocator)) |arg| : (index += 1) {
\\ try stdout.print("{}: {s}\n", .{index, arg});
\\ }
\\}
@ -401,8 +400,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ const stdout = io.getStdOut().writer();
\\ var index: usize = 0;
\\ _ = args_it.skip();
\\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {
\\ const arg = try arg_or_err;
\\ while (try args_it.next(allocator)) |arg| : (index += 1) {
\\ try stdout.print("{}: {s}\n", .{index, arg});
\\ }
\\}