authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-29 14:09:12+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-30 10:33:50+03:00
log2d06e731ec4feee03a2a9bc79eb44b15e2db2488
tree94dc10d53c7ebd95d2013a409063ec21e4aa9ec6
parentdaff072af2afe794d516b1406073ada877e4b1ab
signature Commit is signed but in an unrecognized format.

rename diffIndex to indexOfDiff


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 {
493493}
494494
495495/// Compares two slices and returns the index of the first inequality.
496/// Returns the length of the slices if they are equal.
497pub fn diffIndex(comptime T: type, a: []const T, b: []const T) usize {
496/// Returns null if the slices are equal.
497pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
498498 const shortest = math.min(a.len, b.len);
499499 if (a.ptr == b.ptr)
500 return shortest;
500 return if (a.len == b.len) null else shortest;
501501 var index: usize = 0;
502502 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;
504504}
505505
506test "diffIndex" {
507 testing.expectEqual(diffIndex(u8, "one", "one"), 3);
508 testing.expectEqual(diffIndex(u8, "one two", "one"), 3);
509 testing.expectEqual(diffIndex(u8, "one", "one two"), 3);
510 testing.expectEqual(diffIndex(u8, "one twx", "one two"), 6);
511 testing.expectEqual(diffIndex(u8, "xne", "one"), 0);
506test "indexOfDiff" {
507 testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
508 testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
509 testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
510 testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);
511 testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);
512512}
513513
514514pub 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 {
29972997 var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
29982998 var anything_changed: bool = undefined;
29992999 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| {
30013001 warn("\n====== expected this output: =========\n", .{});
30023002 printWithVisibleNewlines(expected_source);
30033003 warn("\n======== instead found this: =========\n", .{});
30043004 printWithVisibleNewlines(result_source);
30053005 warn("\n======================================\n", .{});
30063006
3007 const diff_index = mem.diffIndex(u8, result_source, expected_source);
3008
30093007 var diff_line_number: usize = 1;
30103008 for (expected_source[0..diff_index]) |value| {
30113009 if (value == '\n') diff_line_number += 1;