| ... | ... | @@ -656,7 +656,18 @@ const eqlBytes_allowed = switch (builtin.zig_backend) { |
| 656 | 656 | |
| 657 | 657 | /// Compares two slices and returns whether they are equal. |
| 658 | 658 | pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 659 | | if (@sizeOf(T) == 0) return true; |
| 659 | switch (@typeInfo(T)) { |
| 660 | .Type, .ComptimeInt, .ComptimeFloat => { |
| 661 | if (a.len != b.len) return false; |
| 662 | inline for (a, b) |a_elem, b_elem| { |
| 663 | if (a_elem != b_elem) return false; |
| 664 | } |
| 665 | return true; |
| 666 | }, |
| 667 | .Null, .Undefined => return a.len == b.len, |
| 668 | else => {}, |
| 669 | } |
| 670 | if (@sizeOf(T) == 0) return a.len == b.len; |
| 660 | 671 | if (!@inComptime() and std.meta.hasUniqueRepresentation(T) and eqlBytes_allowed) return eqlBytes(sliceAsBytes(a), sliceAsBytes(b)); |
| 661 | 672 | |
| 662 | 673 | if (a.len != b.len) return false; |
| ... | ... | @@ -3296,6 +3307,26 @@ test eql { |
| 3296 | 3307 | try testing.expect(eql(u8, "abcd", "abcd")); |
| 3297 | 3308 | try testing.expect(!eql(u8, "abcdef", "abZdef")); |
| 3298 | 3309 | try testing.expect(!eql(u8, "abcdefg", "abcdef")); |
| 3310 | |
| 3311 | try testing.expect(eql(type, &.{ bool, f32 }, &.{ bool, f32 })); |
| 3312 | try testing.expect(!eql(type, &.{ bool, f32 }, &.{ f32, bool })); |
| 3313 | try testing.expect(!eql(type, &.{ bool, f32 }, &.{bool})); |
| 3314 | |
| 3315 | try testing.expect(eql(comptime_int, &.{ 1, 2, 3 }, &.{ 1, 2, 3 })); |
| 3316 | try testing.expect(!eql(comptime_int, &.{ 1, 2, 3 }, &.{ 3, 2, 1 })); |
| 3317 | try testing.expect(!eql(comptime_int, &.{1}, &.{ 1, 2 })); |
| 3318 | |
| 3319 | try testing.expect(eql(@TypeOf(undefined), &.{ undefined, undefined }, &.{ undefined, undefined })); |
| 3320 | try testing.expect(!eql(@TypeOf(undefined), &.{undefined}, &.{ undefined, undefined })); |
| 3321 | |
| 3322 | try testing.expect(eql(enum {}, &.{ undefined, undefined }, &.{ undefined, undefined })); |
| 3323 | try testing.expect(!eql(enum {}, &.{undefined}, &.{ undefined, undefined })); |
| 3324 | |
| 3325 | try testing.expect(eql(void, &.{ {}, {} }, &.{ {}, {} })); |
| 3326 | try testing.expect(!eql(void, &.{{}}, &.{ {}, {} })); |
| 3327 | |
| 3328 | try testing.expect(eql(@TypeOf(null), &.{ null, null }, &.{ null, null })); |
| 3329 | try testing.expect(!eql(@TypeOf(null), &.{null}, &.{ null, null })); |
| 3299 | 3330 | } |
| 3300 | 3331 | |
| 3301 | 3332 | fn moreReadIntTests() !void { |