| ... | ... | @@ -493,22 +493,22 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 493 | 493 | } |
| 494 | 494 | |
| 495 | 495 | /// Compares two slices and returns the index of the first inequality. |
| 496 | | /// Returns the length of the slices if they are equal. |
| 497 | | pub fn diffIndex(comptime T: type, a: []const T, b: []const T) usize { |
| 496 | /// Returns null if the slices are equal. |
| 497 | pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 498 | 498 | const shortest = math.min(a.len, b.len); |
| 499 | 499 | if (a.ptr == b.ptr) |
| 500 | | return shortest; |
| 500 | return if (a.len == b.len) null else shortest; |
| 501 | 501 | var index: usize = 0; |
| 502 | 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" { |
| 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); |
| 506 | test "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); |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ"); |