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 {...@@ -27,18 +27,18 @@ pub fn main() !void {
2727
28 if (!args_it.skip()) @panic("expected self arg");28 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");
31 defer allocator.free(zig_exe);31 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");
34 defer allocator.free(in_file_name);34 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");
37 defer allocator.free(out_file_name);37 defer allocator.free(out_file_name);
3838
39 var do_code_tests = true;39 var do_code_tests = true;
40 if (args_it.next(allocator)) |arg| {40 if (try args_it.next(allocator)) |arg| {
41 if (mem.eql(u8, try arg, "--skip-code-tests")) {41 if (mem.eql(u8, arg, "--skip-code-tests")) {
42 do_code_tests = false;42 do_code_tests = false;
43 } else {43 } else {
44 @panic("unrecognized arg");44 @panic("unrecognized arg");
lib/std/process.zig+9-10
...@@ -325,7 +325,7 @@ pub const ArgIteratorWindows = struct {...@@ -325,7 +325,7 @@ pub const ArgIteratorWindows = struct {
325 }325 }
326326
327 /// You must free the returned memory when done.327 /// 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 {
329 // march forward over whitespace329 // march forward over whitespace
330 while (true) : (self.index += 1) {330 while (true) : (self.index += 1) {
331 const character = self.getPointAtIndex();331 const character = self.getPointAtIndex();
...@@ -336,7 +336,7 @@ pub const ArgIteratorWindows = struct {...@@ -336,7 +336,7 @@ pub const ArgIteratorWindows = struct {
336 }336 }
337 }337 }
338338
339 return self.internalNext(allocator);339 return try self.internalNext(allocator);
340 }340 }
341341
342 pub fn skip(self: *ArgIteratorWindows) bool {342 pub fn skip(self: *ArgIteratorWindows) bool {
...@@ -474,11 +474,11 @@ pub const ArgIterator = struct {...@@ -474,11 +474,11 @@ pub const ArgIterator = struct {
474 pub const NextError = ArgIteratorWindows.NextError;474 pub const NextError = ArgIteratorWindows.NextError;
475475
476 /// You must free the returned memory when done.476 /// 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 {
478 if (builtin.os.tag == .windows) {478 if (builtin.os.tag == .windows) {
479 return self.inner.next(allocator);479 return self.inner.next(allocator);
480 } else {480 } else {
481 return allocator.dupeZ(u8, self.inner.next() orelse return null);481 return try allocator.dupeZ(u8, self.inner.next() orelse return null);
482 }482 }
483 }483 }
484484
...@@ -522,7 +522,7 @@ test "args iterator" {...@@ -522,7 +522,7 @@ test "args iterator" {
522 var it = if (builtin.os.tag == .wasi) try argsWithAllocator(ga) else args();522 var it = if (builtin.os.tag == .wasi) try argsWithAllocator(ga) else args();
523 defer it.deinit(); // no-op unless WASI523 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;
526 defer ga.free(prog_name);526 defer ga.free(prog_name);
527527
528 const expected_suffix = switch (builtin.os.tag) {528 const expected_suffix = switch (builtin.os.tag) {
...@@ -534,7 +534,7 @@ test "args iterator" {...@@ -534,7 +534,7 @@ test "args iterator" {
534534
535 try testing.expect(mem.eql(u8, expected_suffix, given_suffix));535 try testing.expect(mem.eql(u8, expected_suffix, given_suffix));
536 try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner536 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);
538 try testing.expect(!it.skip());538 try testing.expect(!it.skip());
539}539}
540540
...@@ -550,8 +550,7 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 {...@@ -550,8 +550,7 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 {
550 var slice_list = std.ArrayList(usize).init(allocator);550 var slice_list = std.ArrayList(usize).init(allocator);
551 defer slice_list.deinit();551 defer slice_list.deinit();
552552
553 while (it.next(allocator)) |arg_or_err| {553 while (try it.next(allocator)) |arg| {
554 const arg = try arg_or_err;
555 defer allocator.free(arg);554 defer allocator.free(arg);
556 try contents.appendSlice(arg[0 .. arg.len + 1]);555 try contents.appendSlice(arg[0 .. arg.len + 1]);
557 try slice_list.append(arg.len);556 try slice_list.append(arg.len);
...@@ -610,11 +609,11 @@ test "windows arg parsing" {...@@ -610,11 +609,11 @@ test "windows arg parsing" {
610fn testWindowsCmdLine(input_cmd_line: [*]const u16, expected_args: []const []const u8) !void {609fn testWindowsCmdLine(input_cmd_line: [*]const u16, expected_args: []const []const u8) !void {
611 var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);610 var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line);
612 for (expected_args) |expected_arg| {611 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).?;
614 defer std.testing.allocator.free(arg);613 defer std.testing.allocator.free(arg);
615 try testing.expectEqualStrings(expected_arg, arg);614 try testing.expectEqualStrings(expected_arg, arg);
616 }615 }
617 try testing.expect(it.next(std.testing.allocator) == null);616 try testing.expect((try it.next(std.testing.allocator)) == null);
618}617}
619618
620pub const UserInfo = struct {619pub const UserInfo = struct {
test/cli.zig+4-4
...@@ -18,14 +18,14 @@ pub fn main() !void {...@@ -18,14 +18,14 @@ pub fn main() !void {
1818
19 a = arena.allocator();19 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 {
22 std.debug.print("Expected first argument to be path to zig compiler\n", .{});22 std.debug.print("Expected first argument to be path to zig compiler\n", .{});
23 return error.InvalidArgs;23 return error.InvalidArgs;
24 });24 };
25 const cache_root = try (arg_it.next(a) orelse {25 const cache_root = (try arg_it.next(a)) orelse {
26 std.debug.print("Expected second argument to be cache root directory path\n", .{});26 std.debug.print("Expected second argument to be cache root directory path\n", .{});
27 return error.InvalidArgs;27 return error.InvalidArgs;
28 });28 };
29 const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});29 const zig_exe = try fs.path.resolve(a, &[_][]const u8{zig_exe_rel});
3030
31 const dir_path = try fs.path.join(a, &[_][]const u8{ cache_root, "clitest" });31 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 {...@@ -362,8 +362,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
362 \\ const stdout = io.getStdOut().writer();362 \\ const stdout = io.getStdOut().writer();
363 \\ var index: usize = 0;363 \\ var index: usize = 0;
364 \\ _ = args_it.skip();364 \\ _ = args_it.skip();
365 \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {365 \\ while (try args_it.next(allocator)) |arg| : (index += 1) {
366 \\ const arg = try arg_or_err;
367 \\ try stdout.print("{}: {s}\n", .{index, arg});366 \\ try stdout.print("{}: {s}\n", .{index, arg});
368 \\ }367 \\ }
369 \\}368 \\}
...@@ -401,8 +400,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -401,8 +400,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
401 \\ const stdout = io.getStdOut().writer();400 \\ const stdout = io.getStdOut().writer();
402 \\ var index: usize = 0;401 \\ var index: usize = 0;
403 \\ _ = args_it.skip();402 \\ _ = args_it.skip();
404 \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) {403 \\ while (try args_it.next(allocator)) |arg| : (index += 1) {
405 \\ const arg = try arg_or_err;
406 \\ try stdout.print("{}: {s}\n", .{index, arg});404 \\ try stdout.print("{}: {s}\n", .{index, arg});
407 \\ }405 \\ }
408 \\}406 \\}