This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf8.h: Remove a branch in macro for Unicode surrogates
authorKarl Williamson <khw@cpan.org>
Sat, 7 Nov 2015 17:32:54 +0000 (10:32 -0700)
committerKarl Williamson <khw@cpan.org>
Sun, 6 Dec 2015 05:33:40 +0000 (22:33 -0700)
By masking, this macro can be written so it only has one branch.
Presumably an optimizing compiler would do the same, but not necessarily
so.

utf8.h

diff --git a/utf8.h b/utf8.h
index cc22942..cd7e5bc 100644 (file)
--- a/utf8.h
+++ b/utf8.h
@@ -662,8 +662,11 @@ case any call to string overloading updates the internal UTF-8 encoding flag.
 #define UNICODE_ALLOW_SUPER    0
 #define UNICODE_ALLOW_ANY      0
 
-#define UNICODE_IS_SURROGATE(uv)        ((UV) (uv) >= UNICODE_SURROGATE_FIRST && \
-                                         (UV) (uv) <= UNICODE_SURROGATE_LAST)
+/* This matches the 2048 code points between UNICODE_SURROGATE_FIRST (0xD800) and
+ * UNICODE_SURROGATE_LAST (0xDFFF) */
+#define UNICODE_IS_SURROGATE(uv)        (((UV) (uv) & (~0xFFFF | 0xF800))       \
+                                                                    == 0xD800)
+
 #define UNICODE_IS_REPLACEMENT(uv)     ((UV) (uv) == UNICODE_REPLACEMENT)
 #define UNICODE_IS_BYTE_ORDER_MARK(uv) ((UV) (uv) == UNICODE_BYTE_ORDER_MARK)
 #define UNICODE_IS_NONCHAR(uv)         (((UV) (uv) >= 0xFDD0 && (UV) (uv) <= 0xFDEF) \