This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Subject: handy.h: Add isSPACE_L1 with Unicode semantics
[perl5.git] / handy.h
diff --git a/handy.h b/handy.h
index a1d753d..7bacab3 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -438,38 +438,51 @@ C<strncmp>).
 /*
 
 =head1 Character classes
+The functions in this section operate using the character set of the platform
+Perl is running on, and are unaffected by locale.  For ASCII platforms, they
+will all return false for characters outside the ASCII range.  For EBCDIC
+platforms, they use the code page of the platform.  The code pages that Perl
+knows about all have 8-bit characters, so most of these functions will return
+true for more characters than on ASCII platforms.
 
 =for apidoc Am|bool|isALNUM|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-alphanumeric character (including underscore) or digit.
+Returns a boolean indicating whether the C C<char> is an
+alphanumeric character (including underscore) or digit in the platform's native
+character set.
 
 =for apidoc Am|bool|isALPHA|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-alphabetic character.
+Returns a boolean indicating whether the C C<char> is an
+alphabetic character in the platform's native character set.
 
 =for apidoc Am|bool|isSPACE|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-whitespace.
+Returns a boolean indicating whether the C C<char> is a
+whitespace character in the platform's native character set.
 
 =for apidoc Am|bool|isDIGIT|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-digit.
+Returns a boolean indicating whether the C C<char> is a
+digit in the platform's native character set.
+
+=for apidoc Am|bool|isOCTAL|char ch
+Returns a boolean indicating whether the C C<char> is an
+octal digit, [0-7] in the platform's native character set.
 
 =for apidoc Am|bool|isUPPER|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-uppercase character.
+Returns a boolean indicating whether the C C<char> is an
+uppercase character in the platform's native character set.
 
 =for apidoc Am|bool|isLOWER|char ch
-Returns a boolean indicating whether the C C<char> is a US-ASCII (Basic Latin)
-lowercase character.
+Returns a boolean indicating whether the C C<char> is a
+lowercase character in the platform's native character set.
+
+=head1 Character case changing
 
 =for apidoc Am|char|toUPPER|char ch
-Converts the specified character to uppercase.  Characters outside the
-US-ASCII (Basic Latin) range are viewed as not having any case.
+Converts the specified character to uppercase in the platform's native
+character set, if possible; otherwise returns the input character itself.
 
 =for apidoc Am|char|toLOWER|char ch
-Converts the specified character to lowercase.  Characters outside the
-US-ASCII (Basic Latin) range are viewed as not having any case.
+Converts the specified character to lowercase in the platform's native
+character set, if possible; otherwise returns the input character itself.
 
 =cut
 
@@ -489,9 +502,10 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
  * 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 alway
- * true, when c's storage class is a byte */
+ * true, when c's storage class is a byte.  Use U64TYPE because U64 is known
+ * only in the perl core, and this macro can be called from outside that */
 #ifdef HAS_QUAD
-#  define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || (((U64)(c) & 0xFF) == (U64)(c)))
+#  define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || (((U64TYPE)(c) & 0xFF) == (U64TYPE)(c)))
 #else
 #  define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || (((U32)(c) & 0xFF) == (U32)(c)))
 #endif
@@ -501,6 +515,8 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
 #define isALPHA(c)     (isUPPER(c) || isLOWER(c))
 /* ALPHAU includes Unicode semantics for latin1 characters.  It has an extra
  * >= AA test to speed up ASCII-only tests at the expense of the others */
+/* XXX decide whether to document the ALPHAU, ALNUMU and isSPACE_L1 functions.
+ * Most of these should be implemented as table lookup for speed */
 #define isALPHAU(c)    (isALPHA(c) || (NATIVE_TO_UNI((U8) c) >= 0xAA \
     && ((NATIVE_TO_UNI((U8) c) >= 0xC0 \
            && NATIVE_TO_UNI((U8) c) != 0xD7 && NATIVE_TO_UNI((U8) c) != 0xF7) \
@@ -513,15 +529,18 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
 #define isCHARNAME_CONT(c) (isALNUMU(c) || (c) == ' ' || (c) == '-' || (c) == '(' || (c) == ')' || (c) == ':' || NATIVE_TO_UNI((U8) c) == 0xA0)
 #define isSPACE(c) \
        ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
+#define isSPACE_L1(c) (isSPACE(c) \
+                   || (NATIVE_TO_UNI(c) == 0x85 || NATIVE_TO_UNI(c) == 0xA0))
 #define isPSXSPC(c)    (isSPACE(c) || (c) == '\v')
 #define isBLANK(c)     ((c) == ' ' || (c) == '\t')
 #define isDIGIT(c)     ((c) >= '0' && (c) <= '9')
+#define isOCTAL(c)     ((c) >= '0' && (c) <= '7')
+#define isASCII(c)     (FITS_IN_8_BITS(c) ? NATIVE_TO_UNI((U8) c) <= 127 : 0)
 #ifdef EBCDIC
     /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
 #   define isUPPER(c)  isupper(c)
 #   define isLOWER(c)  islower(c)
 #   define isALNUMC(c) isalnum(c)
-#   define isASCII(c)  isascii(c)
 #   define isCNTRL(c)  iscntrl(c)
 #   define isGRAPH(c)  isgraph(c)
 #   define isPRINT(c)  isprint(c)
@@ -533,7 +552,6 @@ patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
 #   define isUPPER(c)  ((c) >= 'A' && (c) <= 'Z')
 #   define isLOWER(c)  ((c) >= 'a' && (c) <= 'z')
 #   define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
-#   define isASCII(c)  ((U8) (c) <= 127)
 #   define isCNTRL(c)  ((U8) (c) < ' ' || (c) == 127)
 #   define isGRAPH(c)  (isALNUM(c) || isPUNCT(c))
 #   define isPRINT(c)  (((c) >= 32 && (c) < 127))