Commit 2d06e731ec
Changed files (2)
lib
std
lib/std/zig/parser_test.zig
@@ -2997,15 +2997,13 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
var anything_changed: bool = undefined;
const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed);
- if (!mem.eql(u8, result_source, expected_source)) {
+ if (mem.indexOfDiff(u8, result_source, expected_source)) |diff_index| {
warn("\n====== expected this output: =========\n", .{});
printWithVisibleNewlines(expected_source);
warn("\n======== instead found this: =========\n", .{});
printWithVisibleNewlines(result_source);
warn("\n======================================\n", .{});
- const diff_index = mem.diffIndex(u8, result_source, expected_source);
-
var diff_line_number: usize = 1;
for (expected_source[0..diff_index]) |value| {
if (value == '\n') diff_line_number += 1;
lib/std/mem.zig
@@ -493,22 +493,22 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
}
/// Compares two slices and returns the index of the first inequality.
-/// Returns the length of the slices if they are equal.
-pub fn diffIndex(comptime T: type, a: []const T, b: []const T) usize {
+/// Returns null if the slices are equal.
+pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
const shortest = math.min(a.len, b.len);
if (a.ptr == b.ptr)
- return shortest;
+ return if (a.len == b.len) null else shortest;
var index: usize = 0;
while (index < shortest) : (index += 1) if (a[index] != b[index]) return index;
- return shortest;
+ return if (a.len == b.len) null else shortest;
}
-test "diffIndex" {
- testing.expectEqual(diffIndex(u8, "one", "one"), 3);
- testing.expectEqual(diffIndex(u8, "one two", "one"), 3);
- testing.expectEqual(diffIndex(u8, "one", "one two"), 3);
- testing.expectEqual(diffIndex(u8, "one twx", "one two"), 6);
- testing.expectEqual(diffIndex(u8, "xne", "one"), 0);
+test "indexOfDiff" {
+ testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
+ testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
+ testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
+ testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);
+ testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);
}
pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ");