Function indexOfDiff [src]
Compares two slices and returns the index of the first inequality.
Returns null if the slices are equal.
Prototype
pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize
Parameters
T: type
a: []const T
b: []const T
Example
test indexOfDiff {
try testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
try testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
try testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
try testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);
try testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);
}
Source
pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
const shortest = @min(a.len, b.len);
if (a.ptr == b.ptr)
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 if (a.len == b.len) null else shortest;
}