| author | |
| committer | |
| log | 2d06e731ec4feee03a2a9bc79eb44b15e2db2488 |
| tree | 94dc10d53c7ebd95d2013a409063ec21e4aa9ec6 |
| parent | daff072af2afe794d516b1406073ada877e4b1ab |
| signature | Commit is signed but in an unrecognized format. |
2 files changed, 11 insertions(+), 13 deletions(-)
lib/std/mem.zig+10-10| ... | @@ -493,22 +493,22 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { | ... | @@ -493,22 +493,22 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 493 | } | 493 | } |
| 494 | 494 | ||
| 495 | /// Compares two slices and returns the index of the first inequality. | 495 | /// Compares two slices and returns the index of the first inequality. |
| 496 | /// Returns the length of the slices if they are equal. | 496 | /// Returns null if the slices are equal. |
| 497 | pub fn diffIndex(comptime T: type, a: []const T, b: []const T) usize { | 497 | pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 498 | const shortest = math.min(a.len, b.len); | 498 | const shortest = math.min(a.len, b.len); |
| 499 | if (a.ptr == b.ptr) | 499 | if (a.ptr == b.ptr) |
| 500 | return shortest; | 500 | return if (a.len == b.len) null else shortest; |
| 501 | var index: usize = 0; | 501 | var index: usize = 0; |
| 502 | while (index < shortest) : (index += 1) if (a[index] != b[index]) return index; | 502 | while (index < shortest) : (index += 1) if (a[index] != b[index]) return index; |
| 503 | return shortest; | 503 | return if (a.len == b.len) null else shortest; |
| 504 | } | 504 | } |
| 505 | 505 | ||
| 506 | test "diffIndex" { | 506 | test "indexOfDiff" { |
| 507 | testing.expectEqual(diffIndex(u8, "one", "one"), 3); | 507 | testing.expectEqual(indexOfDiff(u8, "one", "one"), null); |
| 508 | testing.expectEqual(diffIndex(u8, "one two", "one"), 3); | 508 | testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3); |
| 509 | testing.expectEqual(diffIndex(u8, "one", "one two"), 3); | 509 | testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3); |
| 510 | testing.expectEqual(diffIndex(u8, "one twx", "one two"), 6); | 510 | testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6); |
| 511 | testing.expectEqual(diffIndex(u8, "xne", "one"), 0); | 511 | testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0); |
| 512 | } | 512 | } |
| 513 | 513 | ||
| 514 | pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ"); | 514 | pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ"); |
lib/std/zig/parser_test.zig+1-3| ... | @@ -2997,15 +2997,13 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { | ... | @@ -2997,15 +2997,13 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { |
| 2997 | var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize)); | 2997 | var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize)); |
| 2998 | var anything_changed: bool = undefined; | 2998 | var anything_changed: bool = undefined; |
| 2999 | const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed); | 2999 | const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed); |
| 3000 | if (!mem.eql(u8, result_source, expected_source)) { | 3000 | if (mem.indexOfDiff(u8, result_source, expected_source)) |diff_index| { |
| 3001 | warn("\n====== expected this output: =========\n", .{}); | 3001 | warn("\n====== expected this output: =========\n", .{}); |
| 3002 | printWithVisibleNewlines(expected_source); | 3002 | printWithVisibleNewlines(expected_source); |
| 3003 | warn("\n======== instead found this: =========\n", .{}); | 3003 | warn("\n======== instead found this: =========\n", .{}); |
| 3004 | printWithVisibleNewlines(result_source); | 3004 | printWithVisibleNewlines(result_source); |
| 3005 | warn("\n======================================\n", .{}); | 3005 | warn("\n======================================\n", .{}); |
| 3006 | 3006 | ||
| 3007 | const diff_index = mem.diffIndex(u8, result_source, expected_source); | ||
| 3008 | |||
| 3009 | var diff_line_number: usize = 1; | 3007 | var diff_line_number: usize = 1; |
| 3010 | for (expected_source[0..diff_index]) |value| { | 3008 | for (expected_source[0..diff_index]) |value| { |
| 3011 | if (value == '\n') diff_line_number += 1; | 3009 | if (value == '\n') diff_line_number += 1; |