From 3a6e6bd538858d2aa271b8975a6e1f742fec144f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 5 Sep 2020 15:20:48 +0200 Subject: [PATCH] Check if the type has unique bit representation to see if it can be compared byte-wise --- lib/std/mem.zig | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index cd1000d5e3..0939bebe98 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -897,9 +897,7 @@ fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void { pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { if (needle.len > haystack.len or needle.len == 0) return null; - // byte comparison with float doesn't work because +0.0 and -0.0 and considered - // equal but their byte representations are not equal. - if (@typeInfo(T) == .Float or haystack.len < 32 or needle.len <= 2) + if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2) return lastIndexOfLinear(T, haystack, needle); const haystack_bytes = sliceAsBytes(haystack); @@ -923,9 +921,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { if (needle.len > haystack.len or needle.len == 0) return null; - // byte comparison with float doesn't work because +0.0 and -0.0 and considered - // equal but their byte representations are not equal. - if (@typeInfo(T) == .Float or haystack.len < 32 or needle.len <= 2) + if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2) return indexOfPosLinear(T, haystack, start_index, needle); const haystack_bytes = sliceAsBytes(haystack);