| ... | @@ -756,7 +756,8 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { | ... | @@ -756,7 +756,8 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 756 | } | 756 | } |
| 757 | | 757 | |
| 758 | if (a.len != b.len) return false; | 758 | if (a.len != b.len) return false; |
| 759 | if (a.len == 0 or a.ptr == b.ptr) return true; | 759 | if (a.len == 0) return true; |
| | 760 | if (@typeInfo(T) != .float and a.ptr == b.ptr) return true; |
| 760 | | 761 | |
| 761 | for (a, b) |a_elem, b_elem| { | 762 | for (a, b) |a_elem, b_elem| { |
| 762 | if (a_elem != b_elem) return false; | 763 | if (a_elem != b_elem) return false; |
| ... | @@ -781,6 +782,9 @@ test eql { | ... | @@ -781,6 +782,9 @@ test eql { |
| 781 | | 782 | |
| 782 | try testing.expect(eql(void, &.{ {}, {} }, &.{ {}, {} })); | 783 | try testing.expect(eql(void, &.{ {}, {} }, &.{ {}, {} })); |
| 783 | try testing.expect(!eql(void, &.{{}}, &.{ {}, {} })); | 784 | try testing.expect(!eql(void, &.{{}}, &.{ {}, {} })); |
| | 785 | |
| | 786 | const x: [3]f64 = .{ 42.0, math.nan(f64), 3.1415 }; |
| | 787 | try testing.expect(!eql(f64, &x, &x)); |
| 784 | } | 788 | } |
| 785 | | 789 | |
| 786 | /// std.mem.eql heavily optimized for slices of bytes. | 790 | /// std.mem.eql heavily optimized for slices of bytes. |
| ... | @@ -850,20 +854,25 @@ pub const indexOfDiff = findDiff; | ... | @@ -850,20 +854,25 @@ pub const indexOfDiff = findDiff; |
| 850 | /// Compares two slices and returns the index of the first inequality. | 854 | /// Compares two slices and returns the index of the first inequality. |
| 851 | /// Returns null if the slices are equal. | 855 | /// Returns null if the slices are equal. |
| 852 | pub fn findDiff(comptime T: type, a: []const T, b: []const T) ?usize { | 856 | pub fn findDiff(comptime T: type, a: []const T, b: []const T) ?usize { |
| 853 | const shortest = @min(a.len, b.len); | 857 | const shorter = @min(a.len, b.len); |
| 854 | if (a.ptr == b.ptr) | 858 | if (@typeInfo(T) != .float and a.ptr == b.ptr) { |
| 855 | return if (a.len == b.len) null else shortest; | 859 | return if (a.len == b.len) null else shorter; |
| 856 | var index: usize = 0; | 860 | } |
| 857 | while (index < shortest) : (index += 1) if (a[index] != b[index]) return index; | 861 | for (a[0..shorter], b[0..shorter], 0..) |a_elem, b_elem, i| { |
| 858 | return if (a.len == b.len) null else shortest; | 862 | if (a_elem != b_elem) return i; |
| | 863 | } |
| | 864 | return if (a.len == b.len) null else shorter; |
| 859 | } | 865 | } |
| 860 | | 866 | |
| 861 | test findDiff { | 867 | test findDiff { |
| 862 | try testing.expectEqual(findDiff(u8, "one", "one"), null); | 868 | try testing.expectEqual(null, findDiff(u8, "one", "one")); |
| 863 | try testing.expectEqual(findDiff(u8, "one two", "one"), 3); | 869 | try testing.expectEqual(3, findDiff(u8, "one two", "one")); |
| 864 | try testing.expectEqual(findDiff(u8, "one", "one two"), 3); | 870 | try testing.expectEqual(3, findDiff(u8, "one", "one two")); |
| 865 | try testing.expectEqual(findDiff(u8, "one twx", "one two"), 6); | 871 | try testing.expectEqual(6, findDiff(u8, "one twx", "one two")); |
| 866 | try testing.expectEqual(findDiff(u8, "xne", "one"), 0); | 872 | try testing.expectEqual(0, findDiff(u8, "xne", "one")); |
| | 873 | |
| | 874 | const x: [3]f64 = .{ 42.0, math.nan(f64), 3.1415 }; |
| | 875 | try testing.expectEqual(1, findDiff(f64, &x, &x)); |
| 867 | } | 876 | } |
| 868 | | 877 | |
| 869 | /// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes. | 878 | /// Takes a sentinel-terminated pointer and returns a slice preserving pointer attributes. |