authorgravatar for 37453713+ominitay@users.noreply.github.comominitay <37453713+ominitay@users.noreply.github.com> 2021-12-19 22:06:43+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-21 11:15:33-08:00
log7e16bb36d82cf45cd5f6f4da38fba512554f66ed
tree72a943205528ec58a80d6939670cb3b2a1695fe0
parent0d09b87c1409660a9d541e7f2972480b4be137a5

Change `ArgIterator.next()` return type

Changes the return type of `ArgIterator.next()` from `?(NextError![:0]u8)` to `NextError!?[:0]u8`.

4 files changed, 20 insertions(+), 23 deletions(-)

doc/docgen.zig+5-5
......@@ -27,18 +27,18 @@ pub fn main() !void {
2727
2828 if (!args_it.skip()) @panic("expected self arg");
2929
30 const zig_exe = try (args_it.next(allocator) orelse @panic("expected zig exe arg"));
30 const zig_exe = (try args_it.next(allocator)) orelse @panic("expected zig exe arg");
3131 defer allocator.free(zig_exe);
3232
33 const in_file_name = try (args_it.next(allocator) orelse @panic("expected input arg"));
33 const in_file_name = (try args_it.next(allocator)) orelse @panic("expected input arg");
3434 defer allocator.free(in_file_name);
3535
36 const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg"));
36 const out_file_name = (try args_it.next(allocator)) orelse @panic("expected output arg");
3737 defer allocator.free(out_file_name);
3838
3939 var do_code_tests = true;
40 if (args_it.next(allocator)) |arg| {
41 if (mem.eql(u8, try arg, "--skip-code-tests")) {
40 if (try args_it.next(allocator)) |arg| {
41 if (mem.eql(u8, arg, "--skip-code-tests")) {
4242 do_code_tests = false;
4343 } else {
4444 @panic("unrecognized arg");
lib/std/process.zig+9-10
......@@ -325,7 +325,7 @@ pub const ArgIteratorWindows = struct {
325325 }
326326
327327 /// You must free the returned memory when done.
328 pub fn next(self: *ArgIteratorWindows, allocator: Allocator) ?(NextError![:0]u8) {
328 pub fn next(self: *ArgIteratorWindows, allocator: Allocator) NextError!?[:0]u8 {
329329 // march forward over whitespace
330330 while (true) : (self.index += 1) {
331331 const character = self.getPointAtIndex();
......@@ -336,7 +336,7 @@ pub const ArgIteratorWindows = struct {
336336 }
337337 }
338338
339 return self.internalNext(allocator);
339 return try self.internalNext(allocator);
340340 }
341341
342342 pub fn skip(self: *ArgIteratorWindows) bool {
......@@ -474,11 +474,11 @@ pub const ArgIterator = struct {
474474 pub const NextError = ArgIteratorWindows.NextError;
475475
476476 /// You must free the returned memory when done.
477 pub fn next(self: *ArgIterator, allocator: Allocator) ?(NextError![:0]u8) {
477 pub fn next(self: *ArgIterator, allocator: Allocator) NextError!?[:0]u8 {
478478 if (builtin.os.tag == .windows) {
479479 return self.inner.next(allocator);
480480 } else {
481 return allocator.dupeZ(u8, self.inner.next() orelse return null);
481 return try allocator.dupeZ(u8, self.inner.next() orelse return null);
482482 }
483483 }
484484
......@@ -522,7 +522,7 @@ test "args iterator" {
522522 var it = if (builtin.os.tag == .wasi) try argsWithAllocator(ga) else args();
523523 defer it.deinit(); // no-op unless WASI
524524
525 const prog_name = try it.next(ga) orelse unreachable;
525 const prog_name = (try it.next(ga)) orelse unreachable;
526526 defer ga.free(prog_name);
527527
528528 const expected_suffix = switch (builtin.os.tag) {
......@@ -534,7 +534,7 @@ test "args iterator" {
534534
535535 try testing.expect(mem.eql(u8, expected_suffix, given_suffix));
536536 try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner
537 try testing.expect(it.next(ga) == null);
537 try testing.expect((try it.next(ga)) == null);
538538 try testing.expect(!it.skip());
539539}
540540
......@@ -550,8 +550,7 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 {
550550 var slice_list = std.ArrayList(usize).init(allocator);
551551 defer slice_list.deinit();
552552
553 while (it.next(allocator)) |arg_or_err| {
554 const arg = try arg_or_err;
553 while (try it.next(allocator)) |arg| {
555554 defer allocator.free(arg);
556555 try contents.appendSlice(arg[0 .. arg.len + 1]);
557556 try slice_list.append(arg.len);
......@@ -610,11 +609,11 @@ test "windows arg parsing" {
610609fn testWindowsCmdLine(input_cmd_line: [*]const u16, expected_args: []const []const u8) !void {
611610 var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
612611 for (expected_args) |expected_arg| {
613 const arg = it.next(std.testing.allocator).? catch unreachable;
612 const arg = (it.next(std.testing.allocator) catch unreachable).?;
614613 defer std.testing.allocator.free(arg);
615614 try testing.expectEqualStrings(expected_arg, arg);
616615 }
617 try testing.expect(it.next(std.testing.allocator) == null);
616 try testing.expect((try it.next(std.testing.allocator)) == null);
618617}
619618
620619pub const UserInfo = struct {
test/cli.zig+4-4
......@@ -18,14 +18,14 @@ pub fn main() !void {
1818
1919 a = arena.allocator();
2020
21 const zig_exe_rel = try (arg_it.next(a) orelse {
21 const zig_exe_rel = (try arg_it.next(a)) orelse {
2222 std.debug.print("Expected first argument to be path to zig compiler\n", .{});
2323 return error.InvalidArgs;
24 });
25 const cache_root = try (arg_it.next(a) orelse {
24 };
25 const cache_root = (try arg_it.next(a)) orelse {
2626 std.debug.print("Expected second argument to be cache root directory path\n", .{});
2727 return error.InvalidArgs;
28 });
28 };
2929 const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});
3030
3131 const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });
test/compare_output.zig+2-4
......@@ -362,8 +362,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
362362 \\ const stdout = io.getStdOut().writer();
363363 \\ var index: usize = 0;
364364 \\ _ = args_it.skip();
365 \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {
366 \\ const arg = try arg_or_err;
365 \\ while (try args_it.next(allocator)) |arg| : (index += 1) {
367366 \\ try stdout.print("{}: {s}\n", .{index, arg});
368367 \\ }
369368 \\}
......@@ -401,8 +400,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
401400 \\ const stdout = io.getStdOut().writer();
402401 \\ var index: usize = 0;
403402 \\ _ = args_it.skip();
404 \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {
405 \\ const arg = try arg_or_err;
403 \\ while (try args_it.next(allocator)) |arg| : (index += 1) {
406404 \\ try stdout.print("{}: {s}\n", .{index, arg});
407405 \\ }
408406 \\}