This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweak the cBOOL() macro to avoid problems with the AIX compiler.
[perl5.git] / handy.h
diff --git a/handy.h b/handy.h
index 2555736..8777644 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -115,9 +115,10 @@ Null SV pointer. (No longer available when C<PERL_CORE> is defined.)
 
 /* a simple (bool) cast may not do the right thing: if bool is defined
  * as char for example, then the cast from int is implementation-defined
+ * (bool)!!(cbool) in a ternary triggers a bug in xlc on AIX
  */
 
-#define cBOOL(cbool) ((bool)!!(cbool))
+#define cBOOL(cbool) ((cbool) ? (bool)1 : (bool)0)
 
 /* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
  * XXX Should really be a Configure probe, with HAS__FUNCTION__
@@ -473,7 +474,7 @@ There are three variants for all the functions in this section.  The base ones
 operate using the character set of the platform Perl is running on.  The ones
 with an C<_A> suffix operate on the ASCII character set, and the ones with an
 C<_L1> suffix operate on the full Latin1 character set.  All are unaffected by
-locale.
+locale and by C<use bytes>.
 
 For ASCII platforms, the base function with no suffix and the one with the
 C<_A> suffix are identical.  The function with the C<_L1> suffix imposes the
@@ -577,13 +578,11 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
  * compiler to optimize it out if possible.  This is because Configure makes
  * sure that the machine has an 8-bit byte, so if c is stored in a byte, the
  * sizeof() guarantees that this evaluates to a constant true at compile time.
- * The use of the mask instead of '< 256' keeps gcc from complaining that it is
- * always true, when c's storage class is a byte. */
-#define FITS_IN_8_BITS(c) ((sizeof(c) == 1)                                    \
-                         || (((WIDEST_UTYPE)(c) & 0xFF) == (WIDEST_UTYPE)(c)))
+ */
+#define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || !(((WIDEST_UTYPE)(c)) & ~0xFF))
 
 #ifdef EBCDIC
-#   define isASCII(c)    (FITS_IN_8_BITS(c) ? NATIVE_TO_UNI((U8) (c)) < 128 : 0)
+#   define isASCII(c)    (FITS_IN_8_BITS(c) && (NATIVE_TO_UNI((U8) (c)) < 128))
 #else
 #   define isASCII(c)    ((WIDEST_UTYPE)(c) < 128)
 #endif
@@ -593,7 +592,9 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
 
 /* ASCII range only */
 #ifdef H_PERL       /* If have access to perl.h, lookup in its table */
-/* Bits for PL_charclass[] */
+/* Bits for PL_charclass[].  These use names used in l1_char_class_tab.h but
+ * their actual definitions are here.  If that has a name not used here, it
+ * won't compile. */
 #  define _CC_ALNUMC_A         (1<<0)
 #  define _CC_ALNUMC_L1        (1<<1)
 #  define _CC_ALPHA_A          (1<<2)
@@ -627,6 +628,10 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
 #  define _CC_NONLATIN1_FOLD   (1<<30)
 /* Unused
  *                             (1<<31)
+ * If more are needed, can give up some of the above.  The first ones to go
+ * would be those that require just two tests to verify, either there are two
+ * code points, like BLANK_A, or occupy a single range like OCTAL_A, DIGIT_A,
+ * UPPER_A, and LOWER_A.
  */
 
 #  ifdef DOINIT
@@ -658,7 +663,7 @@ EXTCONST U32 PL_charclass[];
      * multi-char fold */
 #   define _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(c) ((! cBOOL(FITS_IN_8_BITS(c))) || (PL_charclass[(U8) NATIVE_TO_UNI(c)] & _CC_NONLATIN1_FOLD))
 #else   /* No perl.h. */
-#   define isOCTAL_A(c)  ((c) >= '0' && (c) <= '9')
+#   define isOCTAL_A(c)  ((c) <= '7' && (c) >= '0')
 #   ifdef EBCDIC
 #       define isALNUMC_A(c)   (isASCII(c) && isALNUMC(c))
 #       define isALPHA_A(c)    (isASCII(c) && isALPHA(c))
@@ -679,8 +684,8 @@ EXTCONST U32 PL_charclass[];
 #       define isALNUMC_A(c) (isALPHA_A(c) || isDIGIT_A(c))
 #       define isALPHA_A(c)  (isUPPER_A(c) || isLOWER_A(c))
 #       define isBLANK_A(c)  ((c) == ' ' || (c) == '\t')
-#       define isCNTRL_A(c)  (FITS_IN_8_BITS(c) ? ((U8) (c) < ' ' || (c) == 127) : 0)
-#       define isDIGIT_A(c)  ((c) >= '0' && (c) <= '9')
+#       define isCNTRL_A(c)  (FITS_IN_8_BITS(c) && ((U8) (c) < ' ' || (c) == 127))
+#       define isDIGIT_A(c)  ((c) <= '9' && (c) >= '0')
 #       define isGRAPH_A(c)  (isWORDCHAR_A(c) || isPUNCT_A(c))
 #       define isIDFIRST_A(c) (isALPHA_A(c) || (c) == '_')
 #       define isLOWER_A(c)  ((c) >= 'a' && (c) <= 'z')
@@ -688,9 +693,9 @@ EXTCONST U32 PL_charclass[];
 #       define isPSXSPC_A(c) (isSPACE_A(c) || (c) == '\v')
 #       define isPUNCT_A(c)  (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64)  || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
 #       define isSPACE_A(c)  ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
-#       define isUPPER_A(c) ((c) >= 'A' && (c) <= 'Z')
+#       define isUPPER_A(c)  ((c) <= 'Z' && (c) >= 'A')
 #       define isWORDCHAR_A(c) (isALPHA_A(c) || isDIGIT_A(c) || (c) == '_')
-#       define isXDIGIT_A(c)   (isDIGIT_A(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
+#       define isXDIGIT_A(c)   (isDIGIT_A(c) || ((c) >= 'a' && (c) <= 'f') || ((c) <= 'F' && (c) >= 'A'))
 #   endif
 #endif  /* ASCII range definitions */
 
@@ -869,6 +874,7 @@ EXTCONST U32 PL_charclass[];
 #define generic_uni(macro, function, c) ((c) < 256               \
                                          ? CAT2(macro, _L1)(c)   \
                                          : function(c))
+/* Note that all ignore 'use bytes' */
 
 #define isALNUM_uni(c)         generic_uni(isWORDCHAR, is_uni_alnum, c)
 #define isIDFIRST_uni(c)        generic_uni(isIDFIRST, is_uni_idfirst, c)
@@ -879,7 +885,7 @@ EXTCONST U32 PL_charclass[];
 #define isLOWER_uni(c)         generic_uni(isLOWER, is_uni_lower, c)
 #define isASCII_uni(c)         isASCII(c)
 /* All controls are in Latin1 */
-#define isCNTRL_uni(c)         ((c) < 256 ? isCNTRL_L1(c) : 0)
+#define isCNTRL_uni(c)         ((c) < 256 && isCNTRL_L1(c))
 #define isGRAPH_uni(c)         generic_uni(isGRAPH, is_uni_graph, c)
 #define isPRINT_uni(c)         generic_uni(isPRINT, is_uni_print, c)
 #define isPUNCT_uni(c)         generic_uni(isPUNCT, is_uni_punct, c)
@@ -914,13 +920,15 @@ EXTCONST U32 PL_charclass[];
  * 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(macro, _A)(*(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 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
@@ -934,7 +942,7 @@ EXTCONST U32 PL_charclass[];
                                 : (UTF8_IS_DOWNGRADEABLE_START(*(p)))           \
                                   ? isIDFIRST_L1(TWO_BYTE_UTF8_TO_UNI(*(p),     \
                                                                       *((p)+1)))\
-                                  : _is_utf8__perl_idstart(p))
+                                  : 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)