-#define isALNUM_utf8(p) is_utf8_alnum(p)
-/* The ID_Start of Unicode was originally quite limiting: it assumed an
- * L-class character (meaning that you could not have, say, a CJK charac-
- * ter). So, instead, perl has for a long time allowed ID_Continue but
- * not digits.
- * We still preserve that for backward compatibility. But we also make sure
- * that it is alphanumeric, so S_scan_word in toke.c will not hang. See
- * http://rt.perl.org/rt3/Ticket/Display.html?id=74022
- * for more detail than you ever wanted to know about. */
-#define isIDFIRST_utf8(p) \
- (is_utf8_idcont(p) && !is_utf8_digit(p) && is_utf8_alnum(p))
-#define isALPHA_utf8(p) is_utf8_alpha(p)
-#define isSPACE_utf8(p) is_utf8_space(p)
-#define isDIGIT_utf8(p) is_utf8_digit(p)
-#define isUPPER_utf8(p) is_utf8_upper(p)
-#define isLOWER_utf8(p) is_utf8_lower(p)
-#define isASCII_utf8(p) is_utf8_ascii(p)
-#define isCNTRL_utf8(p) is_utf8_cntrl(p)
-#define isGRAPH_utf8(p) is_utf8_graph(p)
-#define isPRINT_utf8(p) is_utf8_print(p)
-#define isPUNCT_utf8(p) is_utf8_punct(p)
-#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
+/* For use in the macros just below. If the input is ASCII, use the ASCII (_A)
+ * version of the macro; if the input is in the upper Latin1 range, use the
+ * Latin1 (_L1) version of the macro, after converting from utf8; otherwise use
+ * the function. This relies on the fact that ASCII characters have the same
+ * representation whether utf8 or not */
+#define generic_utf8(macro, function, p) (isASCII(*(p)) \
+ ? CAT2(CAT2(macro,_),A)(*(p)) \
+ : (UTF8_IS_DOWNGRADEABLE_START(*(p))) \
+ ? CAT2(macro, _L1) \
+ (TWO_BYTE_UTF8_TO_UNI(*(p), \
+ *((p)+1))) \
+ : function(p))
+
+/* Note that all assume that the utf8 has been validated, and ignore 'use
+ * bytes' */
+
+#define isALNUM_utf8(p) generic_utf8(isWORDCHAR, is_utf8_alnum, p)
+/* To prevent S_scan_word in toke.c from hanging, we have to make sure that
+ * IDFIRST is an alnum. See
+ * http://rt.perl.org/rt3/Ticket/Display.html?id=74022 for more detail than you
+ * ever wanted to know about. XXX It is unclear if this should extend to
+ * isIDFIRST_uni() which it hasn't so far. (In the ASCII range, there isn't a
+ * difference.) This used to be not the XID version, but we decided to go with
+ * the more modern Unicode definition */
+#define isIDFIRST_utf8(p) (isASCII(*(p)) \
+ ? isIDFIRST_A(*(p)) \
+ : (UTF8_IS_DOWNGRADEABLE_START(*(p))) \
+ ? isIDFIRST_L1(TWO_BYTE_UTF8_TO_UNI(*(p), \
+ *((p)+1)))\
+ : Perl__is_utf8__perl_idstart(aTHX_ p))
+#define isIDCONT_utf8(p) generic_utf8(isWORDCHAR, is_utf8_xidcont, p)
+#define isALPHA_utf8(p) generic_utf8(isALPHA, is_utf8_alpha, p)
+#define isSPACE_utf8(p) generic_utf8(isSPACE, is_utf8_space, p)
+#define isDIGIT_utf8(p) generic_utf8(isDIGIT, is_utf8_digit, p)
+#define isUPPER_utf8(p) generic_utf8(isUPPER, is_utf8_upper, p)
+#define isLOWER_utf8(p) generic_utf8(isLOWER, is_utf8_lower, p)
+/* Because ASCII is invariant under utf8, the non-utf8 macro works */
+#define isASCII_utf8(p) isASCII(p)
+#define isCNTRL_utf8(p) generic_utf8(isCNTRL, is_utf8_cntrl, p)
+#define isGRAPH_utf8(p) generic_utf8(isGRAPH, is_utf8_graph, p)
+#define isPRINT_utf8(p) generic_utf8(isPRINT, is_utf8_print, p)
+#define isPUNCT_utf8(p) generic_utf8(isPUNCT, is_utf8_punct, p)
+#define isXDIGIT_utf8(p) generic_utf8(isXDIGIT, is_utf8_xdigit, p)