xref: /openbsd-src/gnu/usr.bin/perl/handy.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*    handy.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4  *    2001, 2002, 2004, 2005, 2006, 2007, 2008, 2012 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10 
11 /* IMPORTANT NOTE: Everything whose name begins with an underscore is for
12  * internal core Perl use only. */
13 
14 #ifndef HANDY_H /* Guard against nested #inclusion */
15 #define HANDY_H
16 
17 #if !defined(__STDC__)
18 #ifdef NULL
19 #undef NULL
20 #endif
21 #  define NULL 0
22 #endif
23 
24 #ifndef PERL_CORE
25 #  define Null(type) ((type)NULL)
26 
27 /*
28 =head1 Handy Values
29 
30 =for apidoc AmU||Nullch
31 Null character pointer.  (No longer available when C<PERL_CORE> is
32 defined.)
33 
34 =for apidoc AmU||Nullsv
35 Null SV pointer.  (No longer available when C<PERL_CORE> is defined.)
36 
37 =cut
38 */
39 
40 #  define Nullch Null(char*)
41 #  define Nullfp Null(PerlIO*)
42 #  define Nullsv Null(SV*)
43 #endif
44 
45 #ifdef TRUE
46 #undef TRUE
47 #endif
48 #ifdef FALSE
49 #undef FALSE
50 #endif
51 #define TRUE (1)
52 #define FALSE (0)
53 
54 /* The MUTABLE_*() macros cast pointers to the types shown, in such a way
55  * (compiler permitting) that casting away const-ness will give a warning;
56  * e.g.:
57  *
58  * const SV *sv = ...;
59  * AV *av1 = (AV*)sv;        <== BAD:  the const has been silently cast away
60  * AV *av2 = MUTABLE_AV(sv); <== GOOD: it may warn
61  */
62 
63 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
64 #  define MUTABLE_PTR(p) ({ void *_p = (p); _p; })
65 #else
66 #  define MUTABLE_PTR(p) ((void *) (p))
67 #endif
68 
69 #define MUTABLE_AV(p)	((AV *)MUTABLE_PTR(p))
70 #define MUTABLE_CV(p)	((CV *)MUTABLE_PTR(p))
71 #define MUTABLE_GV(p)	((GV *)MUTABLE_PTR(p))
72 #define MUTABLE_HV(p)	((HV *)MUTABLE_PTR(p))
73 #define MUTABLE_IO(p)	((IO *)MUTABLE_PTR(p))
74 #define MUTABLE_SV(p)	((SV *)MUTABLE_PTR(p))
75 
76 #if defined(I_STDBOOL) && !defined(PERL_BOOL_AS_CHAR)
77 #  include <stdbool.h>
78 #  ifndef HAS_BOOL
79 #    define HAS_BOOL 1
80 #  endif
81 #endif
82 
83 /* bool is built-in for g++-2.6.3 and later, which might be used
84    for extensions.  <_G_config.h> defines _G_HAVE_BOOL, but we can't
85    be sure _G_config.h will be included before this file.  _G_config.h
86    also defines _G_HAVE_BOOL for both gcc and g++, but only g++
87    actually has bool.  Hence, _G_HAVE_BOOL is pretty useless for us.
88    g++ can be identified by __GNUG__.
89    Andy Dougherty	February 2000
90 */
91 #ifdef __GNUG__		/* GNU g++ has bool built-in */
92 # ifndef PERL_BOOL_AS_CHAR
93 #  ifndef HAS_BOOL
94 #    define HAS_BOOL 1
95 #  endif
96 # endif
97 #endif
98 
99 /* The NeXT dynamic loader headers will not build with the bool macro
100    So declare them now to clear confusion.
101 */
102 #if defined(NeXT) || defined(__NeXT__)
103 # undef FALSE
104 # undef TRUE
105   typedef enum bool { FALSE = 0, TRUE = 1 } bool;
106 # define ENUM_BOOL 1
107 # ifndef HAS_BOOL
108 #  define HAS_BOOL 1
109 # endif /* !HAS_BOOL */
110 #endif /* NeXT || __NeXT__ */
111 
112 #ifndef HAS_BOOL
113 # ifdef bool
114 #  undef bool
115 # endif
116 # define bool char
117 # define HAS_BOOL 1
118 #endif
119 
120 /* cast-to-bool.  A simple (bool) cast may not do the right thing: if bool is
121  * defined as char for example, then the cast from int is
122  * implementation-defined (bool)!!(cbool) in a ternary triggers a bug in xlc on
123  * AIX */
124 #define cBOOL(cbool) ((cbool) ? (bool)1 : (bool)0)
125 
126 /* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
127  * XXX Should really be a Configure probe, with HAS__FUNCTION__
128  *     and FUNCTION__ as results.
129  * XXX Similarly, a Configure probe for __FILE__ and __LINE__ is needed. */
130 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
131 #  define FUNCTION__ __func__
132 #else
133 #  if (defined(USING_MSVC6)) || /* MSVC6 has neither __func__ nor __FUNCTION and no good workarounds, either. */ \
134       (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used, but not modern enough cc (in Tur64, -c99 not known, only -std1). */
135 #    define FUNCTION__ ""
136 #  else
137 #    define FUNCTION__ __FUNCTION__ /* Common extension. */
138 #  endif
139 #endif
140 
141 /* XXX A note on the perl source internal type system.  The
142    original intent was that I32 be *exactly* 32 bits.
143 
144    Currently, we only guarantee that I32 is *at least* 32 bits.
145    Specifically, if int is 64 bits, then so is I32.  (This is the case
146    for the Cray.)  This has the advantage of meshing nicely with
147    standard library calls (where we pass an I32 and the library is
148    expecting an int), but the disadvantage that an I32 is not 32 bits.
149    Andy Dougherty	August 1996
150 
151    There is no guarantee that there is *any* integral type with
152    exactly 32 bits.  It is perfectly legal for a system to have
153    sizeof(short) == sizeof(int) == sizeof(long) == 8.
154 
155    Similarly, there is no guarantee that I16 and U16 have exactly 16
156    bits.
157 
158    For dealing with issues that may arise from various 32/64-bit
159    systems, we will ask Configure to check out
160 
161 	SHORTSIZE == sizeof(short)
162 	INTSIZE == sizeof(int)
163 	LONGSIZE == sizeof(long)
164 	LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
165 	PTRSIZE == sizeof(void *)
166 	DOUBLESIZE == sizeof(double)
167 	LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
168 
169 */
170 
171 #ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
172 #   include <inttypes.h>
173 #   ifdef INT32_MIN_BROKEN
174 #       undef  INT32_MIN
175 #       define INT32_MIN (-2147483647-1)
176 #   endif
177 #   ifdef INT64_MIN_BROKEN
178 #       undef  INT64_MIN
179 #       define INT64_MIN (-9223372036854775807LL-1)
180 #   endif
181 #endif
182 
183 typedef I8TYPE I8;
184 typedef U8TYPE U8;
185 typedef I16TYPE I16;
186 typedef U16TYPE U16;
187 typedef I32TYPE I32;
188 typedef U32TYPE U32;
189 #ifdef PERL_CORE
190 #   ifdef HAS_QUAD
191 typedef I64TYPE I64;
192 typedef U64TYPE U64;
193 #   endif
194 #endif /* PERL_CORE */
195 
196 #if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
197 #   if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
198 #       define PeRl_INT64_C(c)	CAT2(c,LL)
199 #       define PeRl_UINT64_C(c)	CAT2(c,ULL)
200 #   else
201 #       if QUADKIND == QUAD_IS___INT64
202 #           define PeRl_INT64_C(c)	CAT2(c,I64)
203 #           define PeRl_UINT64_C(c)	CAT2(c,UI64)
204 #       else
205 #           if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
206 #               define PeRl_INT64_C(c)	CAT2(c,L)
207 #               define PeRl_UINT64_C(c)	CAT2(c,UL)
208 #           else
209 #               define PeRl_INT64_C(c)	((I64TYPE)(c))
210 #               define PeRl_UINT64_C(c)	((U64TYPE)(c))
211 #           endif
212 #       endif
213 #   endif
214 #   ifndef UINT64_C
215 #   define UINT64_C(c) PeRl_UINT64_C(c)
216 #   endif
217 #   ifndef INT64_C
218 #   define INT64_C(c) PeRl_INT64_C(c)
219 #   endif
220 #endif
221 
222 #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
223 
224 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
225    Please search CHAR_MAX in perl.h for further details. */
226 #define U8_MAX UINT8_MAX
227 #define U8_MIN UINT8_MIN
228 
229 #define I16_MAX INT16_MAX
230 #define I16_MIN INT16_MIN
231 #define U16_MAX UINT16_MAX
232 #define U16_MIN UINT16_MIN
233 
234 #define I32_MAX INT32_MAX
235 #define I32_MIN INT32_MIN
236 #ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
237 #  define U32_MAX UINT32_MAX
238 #else
239 #  define U32_MAX 4294967295U
240 #endif
241 #define U32_MIN UINT32_MIN
242 
243 #else
244 
245 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
246    Please search CHAR_MAX in perl.h for further details. */
247 #define U8_MAX PERL_UCHAR_MAX
248 #define U8_MIN PERL_UCHAR_MIN
249 
250 #define I16_MAX PERL_SHORT_MAX
251 #define I16_MIN PERL_SHORT_MIN
252 #define U16_MAX PERL_USHORT_MAX
253 #define U16_MIN PERL_USHORT_MIN
254 
255 #if LONGSIZE > 4
256 # define I32_MAX PERL_INT_MAX
257 # define I32_MIN PERL_INT_MIN
258 # define U32_MAX PERL_UINT_MAX
259 # define U32_MIN PERL_UINT_MIN
260 #else
261 # define I32_MAX PERL_LONG_MAX
262 # define I32_MIN PERL_LONG_MIN
263 # define U32_MAX PERL_ULONG_MAX
264 # define U32_MIN PERL_ULONG_MIN
265 #endif
266 
267 #endif
268 
269 /* log(2) is pretty close to  0.30103, just in case anyone is grepping for it */
270 #define BIT_DIGITS(N)   (((N)*146)/485 + 1)  /* log2(10) =~ 146/485 */
271 #define TYPE_DIGITS(T)  BIT_DIGITS(sizeof(T) * 8)
272 #define TYPE_CHARS(T)   (TYPE_DIGITS(T) + 2) /* sign, NUL */
273 
274 #define Ctl(ch) ((ch) & 037)
275 
276 /* This is a helper macro to avoid preprocessor issues, replaced by nothing
277  * unless under DEBUGGING, where it expands to an assert of its argument,
278  * followed by a comma (hence the comma operator).  If we just used a straight
279  * assert(), we would get a comma with nothing before it when not DEBUGGING */
280 #ifdef DEBUGGING
281 #   define __ASSERT_(statement)  assert(statement),
282 #else
283 #   define __ASSERT_(statement)
284 #endif
285 
286 /*
287 =head1 SV-Body Allocation
288 
289 =for apidoc Ama|SV*|newSVpvs|const char* s
290 Like C<newSVpvn>, but takes a literal C<NUL>-terminated string instead of a
291 string/length pair.
292 
293 =for apidoc Ama|SV*|newSVpvs_flags|const char* s|U32 flags
294 Like C<newSVpvn_flags>, but takes a literal C<NUL>-terminated string instead of
295 a string/length pair.
296 
297 =for apidoc Ama|SV*|newSVpvs_share|const char* s
298 Like C<newSVpvn_share>, but takes a literal C<NUL>-terminated string instead of
299 a string/length pair and omits the hash parameter.
300 
301 =for apidoc Am|void|sv_catpvs_flags|SV* sv|const char* s|I32 flags
302 Like C<sv_catpvn_flags>, but takes a literal C<NUL>-terminated string instead
303 of a string/length pair.
304 
305 =for apidoc Am|void|sv_catpvs_nomg|SV* sv|const char* s
306 Like C<sv_catpvn_nomg>, but takes a literal string instead of a
307 string/length pair.
308 
309 =for apidoc Am|void|sv_catpvs|SV* sv|const char* s
310 Like C<sv_catpvn>, but takes a literal string instead of a string/length pair.
311 
312 =for apidoc Am|void|sv_catpvs_mg|SV* sv|const char* s
313 Like C<sv_catpvn_mg>, but takes a literal string instead of a
314 string/length pair.
315 
316 =for apidoc Am|void|sv_setpvs|SV* sv|const char* s
317 Like C<sv_setpvn>, but takes a literal string instead of a string/length pair.
318 
319 =for apidoc Am|void|sv_setpvs_mg|SV* sv|const char* s
320 Like C<sv_setpvn_mg>, but takes a literal string instead of a
321 string/length pair.
322 
323 =for apidoc Am|SV *|sv_setref_pvs|const char* s
324 Like C<sv_setref_pvn>, but takes a literal string instead of a
325 string/length pair.
326 
327 =head1 Memory Management
328 
329 =for apidoc Ama|char*|savepvs|const char* s
330 Like C<savepvn>, but takes a literal C<NUL>-terminated string instead of a
331 string/length pair.
332 
333 =for apidoc Ama|char*|savesharedpvs|const char* s
334 A version of C<savepvs()> which allocates the duplicate string in memory
335 which is shared between threads.
336 
337 =head1 GV Functions
338 
339 =for apidoc Am|HV*|gv_stashpvs|const char* name|I32 create
340 Like C<gv_stashpvn>, but takes a literal string instead of a string/length pair.
341 
342 =head1 Hash Manipulation Functions
343 
344 =for apidoc Am|SV**|hv_fetchs|HV* tb|const char* key|I32 lval
345 Like C<hv_fetch>, but takes a literal string instead of a string/length pair.
346 
347 =for apidoc Am|SV**|hv_stores|HV* tb|const char* key|NULLOK SV* val
348 Like C<hv_store>, but takes a literal string instead of a string/length pair
349 and omits the hash parameter.
350 
351 =head1 Lexer interface
352 
353 =for apidoc Amx|void|lex_stuff_pvs|const char *pv|U32 flags
354 
355 Like L</lex_stuff_pvn>, but takes a literal string instead of a
356 string/length pair.
357 
358 =cut
359 */
360 
361 /* concatenating with "" ensures that only literal strings are accepted as
362  * argument */
363 #define STR_WITH_LEN(s)  ("" s ""), (sizeof(s)-1)
364 
365 /* note that STR_WITH_LEN() can't be used as argument to macros or functions
366  * that under some configurations might be macros, which means that it requires
367  * the full Perl_xxx(aTHX_ ...) form for any API calls where it's used.
368  */
369 
370 /* STR_WITH_LEN() shortcuts */
371 #define newSVpvs(str) Perl_newSVpvn(aTHX_ STR_WITH_LEN(str))
372 #define newSVpvs_flags(str,flags)	\
373     Perl_newSVpvn_flags(aTHX_ STR_WITH_LEN(str), flags)
374 #define newSVpvs_share(str) Perl_newSVpvn_share(aTHX_ STR_WITH_LEN(str), 0)
375 #define sv_catpvs_flags(sv, str, flags) \
376     Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), flags)
377 #define sv_catpvs_nomg(sv, str) \
378     Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), 0)
379 #define sv_catpvs(sv, str) \
380     Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC)
381 #define sv_catpvs_mg(sv, str) \
382     Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC|SV_SMAGIC)
383 #define sv_setpvs(sv, str) Perl_sv_setpvn(aTHX_ sv, STR_WITH_LEN(str))
384 #define sv_setpvs_mg(sv, str) Perl_sv_setpvn_mg(aTHX_ sv, STR_WITH_LEN(str))
385 #define sv_setref_pvs(rv, classname, str) \
386     Perl_sv_setref_pvn(aTHX_ rv, classname, STR_WITH_LEN(str))
387 #define savepvs(str) Perl_savepvn(aTHX_ STR_WITH_LEN(str))
388 #define savesharedpvs(str) Perl_savesharedpvn(aTHX_ STR_WITH_LEN(str))
389 #define gv_stashpvs(str, create) \
390     Perl_gv_stashpvn(aTHX_ STR_WITH_LEN(str), create)
391 #define gv_fetchpvs(namebeg, add, sv_type) \
392     Perl_gv_fetchpvn_flags(aTHX_ STR_WITH_LEN(namebeg), add, sv_type)
393 #define gv_fetchpvn(namebeg, len, add, sv_type) \
394     Perl_gv_fetchpvn_flags(aTHX_ namebeg, len, add, sv_type)
395 #define sv_catxmlpvs(dsv, str, utf8) \
396     Perl_sv_catxmlpvn(aTHX_ dsv, STR_WITH_LEN(str), utf8)
397 #define hv_fetchs(hv,key,lval)						\
398   ((SV **)Perl_hv_common(aTHX_ (hv), NULL, STR_WITH_LEN(key), 0,	\
399 			 (lval) ? (HV_FETCH_JUST_SV | HV_FETCH_LVALUE)	\
400 			 : HV_FETCH_JUST_SV, NULL, 0))
401 
402 #define hv_stores(hv,key,val)						\
403   ((SV **)Perl_hv_common(aTHX_ (hv), NULL, STR_WITH_LEN(key), 0,	\
404 			 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), (val), 0))
405 
406 #define lex_stuff_pvs(pv,flags) Perl_lex_stuff_pvn(aTHX_ STR_WITH_LEN(pv), flags)
407 
408 #define get_cvs(str, flags)					\
409 	Perl_get_cvn_flags(aTHX_ STR_WITH_LEN(str), (flags))
410 
411 /*
412 =head1 Miscellaneous Functions
413 
414 =for apidoc Am|bool|strNE|char* s1|char* s2
415 Test two strings to see if they are different.  Returns true or
416 false.
417 
418 =for apidoc Am|bool|strEQ|char* s1|char* s2
419 Test two strings to see if they are equal.  Returns true or false.
420 
421 =for apidoc Am|bool|strLT|char* s1|char* s2
422 Test two strings to see if the first, C<s1>, is less than the second,
423 C<s2>.  Returns true or false.
424 
425 =for apidoc Am|bool|strLE|char* s1|char* s2
426 Test two strings to see if the first, C<s1>, is less than or equal to the
427 second, C<s2>.  Returns true or false.
428 
429 =for apidoc Am|bool|strGT|char* s1|char* s2
430 Test two strings to see if the first, C<s1>, is greater than the second,
431 C<s2>.  Returns true or false.
432 
433 =for apidoc Am|bool|strGE|char* s1|char* s2
434 Test two strings to see if the first, C<s1>, is greater than or equal to
435 the second, C<s2>.  Returns true or false.
436 
437 =for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
438 Test two strings to see if they are different.  The C<len> parameter
439 indicates the number of bytes to compare.  Returns true or false.  (A
440 wrapper for C<strncmp>).
441 
442 =for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
443 Test two strings to see if they are equal.  The C<len> parameter indicates
444 the number of bytes to compare.  Returns true or false.  (A wrapper for
445 C<strncmp>).
446 
447 =cut
448 */
449 
450 #define strNE(s1,s2) (strcmp(s1,s2))
451 #define strEQ(s1,s2) (!strcmp(s1,s2))
452 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
453 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
454 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
455 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
456 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
457 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
458 
459 #ifdef HAS_MEMCMP
460 #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
461 #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
462 #else
463 #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
464 #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
465 #endif
466 
467 #define memEQs(s1, l, s2) \
468 	(sizeof(s2)-1 == l && memEQ(s1, ("" s2 ""), (sizeof(s2)-1)))
469 #define memNEs(s1, l, s2) !memEQs(s1, l, s2)
470 
471 /*
472  * Character classes.
473  *
474  * Unfortunately, the introduction of locales means that we
475  * can't trust isupper(), etc. to tell the truth.  And when
476  * it comes to /\w+/ with tainting enabled, we *must* be able
477  * to trust our character classes.
478  *
479  * Therefore, the default tests in the text of Perl will be
480  * independent of locale.  Any code that wants to depend on
481  * the current locale will use the tests that begin with "lc".
482  */
483 
484 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
485 #  ifndef CTYPE256
486 #    define CTYPE256
487 #  endif
488 #endif
489 
490 /*
491 
492 =head1 Character classes
493 This section is about functions (really macros) that classify characters
494 into types, such as punctuation versus alphabetic, etc.  Most of these are
495 analogous to regular expression character classes.  (See
496 L<perlrecharclass/POSIX Character Classes>.)  There are several variants for
497 each class.  (Not all macros have all variants; each item below lists the
498 ones valid for it.)  None are affected by C<use bytes>, and only the ones
499 with C<LC> in the name are affected by the current locale.
500 
501 The base function, e.g., C<isALPHA()>, takes an octet (either a C<char> or a
502 C<U8>) as input and returns a boolean as to whether or not the character
503 represented by that octet is (or on non-ASCII platforms, corresponds to) an
504 ASCII character in the named class based on platform, Unicode, and Perl rules.
505 If the input is a number that doesn't fit in an octet, FALSE is returned.
506 
507 Variant C<isFOO_A> (e.g., C<isALPHA_A()>) is identical to the base function
508 with no suffix C<"_A">.
509 
510 Variant C<isFOO_L1> imposes the Latin-1 (or EBCDIC equivlalent) character set
511 onto the platform.  That is, the code points that are ASCII are unaffected,
512 since ASCII is a subset of Latin-1.  But the non-ASCII code points are treated
513 as if they are Latin-1 characters.  For example, C<isWORDCHAR_L1()> will return
514 true when called with the code point 0xDF, which is a word character in both
515 ASCII and EBCDIC (though it represents different characters in each).
516 
517 Variant C<isFOO_uni> is like the C<isFOO_L1> variant, but accepts any UV code
518 point as input.  If the code point is larger than 255, Unicode rules are used
519 to determine if it is in the character class.  For example,
520 C<isWORDCHAR_uni(0x100)> returns TRUE, since 0x100 is LATIN CAPITAL LETTER A
521 WITH MACRON in Unicode, and is a word character.
522 
523 Variant C<isFOO_utf8> is like C<isFOO_uni>, but the input is a pointer to a
524 (known to be well-formed) UTF-8 encoded string (C<U8*> or C<char*>).  The
525 classification of just the first (possibly multi-byte) character in the string
526 is tested.
527 
528 Variant C<isFOO_LC> is like the C<isFOO_A> and C<isFOO_L1> variants, but the
529 result is based on the current locale, which is what C<LC> in the name stands
530 for.  If Perl can determine that the current locale is a UTF-8 locale, it uses
531 the published Unicode rules; otherwise, it uses the C library function that
532 gives the named classification.  For example, C<isDIGIT_LC()> when not in a
533 UTF-8 locale returns the result of calling C<isdigit()>.  FALSE is always
534 returned if the input won't fit into an octet.
535 
536 Variant C<isFOO_LC_uvchr> is like C<isFOO_LC>, but is defined on any UV.  It
537 returns the same as C<isFOO_LC> for input code points less than 256, and
538 returns the hard-coded, not-affected-by-locale, Unicode results for larger ones.
539 
540 Variant C<isFOO_LC_utf8> is like C<isFOO_LC_uvchr>, but the input is a pointer to a
541 (known to be well-formed) UTF-8 encoded string (C<U8*> or C<char*>).  The
542 classification of just the first (possibly multi-byte) character in the string
543 is tested.
544 
545 =for apidoc Am|bool|isALPHA|char ch
546 Returns a boolean indicating whether the specified character is an
547 alphabetic character, analogous to C<m/[[:alpha:]]/>.
548 See the L<top of this section|/Character classes> for an explanation of variants
549 C<isALPHA_A>, C<isALPHA_L1>, C<isALPHA_uni>, C<isALPHA_utf8>, C<isALPHA_LC>,
550 C<isALPHA_LC_uvchr>, and C<isALPHA_LC_utf8>.
551 
552 =for apidoc Am|bool|isALPHANUMERIC|char ch
553 Returns a boolean indicating whether the specified character is a either an
554 alphabetic character or decimal digit, analogous to C<m/[[:alnum:]]/>.
555 See the L<top of this section|/Character classes> for an explanation of variants
556 C<isALPHANUMERIC_A>, C<isALPHANUMERIC_L1>, C<isALPHANUMERIC_uni>,
557 C<isALPHANUMERIC_utf8>, C<isALPHANUMERIC_LC>, C<isALPHANUMERIC_LC_uvchr>, and
558 C<isALPHANUMERIC_LC_utf8>.
559 
560 =for apidoc Am|bool|isASCII|char ch
561 Returns a boolean indicating whether the specified character is one of the 128
562 characters in the ASCII character set, analogous to C<m/[[:ascii:]]/>.
563 On non-ASCII platforms, it returns TRUE iff this
564 character corresponds to an ASCII character.  Variants C<isASCII_A()> and
565 C<isASCII_L1()> are identical to C<isASCII()>.
566 See the L<top of this section|/Character classes> for an explanation of variants
567 C<isASCII_uni>, C<isASCII_utf8>, C<isASCII_LC>, C<isASCII_LC_uvchr>, and
568 C<isASCII_LC_utf8>.  Note, however, that some platforms do not have the C
569 library routine C<isascii()>.  In these cases, the variants whose names contain
570 C<LC> are the same as the corresponding ones without.
571 
572 Also note, that because all ASCII characters are UTF-8 invariant (meaning they
573 have the exact same representation (always a single byte) whether encoded in
574 UTF-8 or not), C<isASCII> will give the correct results when called with any
575 byte in any string encoded or not in UTF-8.  And similarly C<isASCII_utf8> will
576 work properly on any string encoded or not in UTF-8.
577 
578 =for apidoc Am|bool|isBLANK|char ch
579 Returns a boolean indicating whether the specified character is a
580 character considered to be a blank, analogous to C<m/[[:blank:]]/>.
581 See the L<top of this section|/Character classes> for an explanation of variants
582 C<isBLANK_A>, C<isBLANK_L1>, C<isBLANK_uni>, C<isBLANK_utf8>, C<isBLANK_LC>,
583 C<isBLANK_LC_uvchr>, and C<isBLANK_LC_utf8>.  Note, however, that some
584 platforms do not have the C library routine C<isblank()>.  In these cases, the
585 variants whose names contain C<LC> are the same as the corresponding ones
586 without.
587 
588 =for apidoc Am|bool|isCNTRL|char ch
589 Returns a boolean indicating whether the specified character is a
590 control character, analogous to C<m/[[:cntrl:]]/>.
591 See the L<top of this section|/Character classes> for an explanation of variants
592 C<isCNTRL_A>, C<isCNTRL_L1>, C<isCNTRL_uni>, C<isCNTRL_utf8>, C<isCNTRL_LC>,
593 C<isCNTRL_LC_uvchr>, and C<isCNTRL_LC_utf8>
594 On EBCDIC platforms, you almost always want to use the C<isCNTRL_L1> variant.
595 
596 =for apidoc Am|bool|isDIGIT|char ch
597 Returns a boolean indicating whether the specified character is a
598 digit, analogous to C<m/[[:digit:]]/>.
599 Variants C<isDIGIT_A> and C<isDIGIT_L1> are identical to C<isDIGIT>.
600 See the L<top of this section|/Character classes> for an explanation of variants
601 C<isDIGIT_uni>, C<isDIGIT_utf8>, C<isDIGIT_LC>, C<isDIGIT_LC_uvchr>, and
602 C<isDIGIT_LC_utf8>.
603 
604 =for apidoc Am|bool|isGRAPH|char ch
605 Returns a boolean indicating whether the specified character is a
606 graphic character, analogous to C<m/[[:graph:]]/>.
607 See the L<top of this section|/Character classes> for an explanation of variants
608 C<isGRAPH_A>, C<isGRAPH_L1>, C<isGRAPH_uni>, C<isGRAPH_utf8>, C<isGRAPH_LC>,
609 C<isGRAPH_LC_uvchr>, and C<isGRAPH_LC_utf8>.
610 
611 =for apidoc Am|bool|isLOWER|char ch
612 Returns a boolean indicating whether the specified character is a
613 lowercase character, analogous to C<m/[[:lower:]]/>.
614 See the L<top of this section|/Character classes> for an explanation of variants
615 C<isLOWER_A>, C<isLOWER_L1>, C<isLOWER_uni>, C<isLOWER_utf8>, C<isLOWER_LC>,
616 C<isLOWER_LC_uvchr>, and C<isLOWER_LC_utf8>.
617 
618 =for apidoc Am|bool|isOCTAL|char ch
619 Returns a boolean indicating whether the specified character is an
620 octal digit, [0-7].
621 The only two variants are C<isOCTAL_A> and C<isOCTAL_L1>; each is identical to
622 C<isOCTAL>.
623 
624 =for apidoc Am|bool|isPUNCT|char ch
625 Returns a boolean indicating whether the specified character is a
626 punctuation character, analogous to C<m/[[:punct:]]/>.
627 Note that the definition of what is punctuation isn't as
628 straightforward as one might desire.  See L<perlrecharclass/POSIX Character
629 Classes> for details.
630 See the L<top of this section|/Character classes> for an explanation of variants
631 C<isPUNCT_A>, C<isPUNCT_L1>, C<isPUNCT_uni>, C<isPUNCT_utf8>, C<isPUNCT_LC>,
632 C<isPUNCT_LC_uvchr>, and C<isPUNCT_LC_utf8>.
633 
634 =for apidoc Am|bool|isSPACE|char ch
635 Returns a boolean indicating whether the specified character is a
636 whitespace character.  This is analogous
637 to what C<m/\s/> matches in a regular expression.  Starting in Perl 5.18
638 (experimentally), this also matches what C<m/[[:space:]]/> does.
639 ("Experimentally" means that this change may be backed out in 5.22 if
640 field experience indicates that it was unwise.)  Prior to 5.18, only the
641 locale forms of this macro (the ones with C<LC> in their names) matched
642 precisely what C<m/[[:space:]]/> does.  In those releases, the only difference,
643 in the non-locale variants, was that C<isSPACE()> did not match a vertical tab.
644 (See L</isPSXSPC> for a macro that matches a vertical tab in all releases.)
645 See the L<top of this section|/Character classes> for an explanation of variants
646 C<isSPACE_A>, C<isSPACE_L1>, C<isSPACE_uni>, C<isSPACE_utf8>, C<isSPACE_LC>,
647 C<isSPACE_LC_uvchr>, and C<isSPACE_LC_utf8>.
648 
649 =for apidoc Am|bool|isPSXSPC|char ch
650 (short for Posix Space)
651 Starting in 5.18, this is identical (experimentally) in all its forms to the
652 corresponding C<isSPACE()> macros.  ("Experimentally" means that this change
653 may be backed out in 5.22 if field experience indicates that it
654 was unwise.)
655 The locale forms of this macro are identical to their corresponding
656 C<isSPACE()> forms in all Perl releases.  In releases prior to 5.18, the
657 non-locale forms differ from their C<isSPACE()> forms only in that the
658 C<isSPACE()> forms don't match a Vertical Tab, and the C<isPSXSPC()> forms do.
659 Otherwise they are identical.  Thus this macro is analogous to what
660 C<m/[[:space:]]/> matches in a regular expression.
661 See the L<top of this section|/Character classes> for an explanation of variants
662 C<isPSXSPC_A>, C<isPSXSPC_L1>, C<isPSXSPC_uni>, C<isPSXSPC_utf8>, C<isPSXSPC_LC>,
663 C<isPSXSPC_LC_uvchr>, and C<isPSXSPC_LC_utf8>.
664 
665 =for apidoc Am|bool|isUPPER|char ch
666 Returns a boolean indicating whether the specified character is an
667 uppercase character, analogous to C<m/[[:upper:]]/>.
668 See the L<top of this section|/Character classes> for an explanation of variants
669 C<isUPPER_A>, C<isUPPER_L1>, C<isUPPER_uni>, C<isUPPER_utf8>, C<isUPPER_LC>,
670 C<isUPPER_LC_uvchr>, and C<isUPPER_LC_utf8>.
671 
672 =for apidoc Am|bool|isPRINT|char ch
673 Returns a boolean indicating whether the specified character is a
674 printable character, analogous to C<m/[[:print:]]/>.
675 See the L<top of this section|/Character classes> for an explanation of variants
676 C<isPRINT_A>, C<isPRINT_L1>, C<isPRINT_uni>, C<isPRINT_utf8>, C<isPRINT_LC>,
677 C<isPRINT_LC_uvchr>, and C<isPRINT_LC_utf8>.
678 
679 =for apidoc Am|bool|isWORDCHAR|char ch
680 Returns a boolean indicating whether the specified character is a character
681 that is a word character, analogous to what C<m/\w/> and C<m/[[:word:]]/> match
682 in a regular expression.  A word character is an alphabetic character, a
683 decimal digit, a connecting punctuation character (such as an underscore), or
684 a "mark" character that attaches to one of those (like some sort of accent).
685 C<isALNUM()> is a synonym provided for backward compatibility, even though a
686 word character includes more than the standard C language meaning of
687 alphanumeric.
688 See the L<top of this section|/Character classes> for an explanation of variants
689 C<isWORDCHAR_A>, C<isWORDCHAR_L1>, C<isWORDCHAR_uni>, C<isWORDCHAR_utf8>,
690 C<isWORDCHAR_LC>, C<isWORDCHAR_LC_uvchr>, and C<isWORDCHAR_LC_utf8>.
691 
692 =for apidoc Am|bool|isXDIGIT|char ch
693 Returns a boolean indicating whether the specified character is a hexadecimal
694 digit.  In the ASCII range these are C<[0-9A-Fa-f]>.  Variants C<isXDIGIT_A()>
695 and C<isXDIGIT_L1()> are identical to C<isXDIGIT()>.
696 See the L<top of this section|/Character classes> for an explanation of variants
697 C<isXDIGIT_uni>, C<isXDIGIT_utf8>, C<isXDIGIT_LC>, C<isXDIGIT_LC_uvchr>, and
698 C<isXDIGIT_LC_utf8>.
699 
700 =for apidoc Am|bool|isIDFIRST|char ch
701 Returns a boolean indicating whether the specified character can be the first
702 character of an identifier.  This is very close to, but not quite the same as
703 the official Unicode property C<XID_Start>.  The difference is that this
704 returns true only if the input character also matches L</isWORDCHAR>.
705 See the L<top of this section|/Character classes> for an explanation of variants
706 C<isIDFIRST_A>, C<isIDFIRST_L1>, C<isIDFIRST_uni>, C<isIDFIRST_utf8>,
707 C<isIDFIRST_LC>, C<isIDFIRST_LC_uvchr>, and C<isIDFIRST_LC_utf8>.
708 
709 =for apidoc Am|bool|isIDCONT|char ch
710 Returns a boolean indicating whether the specified character can be the
711 second or succeeding character of an identifier.  This is very close to, but
712 not quite the same as the official Unicode property C<XID_Continue>.  The
713 difference is that this returns true only if the input character also matches
714 L</isWORDCHAR>.  See the L<top of this section|/Character classes> for an
715 explanation of variants C<isIDCONT_A>, C<isIDCONT_L1>, C<isIDCONT_uni>,
716 C<isIDCONT_utf8>, C<isIDCONT_LC>, C<isIDCONT_LC_uvchr>, and
717 C<isIDCONT_LC_utf8>.
718 
719 =head1 Miscellaneous Functions
720 
721 =for apidoc Am|U8|READ_XDIGIT|char str*
722 Returns the value of an ASCII-range hex digit and advances the string pointer.
723 Behaviour is only well defined when isXDIGIT(*str) is true.
724 
725 =head1 Character case changing
726 
727 =for apidoc Am|U8|toUPPER|U8 ch
728 Converts the specified character to uppercase.  If the input is anything but an
729 ASCII lowercase character, that input character itself is returned.  Variant
730 C<toUPPER_A> is equivalent.
731 
732 =for apidoc Am|UV|toUPPER_uni|UV cp|U8* s|STRLEN* lenp
733 Converts the Unicode code point C<cp> to its uppercase version, and
734 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
735 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
736 bytes since the uppercase version may be longer than the original character.
737 
738 The first code point of the uppercased version is returned
739 (but note, as explained just above, that there may be more.)
740 
741 =for apidoc Am|UV|toUPPER_utf8|U8* p|U8* s|STRLEN* lenp
742 Converts the UTF-8 encoded character at C<p> to its uppercase version, and
743 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
744 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
745 bytes since the uppercase version may be longer than the original character.
746 
747 The first code point of the uppercased version is returned
748 (but note, as explained just above, that there may be more.)
749 
750 The input character at C<p> is assumed to be well-formed.
751 
752 =for apidoc Am|U8|toFOLD|U8 ch
753 Converts the specified character to foldcase.  If the input is anything but an
754 ASCII uppercase character, that input character itself is returned.  Variant
755 C<toFOLD_A> is equivalent.  (There is no equivalent C<to_FOLD_L1> for the full
756 Latin1 range, as the full generality of L</toFOLD_uni> is needed there.)
757 
758 =for apidoc Am|UV|toFOLD_uni|UV cp|U8* s|STRLEN* lenp
759 Converts the Unicode code point C<cp> to its foldcase version, and
760 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
761 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
762 bytes since the foldcase version may be longer than the original character.
763 
764 The first code point of the foldcased version is returned
765 (but note, as explained just above, that there may be more.)
766 
767 =for apidoc Am|UV|toFOLD_utf8|U8* p|U8* s|STRLEN* lenp
768 Converts the UTF-8 encoded character at C<p> to its foldcase version, and
769 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
770 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
771 bytes since the foldcase version may be longer than the original character.
772 
773 The first code point of the foldcased version is returned
774 (but note, as explained just above, that there may be more.)
775 
776 The input character at C<p> is assumed to be well-formed.
777 
778 =for apidoc Am|U8|toLOWER|U8 ch
779 Converts the specified character to lowercase.  If the input is anything but an
780 ASCII uppercase character, that input character itself is returned.  Variant
781 C<toLOWER_A> is equivalent.
782 
783 =for apidoc Am|U8|toLOWER_L1|U8 ch
784 Converts the specified Latin1 character to lowercase.  The results are undefined if
785 the input doesn't fit in a byte.
786 
787 =for apidoc Am|U8|toLOWER_LC|U8 ch
788 Converts the specified character to lowercase using the current locale's rules,
789 if possible; otherwise returns the input character itself.
790 
791 =for apidoc Am|UV|toLOWER_uni|UV cp|U8* s|STRLEN* lenp
792 Converts the Unicode code point C<cp> to its lowercase version, and
793 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
794 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
795 bytes since the lowercase version may be longer than the original character.
796 
797 The first code point of the lowercased version is returned
798 (but note, as explained just above, that there may be more.)
799 
800 =for apidoc Am|UV|toLOWER_utf8|U8* p|U8* s|STRLEN* lenp
801 Converts the UTF-8 encoded character at C<p> to its lowercase version, and
802 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
803 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
804 bytes since the lowercase version may be longer than the original character.
805 
806 The first code point of the lowercased version is returned
807 (but note, as explained just above, that there may be more.)
808 
809 The input character at C<p> is assumed to be well-formed.
810 
811 =for apidoc Am|U8|toLOWER_LC|U8 ch
812 Converts the specified character to lowercase using the current locale's rules,
813 if possible; otherwise returns the input character itself.
814 
815 =for apidoc Am|U8|toTITLE|U8 ch
816 Converts the specified character to titlecase.  If the input is anything but an
817 ASCII lowercase character, that input character itself is returned.  Variant
818 C<toTITLE_A> is equivalent.  (There is no C<toTITLE_L1> for the full Latin1 range,
819 as the full generality of L</toTITLE_uni> is needed there.  Titlecase is not a
820 concept used in locale handling, so there is no functionality for that.)
821 
822 =for apidoc Am|UV|toTITLE_uni|UV cp|U8* s|STRLEN* lenp
823 Converts the Unicode code point C<cp> to its titlecase version, and
824 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
825 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
826 bytes since the titlecase version may be longer than the original character.
827 
828 The first code point of the titlecased version is returned
829 (but note, as explained just above, that there may be more.)
830 
831 =for apidoc Am|UV|toTITLE_utf8|U8* p|U8* s|STRLEN* lenp
832 Converts the UTF-8 encoded character at C<p> to its titlecase version, and
833 stores that in UTF-8 in C<s>, and its length in bytes in C<lenp>.  Note
834 that the buffer pointed to by C<s> needs to be at least C<UTF8_MAXBYTES_CASE+1>
835 bytes since the titlecase version may be longer than the original character.
836 
837 The first code point of the titlecased version is returned
838 (but note, as explained just above, that there may be more.)
839 
840 The input character at C<p> is assumed to be well-formed.
841 
842 =cut
843 
844 XXX Still undocumented isVERTWS_uni and _utf8; it's unclear what their names
845 really should be.  Also toUPPER_LC and toFOLD_LC, which are subject to change.
846 
847 Note that these macros are repeated in Devel::PPPort, so should also be
848 patched there.  The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
849 
850 */
851 
852 /* Specify the widest unsigned type on the platform.  Use U64TYPE because U64
853  * is known only in the perl core, and this macro can be called from outside
854  * that */
855 #ifdef HAS_QUAD
856 #   define WIDEST_UTYPE U64TYPE
857 #else
858 #   define WIDEST_UTYPE U32
859 #endif
860 
861 /* FITS_IN_8_BITS(c) returns true if c doesn't have  a bit set other than in
862  * the lower 8.  It is designed to be hopefully bomb-proof, making sure that no
863  * bits of information are lost even on a 64-bit machine, but to get the
864  * compiler to optimize it out if possible.  This is because Configure makes
865  * sure that the machine has an 8-bit byte, so if c is stored in a byte, the
866  * sizeof() guarantees that this evaluates to a constant true at compile time.
867  */
868 #define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || !(((WIDEST_UTYPE)(c)) & ~0xFF))
869 
870 #ifdef EBCDIC
871 #   ifndef _ALL_SOURCE
872         /* This returns the wrong results on at least z/OS unless this is
873          * defined. */
874 #       error   _ALL_SOURCE should probably be defined
875 #   endif
876 
877     /* We could be called without perl.h, in which case NATIVE_TO_ASCII() is
878      * likely not defined, and so we use the native function */
879 #   define isASCII(c)    cBOOL(isascii(c))
880 #else
881 #   define isASCII(c)    ((WIDEST_UTYPE)(c) < 128)
882 #endif
883 
884 #define isASCII_A(c)  isASCII(c)
885 #define isASCII_L1(c)  isASCII(c)
886 
887 /* The lower 3 bits in both the ASCII and EBCDIC representations of '0' are 0,
888  * and the 8 possible permutations of those bits exactly comprise the 8 octal
889  * digits */
890 #define isOCTAL_A(c)  cBOOL(FITS_IN_8_BITS(c) && (0xF8 & (c)) == '0')
891 
892 /* ASCII range only */
893 #ifdef H_PERL       /* If have access to perl.h, lookup in its table */
894 
895 /* Character class numbers.  For internal core Perl use only.  The ones less
896  * than 32 are used in PL_charclass[] and the ones up through the one that
897  * corresponds to <_HIGHEST_REGCOMP_DOT_H_SYNC> are used by regcomp.h and
898  * related files.  PL_charclass ones use names used in l1_char_class_tab.h but
899  * their actual definitions are here.  If that file has a name not used here,
900  * it won't compile.
901  *
902  * The first group of these is ordered in what I (khw) estimate to be the
903  * frequency of their use.  This gives a slight edge to exiting a loop earlier
904  * (in reginclass() in regexec.c) */
905 #  define _CC_WORDCHAR           0      /* \w and [:word:] */
906 #  define _CC_DIGIT              1      /* \d and [:digit:] */
907 #  define _CC_ALPHA              2      /* [:alpha:] */
908 #  define _CC_LOWER              3      /* [:lower:] */
909 #  define _CC_UPPER              4      /* [:upper:] */
910 #  define _CC_PUNCT              5      /* [:punct:] */
911 #  define _CC_PRINT              6      /* [:print:] */
912 #  define _CC_ALPHANUMERIC       7      /* [:alnum:] */
913 #  define _CC_GRAPH              8      /* [:graph:] */
914 #  define _CC_CASED              9      /* [:lower:] and [:upper:] under /i */
915 
916 #define _FIRST_NON_SWASH_CC     10
917 /* The character classes above are implemented with swashes.  The second group
918  * (just below) contains the ones implemented without.  These are also sorted
919  * in rough order of the frequency of their use, except that \v should be last,
920  * as it isn't a real Posix character class, and some (small) inefficiencies in
921  * regular expression handling would be introduced by putting it in the middle
922  * of those that are.  Also, cntrl and ascii come after the others as it may be
923  * useful to group these which have no members that match above Latin1, (or
924  * above ASCII in the latter case) */
925 
926 #  define _CC_SPACE             10      /* \s */
927 #  define _CC_BLANK             11      /* [:blank:] */
928 #  define _CC_XDIGIT            12      /* [:xdigit:] */
929 #  define _CC_PSXSPC            13      /* [:space:] */
930 #  define _CC_CNTRL             14      /* [:cntrl:] */
931 #  define _CC_ASCII             15      /* [:ascii:] */
932 #  define _CC_VERTSPACE         16      /* \v */
933 
934 #  define _HIGHEST_REGCOMP_DOT_H_SYNC _CC_VERTSPACE
935 
936 /* The members of the third group below do not need to be coordinated with data
937  * structures in regcomp.[ch] and regexec.c.  But they should be added to
938  * bootstrap_ctype() */
939 #  define _CC_IDFIRST           17
940 #  define _CC_CHARNAME_CONT     18
941 #  define _CC_NONLATIN1_FOLD    19
942 #  define _CC_QUOTEMETA         20
943 #  define _CC_NON_FINAL_FOLD    21
944 #  define _CC_IS_IN_SOME_FOLD   22
945 #  define _CC_BACKSLASH_FOO_LBRACE_IS_META 31 /* temp, see mk_PL_charclass.pl */
946 /* Unused: 23-30
947  * If more bits are needed, one could add a second word for non-64bit
948  * QUAD_IS_INT systems, using some #ifdefs to distinguish between having a 2nd
949  * word or not.  The IS_IN_SOME_FOLD bit is the most easily expendable, as it
950  * is used only for optimization (as of this writing), and differs in the
951  * Latin1 range from the ALPHA bit only in two relatively unimportant
952  * characters: the masculine and feminine ordinal indicators, so removing it
953  * would just cause /i regexes which match them to run less efficiently */
954 
955 #if defined(PERL_CORE) || defined(PERL_EXT)
956 /* An enum version of the character class numbers, to help compilers
957  * optimize */
958 typedef enum {
959     _CC_ENUM_ALPHA          = _CC_ALPHA,
960     _CC_ENUM_ALPHANUMERIC   = _CC_ALPHANUMERIC,
961     _CC_ENUM_ASCII          = _CC_ASCII,
962     _CC_ENUM_BLANK          = _CC_BLANK,
963     _CC_ENUM_CASED          = _CC_CASED,
964     _CC_ENUM_CNTRL          = _CC_CNTRL,
965     _CC_ENUM_DIGIT          = _CC_DIGIT,
966     _CC_ENUM_GRAPH          = _CC_GRAPH,
967     _CC_ENUM_LOWER          = _CC_LOWER,
968     _CC_ENUM_PRINT          = _CC_PRINT,
969     _CC_ENUM_PSXSPC         = _CC_PSXSPC,
970     _CC_ENUM_PUNCT          = _CC_PUNCT,
971     _CC_ENUM_SPACE          = _CC_SPACE,
972     _CC_ENUM_UPPER          = _CC_UPPER,
973     _CC_ENUM_VERTSPACE      = _CC_VERTSPACE,
974     _CC_ENUM_WORDCHAR       = _CC_WORDCHAR,
975     _CC_ENUM_XDIGIT         = _CC_XDIGIT
976 } _char_class_number;
977 #endif
978 
979 #define POSIX_SWASH_COUNT _FIRST_NON_SWASH_CC
980 #define POSIX_CC_COUNT    (_HIGHEST_REGCOMP_DOT_H_SYNC + 1)
981 
982 #if defined(PERL_IN_UTF8_C) || defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
983 #   if _CC_WORDCHAR != 0 || _CC_DIGIT != 1 || _CC_ALPHA != 2 || _CC_LOWER != 3 \
984        || _CC_UPPER != 4 || _CC_PUNCT != 5 || _CC_PRINT != 6                   \
985        || _CC_ALPHANUMERIC != 7 || _CC_GRAPH != 8 || _CC_CASED != 9
986       #error Need to adjust order of swash_property_names[]
987 #   endif
988 
989 /* This is declared static in each of the few files that this is #defined for
990  * to keep them from being publicly accessible.  Hence there is a small amount
991  * of wasted space */
992 
993 static const char* const swash_property_names[] = {
994     "XPosixWord",
995     "XPosixDigit",
996     "XPosixAlpha",
997     "XPosixLower",
998     "XPosixUpper",
999     "XPosixPunct",
1000     "XPosixPrint",
1001     "XPosixAlnum",
1002     "XPosixGraph",
1003     "Cased"
1004 };
1005 #endif
1006 
1007 #  ifdef DOINIT
1008 EXTCONST  U32 PL_charclass[] = {
1009 #    include "l1_char_class_tab.h"
1010 };
1011 
1012 #  else /* ! DOINIT */
1013 EXTCONST U32 PL_charclass[];
1014 #  endif
1015 #endif  /* Has perl.h */
1016 
1017 #if defined(H_PERL) && ! defined(BOOTSTRAP_CHARSET)
1018 
1019     /* The 1U keeps Solaris from griping when shifting sets the uppermost bit */
1020 #   define _CC_mask(classnum) (1U << (classnum))
1021 
1022     /* For internal core Perl use only: the base macro for defining macros like
1023      * isALPHA */
1024 #   define _generic_isCC(c, classnum) cBOOL(FITS_IN_8_BITS(c) \
1025                 && (PL_charclass[(U8) (c)] & _CC_mask(classnum)))
1026 
1027     /* The mask for the _A versions of the macros; it just adds in the bit for
1028      * ASCII. */
1029 #   define _CC_mask_A(classnum) (_CC_mask(classnum) | _CC_mask(_CC_ASCII))
1030 
1031     /* For internal core Perl use only: the base macro for defining macros like
1032      * isALPHA_A.  The foo_A version makes sure that both the desired bit and
1033      * the ASCII bit are present */
1034 #   define _generic_isCC_A(c, classnum) (FITS_IN_8_BITS(c)  \
1035         && ((PL_charclass[(U8) (c)] & _CC_mask_A(classnum)) \
1036                                 == _CC_mask_A(classnum)))
1037 
1038 #   define isALPHA_A(c)  _generic_isCC_A(c, _CC_ALPHA)
1039 #   define isALPHANUMERIC_A(c) _generic_isCC_A(c, _CC_ALPHANUMERIC)
1040 #   define isBLANK_A(c)  _generic_isCC_A(c, _CC_BLANK)
1041 #   define isCNTRL_A(c)  _generic_isCC_A(c, _CC_CNTRL)
1042 #   define isDIGIT_A(c)  _generic_isCC(c, _CC_DIGIT)
1043 #   define isGRAPH_A(c)  _generic_isCC_A(c, _CC_GRAPH)
1044 #   define isLOWER_A(c)  _generic_isCC_A(c, _CC_LOWER)
1045 #   define isPRINT_A(c)  _generic_isCC_A(c, _CC_PRINT)
1046 #   define isPSXSPC_A(c) _generic_isCC_A(c, _CC_PSXSPC)
1047 #   define isPUNCT_A(c)  _generic_isCC_A(c, _CC_PUNCT)
1048 #   define isSPACE_A(c)  _generic_isCC_A(c, _CC_SPACE)
1049 #   define isUPPER_A(c)  _generic_isCC_A(c, _CC_UPPER)
1050 #   define isWORDCHAR_A(c) _generic_isCC_A(c, _CC_WORDCHAR)
1051 #   define isXDIGIT_A(c)  _generic_isCC(c, _CC_XDIGIT)
1052 #   define isIDFIRST_A(c) _generic_isCC_A(c, _CC_IDFIRST)
1053 #   define isALPHA_L1(c)  _generic_isCC(c, _CC_ALPHA)
1054 #   define isALPHANUMERIC_L1(c) _generic_isCC(c, _CC_ALPHANUMERIC)
1055 #   define isBLANK_L1(c)  _generic_isCC(c, _CC_BLANK)
1056 
1057     /* continuation character for legal NAME in \N{NAME} */
1058 #   define isCHARNAME_CONT(c) _generic_isCC(c, _CC_CHARNAME_CONT)
1059 
1060 #   define isCNTRL_L1(c)  _generic_isCC(c, _CC_CNTRL)
1061 #   define isGRAPH_L1(c)  _generic_isCC(c, _CC_GRAPH)
1062 #   define isLOWER_L1(c)  _generic_isCC(c, _CC_LOWER)
1063 #   define isPRINT_L1(c)  _generic_isCC(c, _CC_PRINT)
1064 #   define isPSXSPC_L1(c) _generic_isCC(c, _CC_PSXSPC)
1065 #   define isPUNCT_L1(c)  _generic_isCC(c, _CC_PUNCT)
1066 #   define isSPACE_L1(c)  _generic_isCC(c, _CC_SPACE)
1067 #   define isUPPER_L1(c)  _generic_isCC(c, _CC_UPPER)
1068 #   define isWORDCHAR_L1(c) _generic_isCC(c, _CC_WORDCHAR)
1069 #   define isIDFIRST_L1(c) _generic_isCC(c, _CC_IDFIRST)
1070 
1071     /* Either participates in a fold with a character above 255, or is a
1072      * multi-char fold */
1073 #   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) (c)] & _CC_mask(_CC_NONLATIN1_FOLD)))
1074 
1075 #   define _isQUOTEMETA(c) _generic_isCC(c, _CC_QUOTEMETA)
1076 #   define _IS_NON_FINAL_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) \
1077                                             _generic_isCC(c, _CC_NON_FINAL_FOLD)
1078 #   define _IS_IN_SOME_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) \
1079                                             _generic_isCC(c, _CC_IS_IN_SOME_FOLD)
1080 #else   /* Either don't have perl.h or don't want to use char_class_tab.h */
1081 
1082     /* If we don't have perl.h, we are compiling a utility program.  Below we
1083      * hard-code various macro definitions that wouldn't otherwise be available
1084      * to it.  We can also get here if we are configured to bootstrap up Perl
1085      * on a non-ASCII platform that doesn't have a working Perl (currently only
1086      * EBCDIC).  For these we currently use the native definitions to get
1087      * things going.  (It should also be possible to use the translation
1088      * function NATIVE_TO_LATIN1(), but that is an extra layer of dependence on
1089      * Perl, so it is currently avoided for the macros where it's possible to
1090      * do so.) */
1091 #   ifdef EBCDIC
1092         /* Use the native functions.  They likely will return false for all
1093          * non-ASCII values, but this makes sure */
1094 #       define isALPHA_A(c)    (isASCII(c) && isalpha(c))
1095 #       define isALPHANUMERIC_A(c) (isASCII(c) && isalnum(c))
1096 #       define isCNTRL_A(c)    (isASCII(c) && iscntrl(c))
1097 #       define isDIGIT_A(c)    (isASCII(c) && isdigit(c))
1098 #       define isGRAPH_A(c)    (isASCII(c) && isgraph(c))
1099 #       define isLOWER_A(c)    (isASCII(c) && islower(c))
1100 #       define isPRINT_A(c)    (isASCII(c) && isprint(c))
1101 #       define isPUNCT_A(c)    (isASCII(c) && ispunct(c))
1102 #       define isSPACE_A(c)    (isASCII(c) && isspace(c))
1103 #       define isUPPER_A(c)    (isASCII(c) && isupper(c))
1104 #       define isXDIGIT_A(c)   (isASCII(c) && isxdigit(c))
1105 #   else   /* ASCII platform.  These are coded based on first principals */
1106 #       define isALPHA_A(c)  (isUPPER_A(c) || isLOWER_A(c))
1107 #       define isALPHANUMERIC_A(c) (isALPHA_A(c) || isDIGIT_A(c))
1108 #       define isCNTRL_A(c)  (isASCII(c) && (! isPRINT_A(c)))
1109 #       define isDIGIT_A(c)  ((c) <= '9' && (c) >= '0')
1110 #       define isGRAPH_A(c)  (isPRINT_A(c) && (c) != ' ')
1111 #       define isLOWER_A(c)  ((c) >= 'a' && (c) <= 'z')
1112 #       define isPRINT_A(c)  (((c) >= 32 && (c) < 127))
1113 #       define isPUNCT_A(c)  (isGRAPH_A(c) && (! isALPHANUMERIC_A(c)))
1114 #       define isSPACE_A(c)  ((c) == ' '                                     \
1115                               || (c) == '\t'                                 \
1116                               || (c) == '\n'                                 \
1117                               || (c) == '\r'                                 \
1118                               || (c) =='\v'                                  \
1119                               || (c) == '\f')
1120 #       define isUPPER_A(c)  ((c) <= 'Z' && (c) >= 'A')
1121 #       define isXDIGIT_A(c) (isDIGIT_A(c)                                   \
1122                               || ((c) >= 'a' && (c) <= 'f')                  \
1123                               || ((c) <= 'F' && (c) >= 'A'))
1124 #   endif   /* Below are common definitions for ASCII and non-ASCII */
1125 #   define isBLANK_A(c)      ((c) == ' ' || (c) == '\t')
1126 #   define isIDFIRST_A(c)    (isALPHA_A(c) || (c) == '_')
1127 #   define isWORDCHAR_A(c)   (isALPHANUMERIC_A(c) || (c) == '_')
1128 
1129     /* The _L1 macros may be unnecessary for both the utilities and for
1130      * bootstrapping; I (khw) added them during debugging of bootstrapping, and
1131      * it seems best to keep them. */
1132 #   define isPSXSPC_A(c)     isSPACE_A(c) /* XXX Assumes SPACE matches '\v' */
1133 #   define isALPHA_L1(c)     (isUPPER_L1(c) || isLOWER_L1(c))
1134 #   define isALPHANUMERIC_L1(c) (isALPHA_L1(c) || isDIGIT_A(c))
1135 #   define isBLANK_L1(c)     (isBLANK_A(c)                                   \
1136                               || (FITS_IN_8_BITS(c)                          \
1137                                   && NATIVE_TO_LATIN1((U8) c) == 0xA0))
1138 #   define isCNTRL_L1(c)     (FITS_IN_8_BITS(c) && (! isPRINT_L1(c)))
1139 #   define isGRAPH_L1(c)     (isPRINT_L1(c) && (! isBLANK_L1(c)))
1140 #   define isLOWER_L1(c)     (isLOWER_A(c)                                   \
1141                               || (FITS_IN_8_BITS(c)                          \
1142                                   && ((NATIVE_TO_LATIN1((U8) c) >= 0xDF      \
1143                                        && NATIVE_TO_LATIN1((U8) c) != 0xF7)  \
1144                                        || NATIVE_TO_LATIN1((U8) c) == 0xAA   \
1145                                        || NATIVE_TO_LATIN1((U8) c) == 0xBA   \
1146                                        || NATIVE_TO_LATIN1((U8) c) == 0xB5)))
1147 #   define isPRINT_L1(c)     (isPRINT_A(c)                                   \
1148                               || (FITS_IN_8_BITS(c)                          \
1149                                   && NATIVE_TO_LATIN1((U8) c) >= 0xA0))
1150 #   define isPSXSPC_L1(c)    isSPACE_L1(c)
1151 #   define isPUNCT_L1(c)     (isPUNCT_A(c)                                   \
1152                               || (FITS_IN_8_BITS(c)                          \
1153                                   && (NATIVE_TO_LATIN1((U8) c) == 0xA1       \
1154                                       || NATIVE_TO_LATIN1((U8) c) == 0xA7    \
1155                                       || NATIVE_TO_LATIN1((U8) c) == 0xAB    \
1156                                       || NATIVE_TO_LATIN1((U8) c) == 0xB6    \
1157                                       || NATIVE_TO_LATIN1((U8) c) == 0xB7    \
1158                                       || NATIVE_TO_LATIN1((U8) c) == 0xBB    \
1159                                       || NATIVE_TO_LATIN1((U8) c) == 0xBF)))
1160 #   define isSPACE_L1(c)     (isSPACE_A(c)                                   \
1161                               || (FITS_IN_8_BITS(c)                          \
1162                                   && (NATIVE_TO_LATIN1((U8) c) == 0x85       \
1163                                       || NATIVE_TO_LATIN1((U8) c) == 0xA0)))
1164 #   define isUPPER_L1(c)     (isUPPER_A(c)                                   \
1165                               || (FITS_IN_8_BITS(c)                          \
1166                                   && (NATIVE_TO_LATIN1((U8) c) >= 0xC0       \
1167                                       && NATIVE_TO_LATIN1((U8) c) <= 0xDE    \
1168                                       && NATIVE_TO_LATIN1((U8) c) != 0xD7)))
1169 #   define isWORDCHAR_L1(c)  (isIDFIRST_L1(c) || isDIGIT_A(c))
1170 #   define isIDFIRST_L1(c)   (isALPHA_L1(c) || NATIVE_TO_LATIN1(c) == '_')
1171 #   define isCHARNAME_CONT(c) (isWORDCHAR_L1(c)                              \
1172                                || isBLANK_L1(c)                              \
1173                                || (c) == '-'                                 \
1174                                || (c) == '('                                 \
1175                                || (c) == ')')
1176     /* The following are not fully accurate in the above-ASCII range.  I (khw)
1177      * don't think it's necessary to be so for the purposes where this gets
1178      * compiled */
1179 #   define _isQUOTEMETA(c)      (FITS_IN_8_BITS(c) && ! isWORDCHAR_L1(c))
1180 #   define _IS_IN_SOME_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) isALPHA_L1(c)
1181 
1182     /*  And these aren't accurate at all.  They are useful only for above
1183      *  Latin1, which utilities and bootstrapping don't deal with */
1184 #   define _IS_NON_FINAL_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) 0
1185 #   define _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(c) 0
1186 
1187     /* Many of the macros later in this file are defined in terms of these.  By
1188      * implementing them with a function, which converts the class number into
1189      * a call to the desired macro, all of the later ones work.  However, that
1190      * function won't be actually defined when building a utility program (no
1191      * perl.h), and so a compiler error will be generated if one is attempted
1192      * to be used.  And the above-Latin1 code points require Unicode tables to
1193      * be present, something unlikely to be the case when bootstrapping */
1194 #   define _generic_isCC(c, classnum)                                        \
1195          (FITS_IN_8_BITS(c) && S_bootstrap_ctype((U8) (c), (classnum), TRUE))
1196 #   define _generic_isCC_A(c, classnum)                                      \
1197          (FITS_IN_8_BITS(c) && S_bootstrap_ctype((U8) (c), (classnum), FALSE))
1198 #endif  /* End of no perl.h or have BOOTSTRAP_CHARSET */
1199 
1200 #define isALPHANUMERIC(c)  isALPHANUMERIC_A(c)
1201 #define isALPHA(c)   isALPHA_A(c)
1202 #define isBLANK(c)   isBLANK_A(c)
1203 #define isCNTRL(c)   isCNTRL_A(c)
1204 #define isDIGIT(c)   isDIGIT_A(c)
1205 #define isGRAPH(c)   isGRAPH_A(c)
1206 #define isIDFIRST(c) isIDFIRST_A(c)
1207 #define isLOWER(c)   isLOWER_A(c)
1208 #define isPRINT(c)   isPRINT_A(c)
1209 #define isPSXSPC(c)  isPSXSPC_A(c)
1210 #define isPUNCT(c)   isPUNCT_A(c)
1211 #define isSPACE(c)   isSPACE_A(c)
1212 #define isUPPER(c)   isUPPER_A(c)
1213 #define isWORDCHAR(c) isWORDCHAR_A(c)
1214 #define isXDIGIT(c)  isXDIGIT_A(c)
1215 
1216 /* ASCII casing.  These could also be written as
1217     #define toLOWER(c) (isASCII(c) ? toLOWER_LATIN1(c) : (c))
1218     #define toUPPER(c) (isASCII(c) ? toUPPER_LATIN1_MOD(c) : (c))
1219    which uses table lookup and mask instead of subtraction.  (This would
1220    work because the _MOD does not apply in the ASCII range) */
1221 #define toLOWER(c)  (isUPPER(c) ? (U8)((c) + ('a' - 'A')) : (c))
1222 #define toUPPER(c)  (isLOWER(c) ? (U8)((c) - ('a' - 'A')) : (c))
1223 
1224 /* In the ASCII range, these are equivalent to what they're here defined to be.
1225  * But by creating these definitions, other code doesn't have to be aware of
1226  * this detail */
1227 #define toFOLD(c)    toLOWER(c)
1228 #define toTITLE(c)   toUPPER(c)
1229 
1230 #define toLOWER_A(c) toLOWER(c)
1231 #define toUPPER_A(c) toUPPER(c)
1232 #define toFOLD_A(c)  toFOLD(c)
1233 #define toTITLE_A(c) toTITLE(c)
1234 
1235 /* Use table lookup for speed; returns the input itself if is out-of-range */
1236 #define toLOWER_LATIN1(c)    ((! FITS_IN_8_BITS(c))                        \
1237                              ? (c)                                         \
1238                              : PL_latin1_lc[ (U8) (c) ])
1239 #define toLOWER_L1(c)    toLOWER_LATIN1(c)  /* Synonym for consistency */
1240 
1241 /* Modified uc.  Is correct uc except for three non-ascii chars which are
1242  * all mapped to one of them, and these need special handling; returns the
1243  * input itself if is out-of-range */
1244 #define toUPPER_LATIN1_MOD(c) ((! FITS_IN_8_BITS(c))                       \
1245                                ? (c)                                       \
1246                                : PL_mod_latin1_uc[ (U8) (c) ])
1247 #define IN_UTF8_CTYPE_LOCALE PL_in_utf8_CTYPE_locale
1248 
1249 /* Use foo_LC_uvchr() instead  of these for beyond the Latin1 range */
1250 
1251 /* For internal core Perl use only: the base macro for defining macros like
1252  * isALPHA_LC, which uses the current LC_CTYPE locale.  'c' is the code point
1253  * (0-255) to check.  In a UTF-8 locale, the result is the same as calling
1254  * isFOO_L1(); the 'utf8_locale_classnum' parameter is something like
1255  * _CC_UPPER, which gives the class number for doing this.  For non-UTF-8
1256  * locales, the code to actually do the test this is passed in 'non_utf8'.  If
1257  * 'c' is above 255, 0 is returned.  For accessing the full range of possible
1258  * code points under locale rules, use the macros based on _generic_LC_uvchr
1259  * instead of this. */
1260 #define _generic_LC_base(c, utf8_locale_classnum, non_utf8)                    \
1261            (! FITS_IN_8_BITS(c)                                                \
1262            ? 0                                                                 \
1263            : IN_UTF8_CTYPE_LOCALE                                              \
1264              ? cBOOL(PL_charclass[(U8) (c)] & _CC_mask(utf8_locale_classnum))  \
1265              : cBOOL(non_utf8))
1266 
1267 /* For internal core Perl use only: a helper macro for defining macros like
1268  * isALPHA_LC.  'c' is the code point (0-255) to check.  The function name to
1269  * actually do this test is passed in 'non_utf8_func', which is called on 'c',
1270  * casting 'c' to the macro _LC_CAST, which should not be parenthesized.  See
1271  * _generic_LC_base for more info */
1272 #define _generic_LC(c, utf8_locale_classnum, non_utf8_func)                    \
1273                         _generic_LC_base(c,utf8_locale_classnum,               \
1274                                          non_utf8_func( (_LC_CAST) (c)))
1275 
1276 /* For internal core Perl use only: like _generic_LC, but also returns TRUE if
1277  * 'c' is the platform's native underscore character */
1278 #define _generic_LC_underscore(c,utf8_locale_classnum,non_utf8_func)           \
1279                         _generic_LC_base(c, utf8_locale_classnum,              \
1280                                          (non_utf8_func( (_LC_CAST) (c))       \
1281                                           || (char)(c) == '_'))
1282 
1283 /* These next three are also for internal core Perl use only: case-change
1284  * helper macros */
1285 #define _generic_toLOWER_LC(c, function, cast)  (! FITS_IN_8_BITS(c)           \
1286                                                 ? (c)                          \
1287                                                 : (IN_UTF8_CTYPE_LOCALE)       \
1288                                                   ? PL_latin1_lc[ (U8) (c) ]   \
1289                                                 : function((cast)(c)))
1290 
1291 /* Note that the result can be larger than a byte in a UTF-8 locale.  It
1292  * returns a single value, so can't adequately return the upper case of LATIN
1293  * SMALL LETTER SHARP S in a UTF-8 locale (which should be a string of two
1294  * values "SS");  instead it asserts against that under DEBUGGING, and
1295  * otherwise returns its input */
1296 #define _generic_toUPPER_LC(c, function, cast)                                 \
1297                     (! FITS_IN_8_BITS(c)                                       \
1298                     ? (c)                                                      \
1299                     : ((! IN_UTF8_CTYPE_LOCALE)                                \
1300                       ? function((cast)(c))                                    \
1301                       : ((((U8)(c)) == MICRO_SIGN)                             \
1302                         ? GREEK_CAPITAL_LETTER_MU                              \
1303                         : ((((U8)(c)) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)  \
1304                           ? LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS              \
1305                           : ((((U8)(c)) == LATIN_SMALL_LETTER_SHARP_S)         \
1306                             ? (__ASSERT_(0) (c))                               \
1307                             : PL_mod_latin1_uc[ (U8) (c) ])))))
1308 
1309 /* Note that the result can be larger than a byte in a UTF-8 locale.  It
1310  * returns a single value, so can't adequately return the fold case of LATIN
1311  * SMALL LETTER SHARP S in a UTF-8 locale (which should be a string of two
1312  * values "ss"); instead it asserts against that under DEBUGGING, and
1313  * otherwise returns its input */
1314 #define _generic_toFOLD_LC(c, function, cast)                                  \
1315                     ((UNLIKELY((c) == MICRO_SIGN) && IN_UTF8_CTYPE_LOCALE)     \
1316                       ? GREEK_SMALL_LETTER_MU                                  \
1317                       : (__ASSERT_(! IN_UTF8_CTYPE_LOCALE                      \
1318                                    || (c) != LATIN_SMALL_LETTER_SHARP_S)       \
1319                          _generic_toLOWER_LC(c, function, cast)))
1320 
1321 /* Use the libc versions for these if available. */
1322 #if defined(HAS_ISASCII) && ! defined(USE_NEXT_CTYPE)
1323 #   define isASCII_LC(c) (FITS_IN_8_BITS(c) && isascii( (U8) (c)))
1324 #else
1325 #   define isASCII_LC(c) isASCII(c)
1326 #endif
1327 
1328 #if defined(HAS_ISBLANK) && ! defined(USE_NEXT_CTYPE)
1329 #   define isBLANK_LC(c) _generic_LC(c, _CC_BLANK, isblank)
1330 #else /* Unlike isASCII, varies if in a UTF-8 locale */
1331 #   define isBLANK_LC(c) (IN_UTF8_CTYPE_LOCALE) ? isBLANK_L1(c) : isBLANK(c)
1332 #endif
1333 
1334 #ifdef USE_NEXT_CTYPE   /* NeXT computers */
1335 
1336 #    define _LC_CAST unsigned int   /* Needed by _generic_LC.  NeXT functions
1337                                        use this as their input type */
1338 
1339 #    define isALPHA_LC(c)   _generic_LC(c, _CC_ALPHA, NXIsAlpha)
1340 #    define isALPHANUMERIC_LC(c)  _generic_LC(c, _CC_ALPHANUMERIC, NXIsAlNum)
1341 #    define isCNTRL_LC(c)    _generic_LC(c, _CC_CNTRL, NXIsCntrl)
1342 #    define isDIGIT_LC(c)    _generic_LC(c, _CC_DIGIT, NXIsDigit)
1343 #    define isGRAPH_LC(c)    _generic_LC(c, _CC_GRAPH, NXIsGraph)
1344 #    define isIDFIRST_LC(c)  _generic_LC_underscore(c, _CC_IDFIRST, NXIsAlpha)
1345 #    define isLOWER_LC(c)    _generic_LC(c, _CC_LOWER, NXIsLower)
1346 #    define isPRINT_LC(c)    _generic_LC(c, _CC_PRINT, NXIsPrint)
1347 #    define isPUNCT_LC(c)    _generic_LC(c, _CC_PUNCT, NXIsPunct)
1348 #    define isSPACE_LC(c)    _generic_LC(c, _CC_SPACE, NXIsSpace)
1349 #    define isUPPER_LC(c)    _generic_LC(c, _CC_UPPER, NXIsUpper)
1350 #    define isWORDCHAR_LC(c) _generic_LC_underscore(c, _CC_WORDCHAR, NXIsAlNum)
1351 #    define isXDIGIT_LC(c)   _generic_LC(c, _CC_XDIGIT, NXIsXdigit)
1352 
1353 #    define toLOWER_LC(c) _generic_toLOWER_LC((c), NXToLower, unsigned int)
1354 #    define toUPPER_LC(c) _generic_toUPPER_LC((c), NXToUpper, unsigned int)
1355 #    define toFOLD_LC(c)  _generic_toFOLD_LC((c), NXToLower, unsigned int)
1356 
1357 #else /* !USE_NEXT_CTYPE */
1358 
1359 #  define _LC_CAST U8
1360 
1361 #  if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
1362     /* For most other platforms */
1363 
1364 #    define isALPHA_LC(c)   _generic_LC(c, _CC_ALPHA, isalpha)
1365 #    define isALPHANUMERIC_LC(c)  _generic_LC(c, _CC_ALPHANUMERIC, isalnum)
1366 #    define isCNTRL_LC(c)    _generic_LC(c, _CC_CNTRL, iscntrl)
1367 #    define isDIGIT_LC(c)    _generic_LC(c, _CC_DIGIT, isdigit)
1368 #    define isGRAPH_LC(c)    _generic_LC(c, _CC_GRAPH, isgraph)
1369 #    define isIDFIRST_LC(c)  _generic_LC_underscore(c, _CC_IDFIRST, isalpha)
1370 #    define isLOWER_LC(c)    _generic_LC(c, _CC_LOWER, islower)
1371 #    define isPRINT_LC(c)    _generic_LC(c, _CC_PRINT, isprint)
1372 #    define isPUNCT_LC(c)    _generic_LC(c, _CC_PUNCT, ispunct)
1373 #    define isSPACE_LC(c)    _generic_LC(c, _CC_SPACE, isspace)
1374 #    define isUPPER_LC(c)    _generic_LC(c, _CC_UPPER, isupper)
1375 #    define isWORDCHAR_LC(c) _generic_LC_underscore(c, _CC_WORDCHAR, isalnum)
1376 #    define isXDIGIT_LC(c)   _generic_LC(c, _CC_XDIGIT, isxdigit)
1377 
1378 
1379 #    define toLOWER_LC(c) _generic_toLOWER_LC((c), tolower, U8)
1380 #    define toUPPER_LC(c) _generic_toUPPER_LC((c), toupper, U8)
1381 #    define toFOLD_LC(c)  _generic_toFOLD_LC((c), tolower, U8)
1382 
1383 #  else  /* The final fallback position */
1384 
1385 #    define isALPHA_LC(c)	(isascii(c) && isalpha(c))
1386 #    define isALPHANUMERIC_LC(c) (isascii(c) && isalnum(c))
1387 #    define isCNTRL_LC(c)	(isascii(c) && iscntrl(c))
1388 #    define isDIGIT_LC(c)	(isascii(c) && isdigit(c))
1389 #    define isGRAPH_LC(c)	(isascii(c) && isgraph(c))
1390 #    define isIDFIRST_LC(c)	(isascii(c) && (isalpha(c) || (c) == '_'))
1391 #    define isLOWER_LC(c)	(isascii(c) && islower(c))
1392 #    define isPRINT_LC(c)	(isascii(c) && isprint(c))
1393 #    define isPUNCT_LC(c)	(isascii(c) && ispunct(c))
1394 #    define isSPACE_LC(c)	(isascii(c) && isspace(c))
1395 #    define isUPPER_LC(c)	(isascii(c) && isupper(c))
1396 #    define isWORDCHAR_LC(c)	(isascii(c) && (isalnum(c) || (c) == '_'))
1397 #    define isXDIGIT_LC(c)      (isascii(c) && isxdigit(c))
1398 
1399 #    define toLOWER_LC(c)	(isascii(c) ? tolower(c) : (c))
1400 #    define toUPPER_LC(c)	(isascii(c) ? toupper(c) : (c))
1401 #    define toFOLD_LC(c)	(isascii(c) ? tolower(c) : (c))
1402 
1403 #  endif
1404 #endif /* USE_NEXT_CTYPE */
1405 
1406 #define isIDCONT(c)             isWORDCHAR(c)
1407 #define isIDCONT_A(c)           isWORDCHAR_A(c)
1408 #define isIDCONT_L1(c)	        isWORDCHAR_L1(c)
1409 #define isIDCONT_LC(c)	        isWORDCHAR_LC(c)
1410 #define isPSXSPC_LC(c)		isSPACE_LC(c)
1411 
1412 /* For internal core Perl use only: the base macros for defining macros like
1413  * isALPHA_uni.  'c' is the code point to check.  'classnum' is the POSIX class
1414  * number defined earlier in this file.  _generic_uni() is used for POSIX
1415  * classes where there is a macro or function 'above_latin1' that takes the
1416  * single argument 'c' and returns the desired value.  These exist for those
1417  * classes which have simple definitions, avoiding the overhead of a hash
1418  * lookup or inversion list binary search.  _generic_swash_uni() can be used
1419  * for classes where that overhead is faster than a direct lookup.
1420  * _generic_uni() won't compile if 'c' isn't unsigned, as it won't match the
1421  * 'above_latin1' prototype. _generic_isCC() macro does bounds checking, so
1422  * have duplicate checks here, so could create versions of the macros that
1423  * don't, but experiments show that gcc optimizes them out anyway. */
1424 
1425 /* Note that all ignore 'use bytes' */
1426 #define _generic_uni(classnum, above_latin1, c) ((c) < 256                    \
1427                                              ? _generic_isCC(c, classnum)     \
1428                                              : above_latin1(c))
1429 #define _generic_swash_uni(classnum, c) ((c) < 256                            \
1430                                              ? _generic_isCC(c, classnum)     \
1431                                              : _is_uni_FOO(classnum, c))
1432 #define isALPHA_uni(c)      _generic_swash_uni(_CC_ALPHA, c)
1433 #define isALPHANUMERIC_uni(c) _generic_swash_uni(_CC_ALPHANUMERIC, c)
1434 #define isASCII_uni(c)      isASCII(c)
1435 #define isBLANK_uni(c)      _generic_uni(_CC_BLANK, is_HORIZWS_cp_high, c)
1436 #define isCNTRL_uni(c)      isCNTRL_L1(c) /* All controls are in Latin1 */
1437 #define isDIGIT_uni(c)      _generic_swash_uni(_CC_DIGIT, c)
1438 #define isGRAPH_uni(c)      _generic_swash_uni(_CC_GRAPH, c)
1439 #define isIDCONT_uni(c)     _generic_uni(_CC_WORDCHAR, _is_uni_perl_idcont, c)
1440 #define isIDFIRST_uni(c)    _generic_uni(_CC_IDFIRST, _is_uni_perl_idstart, c)
1441 #define isLOWER_uni(c)      _generic_swash_uni(_CC_LOWER, c)
1442 #define isPRINT_uni(c)      _generic_swash_uni(_CC_PRINT, c)
1443 
1444 /* Posix and regular space are identical above Latin1 */
1445 #define isPSXSPC_uni(c)     _generic_uni(_CC_PSXSPC, is_XPERLSPACE_cp_high, c)
1446 
1447 #define isPUNCT_uni(c)      _generic_swash_uni(_CC_PUNCT, c)
1448 #define isSPACE_uni(c)      _generic_uni(_CC_SPACE, is_XPERLSPACE_cp_high, c)
1449 #define isUPPER_uni(c)      _generic_swash_uni(_CC_UPPER, c)
1450 #define isVERTWS_uni(c)     _generic_uni(_CC_VERTSPACE, is_VERTWS_cp_high, c)
1451 #define isWORDCHAR_uni(c)   _generic_swash_uni(_CC_WORDCHAR, c)
1452 #define isXDIGIT_uni(c)     _generic_uni(_CC_XDIGIT, is_XDIGIT_cp_high, c)
1453 
1454 #define toFOLD_uni(c,s,l)	to_uni_fold(c,s,l)
1455 #define toLOWER_uni(c,s,l)	to_uni_lower(c,s,l)
1456 #define toTITLE_uni(c,s,l)	to_uni_title(c,s,l)
1457 #define toUPPER_uni(c,s,l)	to_uni_upper(c,s,l)
1458 
1459 /* For internal core Perl use only: the base macros for defining macros like
1460  * isALPHA_LC_uvchr.  These are like isALPHA_LC, but the input can be any code
1461  * point, not just 0-255.  Like _generic_uni, there are two versions, one for
1462  * simple class definitions; the other for more complex.  These are like
1463  * _generic_uni, so see it for more info. */
1464 #define _generic_LC_uvchr(latin1, above_latin1, c)                            \
1465                                     (c < 256 ? latin1(c) : above_latin1(c))
1466 #define _generic_LC_swash_uvchr(latin1, classnum, c)                          \
1467                             (c < 256 ? latin1(c) : _is_uni_FOO(classnum, c))
1468 
1469 #define isALPHA_LC_uvchr(c)  _generic_LC_swash_uvchr(isALPHA_LC, _CC_ALPHA, c)
1470 #define isALPHANUMERIC_LC_uvchr(c)  _generic_LC_swash_uvchr(isALPHANUMERIC_LC, \
1471                                                          _CC_ALPHANUMERIC, c)
1472 #define isASCII_LC_uvchr(c)  isASCII_LC(c)
1473 #define isBLANK_LC_uvchr(c)  _generic_LC_uvchr(isBLANK_LC, is_HORIZWS_cp_high, c)
1474 #define isCNTRL_LC_uvchr(c)  (c < 256 ? isCNTRL_LC(c) : 0)
1475 #define isDIGIT_LC_uvchr(c)  _generic_LC_swash_uvchr(isDIGIT_LC, _CC_DIGIT, c)
1476 #define isGRAPH_LC_uvchr(c)  _generic_LC_swash_uvchr(isGRAPH_LC, _CC_GRAPH, c)
1477 #define isIDCONT_LC_uvchr(c)  _generic_LC_uvchr(isIDCONT_LC,                  \
1478                                                   _is_uni_perl_idcont, c)
1479 #define isIDFIRST_LC_uvchr(c)  _generic_LC_uvchr(isIDFIRST_LC,                 \
1480                                                   _is_uni_perl_idstart, c)
1481 #define isLOWER_LC_uvchr(c)  _generic_LC_swash_uvchr(isLOWER_LC, _CC_LOWER, c)
1482 #define isPRINT_LC_uvchr(c)  _generic_LC_swash_uvchr(isPRINT_LC, _CC_PRINT, c)
1483 #define isPSXSPC_LC_uvchr(c) isSPACE_LC_uvchr(c) /* space is identical to posix
1484                                                     space under locale */
1485 #define isPUNCT_LC_uvchr(c)  _generic_LC_swash_uvchr(isPUNCT_LC, _CC_PUNCT, c)
1486 #define isSPACE_LC_uvchr(c)  _generic_LC_uvchr(isSPACE_LC,                     \
1487                                                     is_XPERLSPACE_cp_high, c)
1488 #define isUPPER_LC_uvchr(c)  _generic_LC_swash_uvchr(isUPPER_LC, _CC_UPPER, c)
1489 #define isWORDCHAR_LC_uvchr(c)  _generic_LC_swash_uvchr(isWORDCHAR_LC,              \
1490                                                            _CC_WORDCHAR, c)
1491 #define isXDIGIT_LC_uvchr(c) _generic_LC_uvchr(isXDIGIT_LC, is_XDIGIT_cp_high, c)
1492 
1493 #define isBLANK_LC_uni(c)	isBLANK_LC_uvchr(UNI_TO_NATIVE(c))
1494 
1495 /* For internal core Perl use only: the base macros for defining macros like
1496  * isALPHA_utf8.  These are like the earlier defined macros, but take an input
1497  * UTF-8 encoded string 'p'. If the input is in the Latin1 range, use
1498  * the Latin1 macro 'classnum' on 'p'.  Otherwise use the value given by the
1499  * 'utf8' parameter.  This relies on the fact that ASCII characters have the
1500  * same representation whether utf8 or not.  Note that it assumes that the utf8
1501  * has been validated, and ignores 'use bytes' */
1502 #define _generic_utf8(classnum, p, utf8) (UTF8_IS_INVARIANT(*(p))              \
1503                                          ? _generic_isCC(*(p), classnum)       \
1504                                          : (UTF8_IS_DOWNGRADEABLE_START(*(p))) \
1505                                            ? _generic_isCC(                    \
1506                                                 TWO_BYTE_UTF8_TO_NATIVE(*(p),  \
1507                                                                    *((p)+1 )), \
1508                                                 classnum)                      \
1509                                            : utf8)
1510 /* Like the above, but calls 'above_latin1(p)' to get the utf8 value.  'above_latin1'
1511  * can be a macro */
1512 #define _generic_func_utf8(classnum, above_latin1, p)  \
1513                                     _generic_utf8(classnum, p, above_latin1(p))
1514 /* Like the above, but passes classnum to _isFOO_utf8(), instead of having an
1515  * 'above_latin1' parameter */
1516 #define _generic_swash_utf8(classnum, p)  \
1517                       _generic_utf8(classnum, p, _is_utf8_FOO(classnum, p))
1518 
1519 /* Like the above, but should be used only when it is known that there are no
1520  * characters in the range 128-255 which the class is TRUE for.  Hence it can
1521  * skip the tests for this range.  'above_latin1' should include its arguments */
1522 #define _generic_utf8_no_upper_latin1(classnum, p, above_latin1)               \
1523                                          (UTF8_IS_INVARIANT(*(p))              \
1524                                          ? _generic_isCC(*(p), classnum)       \
1525                                          : (UTF8_IS_ABOVE_LATIN1(*(p)))        \
1526                                            ? above_latin1                      \
1527                                            : 0)
1528 
1529 /* NOTE that some of these macros have very similar ones in regcharclass.h.
1530  * For example, there is (at the time of this writing) an 'is_SPACE_utf8()'
1531  * there, differing in name only by an underscore from the one here
1532  * 'isSPACE_utf8().  The difference is that the ones here are probably more
1533  * efficient and smaller, using an O(1) array lookup for Latin1-range code
1534  * points; the regcharclass.h ones are implemented as a series of
1535  * "if-else-if-else ..." */
1536 
1537 #define isALPHA_utf8(p)         _generic_swash_utf8(_CC_ALPHA, p)
1538 #define isALPHANUMERIC_utf8(p)  _generic_swash_utf8(_CC_ALPHANUMERIC, p)
1539 #define isASCII_utf8(p)         isASCII(*p) /* Because ASCII is invariant under
1540                                                utf8, the non-utf8 macro works
1541                                              */
1542 #define isBLANK_utf8(p)         _generic_func_utf8(_CC_BLANK, is_HORIZWS_high, p)
1543 #define isCNTRL_utf8(p)         _generic_utf8(_CC_CNTRL, p, 0)
1544 #define isDIGIT_utf8(p)         _generic_utf8_no_upper_latin1(_CC_DIGIT, p,   \
1545                                                   _is_utf8_FOO(_CC_DIGIT, p))
1546 #define isGRAPH_utf8(p)         _generic_swash_utf8(_CC_GRAPH, p)
1547 #define isIDCONT_utf8(p)        _generic_func_utf8(_CC_WORDCHAR,              \
1548                                                   _is_utf8_perl_idcont, p)
1549 
1550 /* To prevent S_scan_word in toke.c from hanging, we have to make sure that
1551  * IDFIRST is an alnum.  See
1552  * http://rt.perl.org/rt3/Ticket/Display.html?id=74022 for more detail than you
1553  * ever wanted to know about.  (In the ASCII range, there isn't a difference.)
1554  * This used to be not the XID version, but we decided to go with the more
1555  * modern Unicode definition */
1556 #define isIDFIRST_utf8(p)       _generic_func_utf8(_CC_IDFIRST,               \
1557                                                 _is_utf8_perl_idstart, p)
1558 
1559 #define isLOWER_utf8(p)         _generic_swash_utf8(_CC_LOWER, p)
1560 #define isPRINT_utf8(p)         _generic_swash_utf8(_CC_PRINT, p)
1561 
1562 /* Posix and regular space are identical above Latin1 */
1563 #define isPSXSPC_utf8(p)        _generic_func_utf8(_CC_PSXSPC, is_XPERLSPACE_high, p)
1564 
1565 #define isPUNCT_utf8(p)         _generic_swash_utf8(_CC_PUNCT, p)
1566 #define isSPACE_utf8(p)         _generic_func_utf8(_CC_SPACE, is_XPERLSPACE_high, p)
1567 #define isUPPER_utf8(p)         _generic_swash_utf8(_CC_UPPER, p)
1568 #define isVERTWS_utf8(p)        _generic_func_utf8(_CC_VERTSPACE, is_VERTWS_high, p)
1569 #define isWORDCHAR_utf8(p)      _generic_swash_utf8(_CC_WORDCHAR, p)
1570 #define isXDIGIT_utf8(p)        _generic_utf8_no_upper_latin1(_CC_XDIGIT, p,   \
1571                                                           is_XDIGIT_high(p))
1572 
1573 #define toFOLD_utf8(p,s,l)	to_utf8_fold(p,s,l)
1574 #define toLOWER_utf8(p,s,l)	to_utf8_lower(p,s,l)
1575 #define toTITLE_utf8(p,s,l)	to_utf8_title(p,s,l)
1576 #define toUPPER_utf8(p,s,l)	to_utf8_upper(p,s,l)
1577 
1578 /* For internal core Perl use only: the base macros for defining macros like
1579  * isALPHA_LC_utf8.  These are like _generic_utf8, but if the first code point
1580  * in 'p' is within the 0-255 range, it uses locale rules from the passed-in
1581  * 'macro' parameter */
1582 #define _generic_LC_utf8(macro, p, utf8)                                    \
1583                          (UTF8_IS_INVARIANT(*(p))                           \
1584                          ? macro(*(p))                                      \
1585                          : (UTF8_IS_DOWNGRADEABLE_START(*(p)))              \
1586                            ? macro(TWO_BYTE_UTF8_TO_NATIVE(*(p), *((p)+1))) \
1587                            : utf8)
1588 
1589 #define _generic_LC_swash_utf8(macro, classnum, p)                         \
1590                     _generic_LC_utf8(macro, p, _is_utf8_FOO(classnum, p))
1591 #define _generic_LC_func_utf8(macro, above_latin1, p)                         \
1592                               _generic_LC_utf8(macro, p, above_latin1(p))
1593 
1594 #define isALPHANUMERIC_LC_utf8(p)  _generic_LC_swash_utf8(isALPHANUMERIC_LC,  \
1595                                                       _CC_ALPHANUMERIC, p)
1596 #define isALPHA_LC_utf8(p)   _generic_LC_swash_utf8(isALPHA_LC, _CC_ALPHA, p)
1597 #define isASCII_LC_utf8(p)   isASCII_LC(*p)
1598 #define isBLANK_LC_utf8(p)   _generic_LC_func_utf8(isBLANK_LC, is_HORIZWS_high, p)
1599 #define isCNTRL_LC_utf8(p)   _generic_LC_utf8(isCNTRL_LC, p, 0)
1600 #define isDIGIT_LC_utf8(p)   _generic_LC_swash_utf8(isDIGIT_LC, _CC_DIGIT, p)
1601 #define isGRAPH_LC_utf8(p)   _generic_LC_swash_utf8(isGRAPH_LC, _CC_GRAPH, p)
1602 #define isIDCONT_LC_utf8(p) _generic_LC_func_utf8(isIDCONT_LC, _is_utf8_perl_idcont, p)
1603 #define isIDFIRST_LC_utf8(p) _generic_LC_func_utf8(isIDFIRST_LC, _is_utf8_perl_idstart, p)
1604 #define isLOWER_LC_utf8(p)   _generic_LC_swash_utf8(isLOWER_LC, _CC_LOWER, p)
1605 #define isPRINT_LC_utf8(p)   _generic_LC_swash_utf8(isPRINT_LC, _CC_PRINT, p)
1606 #define isPSXSPC_LC_utf8(p)  isSPACE_LC_utf8(p) /* space is identical to posix
1607                                                    space under locale */
1608 #define isPUNCT_LC_utf8(p)   _generic_LC_swash_utf8(isPUNCT_LC, _CC_PUNCT, p)
1609 #define isSPACE_LC_utf8(p)   _generic_LC_func_utf8(isSPACE_LC, is_XPERLSPACE_high, p)
1610 #define isUPPER_LC_utf8(p)   _generic_LC_swash_utf8(isUPPER_LC, _CC_UPPER, p)
1611 #define isWORDCHAR_LC_utf8(p) _generic_LC_swash_utf8(isWORDCHAR_LC,           \
1612                                                             _CC_WORDCHAR, p)
1613 #define isXDIGIT_LC_utf8(p)  _generic_LC_func_utf8(isXDIGIT_LC, is_XDIGIT_high, p)
1614 
1615 /* Macros for backwards compatibility and for completeness when the ASCII and
1616  * Latin1 values are identical */
1617 #define isALPHAU(c)     isALPHA_L1(c)
1618 #define isDIGIT_L1(c)   isDIGIT_A(c)
1619 #define isOCTAL(c)      isOCTAL_A(c)
1620 #define isOCTAL_L1(c)   isOCTAL_A(c)
1621 #define isXDIGIT_L1(c)  isXDIGIT_A(c)
1622 #define isALNUM(c)      isWORDCHAR(c)
1623 #define isALNUMU(c)     isWORDCHAR_L1(c)
1624 #define isALNUM_LC(c)   isWORDCHAR_LC(c)
1625 #define isALNUM_uni(c)  isWORDCHAR_uni(c)
1626 #define isALNUM_LC_uvchr(c) isWORDCHAR_LC_uvchr(c)
1627 #define isALNUM_utf8(p) isWORDCHAR_utf8(p)
1628 #define isALNUM_LC_utf8(p) isWORDCHAR_LC_utf8(p)
1629 #define isALNUMC_A(c)   isALPHANUMERIC_A(c)      /* Mnemonic: "C's alnum" */
1630 #define isALNUMC_L1(c)  isALPHANUMERIC_L1(c)
1631 #define isALNUMC(c)	isALPHANUMERIC(c)
1632 #define isALNUMC_LC(c)	isALPHANUMERIC_LC(c)
1633 #define isALNUMC_uni(c) isALPHANUMERIC_uni(c)
1634 #define isALNUMC_LC_uvchr(c) isALPHANUMERIC_LC_uvchr(c)
1635 #define isALNUMC_utf8(p) isALPHANUMERIC_utf8(p)
1636 #define isALNUMC_LC_utf8(p) isALPHANUMERIC_LC_utf8(p)
1637 
1638 /* On EBCDIC platforms, CTRL-@ is 0, CTRL-A is 1, etc, just like on ASCII,
1639  * except that they don't necessarily mean the same characters, e.g. CTRL-D is
1640  * 4 on both systems, but that is EOT on ASCII;  ST on EBCDIC.
1641  * '?' is special-cased on EBCDIC to APC, which is the control there that is
1642  * the outlier from the block that contains the other controls, just like
1643  * toCTRL('?') on ASCII yields DEL, the control that is the outlier from the C0
1644  * block.  If it weren't special cased, it would yield a non-control.
1645  * The conversion works both ways, so CTRL('D') is 4, and CTRL(4) is D, etc. */
1646 #ifndef EBCDIC
1647 #  define toCTRL(c)    (toUPPER(c) ^ 64)
1648 #else
1649 #  define toCTRL(c)    ((c) == '?'                               \
1650                         ? LATIN1_TO_NATIVE(0x9F)                 \
1651                         : (c) == LATIN1_TO_NATIVE(0x9F)          \
1652                           ? '?'                                  \
1653                           : (NATIVE_TO_LATIN1(toUPPER(c)) ^ 64))
1654 #endif
1655 
1656 /* Line numbers are unsigned, 32 bits. */
1657 typedef U32 line_t;
1658 #define NOLINE ((line_t) 4294967295UL)  /* = FFFFFFFF */
1659 
1660 /* Helpful alias for version prescan */
1661 #define is_LAX_VERSION(a,b) \
1662 	(a != Perl_prescan_version(aTHX_ a, FALSE, b, NULL, NULL, NULL, NULL))
1663 
1664 #define is_STRICT_VERSION(a,b) \
1665 	(a != Perl_prescan_version(aTHX_ a, TRUE, b, NULL, NULL, NULL, NULL))
1666 
1667 #define BADVERSION(a,b,c) \
1668 	if (b) { \
1669 	    *b = c; \
1670 	} \
1671 	return a;
1672 
1673 /* Converts a character known to represent a hexadecimal digit (0-9, A-F, or
1674  * a-f) to its numeric value.  READ_XDIGIT's argument is a string pointer,
1675  * which is advanced.  The input is validated only by an assert() in DEBUGGING
1676  * builds.  In both ASCII and EBCDIC the last 4 bits of the digits are 0-9; and
1677  * the last 4 bits of A-F and a-f are 1-6, so adding 9 yields 10-15 */
1678 #define XDIGIT_VALUE(c) (__ASSERT_(isXDIGIT(c)) (0xf & (isDIGIT(c)        \
1679                                                         ? (c)             \
1680                                                         : ((c) + 9))))
1681 #define READ_XDIGIT(s)  (__ASSERT_(isXDIGIT(*s)) (0xf & (isDIGIT(*(s))     \
1682                                                         ? (*(s)++)         \
1683                                                         : (*(s)++ + 9))))
1684 
1685 /* Converts a character known to represent an octal digit (0-7) to its numeric
1686  * value.  The input is validated only by an assert() in DEBUGGING builds.  In
1687  * both ASCII and EBCDIC the last 3 bits of the octal digits range from 0-7. */
1688 #define OCTAL_VALUE(c) (__ASSERT_(isOCTAL(c)) (7 & (c)))
1689 
1690 /*
1691 =head1 Memory Management
1692 
1693 =for apidoc Am|void|Newx|void* ptr|int nitems|type
1694 The XSUB-writer's interface to the C C<malloc> function.
1695 
1696 Memory obtained by this should B<ONLY> be freed with L<"Safefree">.
1697 
1698 In 5.9.3, Newx() and friends replace the older New() API, and drops
1699 the first parameter, I<x>, a debug aid which allowed callers to identify
1700 themselves.  This aid has been superseded by a new build option,
1701 PERL_MEM_LOG (see L<perlhacktips/PERL_MEM_LOG>).  The older API is still
1702 there for use in XS modules supporting older perls.
1703 
1704 =for apidoc Am|void|Newxc|void* ptr|int nitems|type|cast
1705 The XSUB-writer's interface to the C C<malloc> function, with
1706 cast.  See also C<Newx>.
1707 
1708 Memory obtained by this should B<ONLY> be freed with L<"Safefree">.
1709 
1710 =for apidoc Am|void|Newxz|void* ptr|int nitems|type
1711 The XSUB-writer's interface to the C C<malloc> function.  The allocated
1712 memory is zeroed with C<memzero>.  See also C<Newx>.
1713 
1714 Memory obtained by this should B<ONLY> be freed with L<"Safefree">.
1715 
1716 =for apidoc Am|void|Renew|void* ptr|int nitems|type
1717 The XSUB-writer's interface to the C C<realloc> function.
1718 
1719 Memory obtained by this should B<ONLY> be freed with L<"Safefree">.
1720 
1721 =for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
1722 The XSUB-writer's interface to the C C<realloc> function, with
1723 cast.
1724 
1725 Memory obtained by this should B<ONLY> be freed with L<"Safefree">.
1726 
1727 =for apidoc Am|void|Safefree|void* ptr
1728 The XSUB-writer's interface to the C C<free> function.
1729 
1730 This should B<ONLY> be used on memory obtained using L<"Newx"> and friends.
1731 
1732 =for apidoc Am|void|Move|void* src|void* dest|int nitems|type
1733 The XSUB-writer's interface to the C C<memmove> function.  The C<src> is the
1734 source, C<dest> is the destination, C<nitems> is the number of items, and
1735 C<type> is the type.  Can do overlapping moves.  See also C<Copy>.
1736 
1737 =for apidoc Am|void *|MoveD|void* src|void* dest|int nitems|type
1738 Like C<Move> but returns dest.  Useful
1739 for encouraging compilers to tail-call
1740 optimise.
1741 
1742 =for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
1743 The XSUB-writer's interface to the C C<memcpy> function.  The C<src> is the
1744 source, C<dest> is the destination, C<nitems> is the number of items, and
1745 C<type> is the type.  May fail on overlapping copies.  See also C<Move>.
1746 
1747 =for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
1748 
1749 Like C<Copy> but returns dest.  Useful
1750 for encouraging compilers to tail-call
1751 optimise.
1752 
1753 =for apidoc Am|void|Zero|void* dest|int nitems|type
1754 
1755 The XSUB-writer's interface to the C C<memzero> function.  The C<dest> is the
1756 destination, C<nitems> is the number of items, and C<type> is the type.
1757 
1758 =for apidoc Am|void *|ZeroD|void* dest|int nitems|type
1759 
1760 Like C<Zero> but returns dest.  Useful
1761 for encouraging compilers to tail-call
1762 optimise.
1763 
1764 =for apidoc Am|void|StructCopy|type *src|type *dest|type
1765 This is an architecture-independent macro to copy one structure to another.
1766 
1767 =for apidoc Am|void|PoisonWith|void* dest|int nitems|type|U8 byte
1768 
1769 Fill up memory with a byte pattern (a byte repeated over and over
1770 again) that hopefully catches attempts to access uninitialized memory.
1771 
1772 =for apidoc Am|void|PoisonNew|void* dest|int nitems|type
1773 
1774 PoisonWith(0xAB) for catching access to allocated but uninitialized memory.
1775 
1776 =for apidoc Am|void|PoisonFree|void* dest|int nitems|type
1777 
1778 PoisonWith(0xEF) for catching access to freed memory.
1779 
1780 =for apidoc Am|void|Poison|void* dest|int nitems|type
1781 
1782 PoisonWith(0xEF) for catching access to freed memory.
1783 
1784 =cut */
1785 
1786 /* Maintained for backwards-compatibility only. Use newSV() instead. */
1787 #ifndef PERL_CORE
1788 #define NEWSV(x,len)	newSV(len)
1789 #endif
1790 
1791 #define MEM_SIZE_MAX ((MEM_SIZE)~0)
1792 
1793 /* The +0.0 in MEM_WRAP_CHECK_ is an attempt to foil
1794  * overly eager compilers that will bleat about e.g.
1795  * (U16)n > (size_t)~0/sizeof(U16) always being false. */
1796 #ifdef PERL_MALLOC_WRAP
1797 #define MEM_WRAP_CHECK(n,t) \
1798 	(void)(UNLIKELY(sizeof(t) > 1 && ((MEM_SIZE)(n)+0.0) > MEM_SIZE_MAX/sizeof(t)) && (croak_memory_wrap(),0))
1799 #define MEM_WRAP_CHECK_1(n,t,a) \
1800 	(void)(UNLIKELY(sizeof(t) > 1 && ((MEM_SIZE)(n)+0.0) > MEM_SIZE_MAX/sizeof(t)) && (Perl_croak_nocontext("%s",(a)),0))
1801 #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
1802 
1803 #define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > MEM_SIZE_MAX - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (croak_memory_wrap(),0):0),((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
1804 #else
1805 
1806 #define MEM_WRAP_CHECK(n,t)
1807 #define MEM_WRAP_CHECK_1(n,t,a)
1808 #define MEM_WRAP_CHECK_2(n,t,a,b)
1809 #define MEM_WRAP_CHECK_(n,t)
1810 
1811 #define PERL_STRLEN_ROUNDUP(n) (((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
1812 
1813 #endif
1814 
1815 #ifdef PERL_MEM_LOG
1816 /*
1817  * If PERL_MEM_LOG is defined, all Newx()s, Renew()s, and Safefree()s
1818  * go through functions, which are handy for debugging breakpoints, but
1819  * which more importantly get the immediate calling environment (file and
1820  * line number, and C function name if available) passed in.  This info can
1821  * then be used for logging the calls, for which one gets a sample
1822  * implementation unless -DPERL_MEM_LOG_NOIMPL is also defined.
1823  *
1824  * Known problems:
1825  * - not all memory allocs get logged, only those
1826  *   that go through Newx() and derivatives (while all
1827  *   Safefrees do get logged)
1828  * - __FILE__ and __LINE__ do not work everywhere
1829  * - __func__ or __FUNCTION__ even less so
1830  * - I think more goes on after the perlio frees but
1831  *   the thing is that STDERR gets closed (as do all
1832  *   the file descriptors)
1833  * - no deeper calling stack than the caller of the Newx()
1834  *   or the kind, but do I look like a C reflection/introspection
1835  *   utility to you?
1836  * - the function prototypes for the logging functions
1837  *   probably should maybe be somewhere else than handy.h
1838  * - one could consider inlining (macrofying) the logging
1839  *   for speed, but I am too lazy
1840  * - one could imagine recording the allocations in a hash,
1841  *   (keyed by the allocation address?), and maintain that
1842  *   through reallocs and frees, but how to do that without
1843  *   any News() happening...?
1844  * - lots of -Ddefines to get useful/controllable output
1845  * - lots of ENV reads
1846  */
1847 
1848 PERL_EXPORT_C Malloc_t Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
1849 
1850 PERL_EXPORT_C Malloc_t Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name, Malloc_t oldalloc, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
1851 
1852 PERL_EXPORT_C Malloc_t Perl_mem_log_free(Malloc_t oldalloc, const char *filename, const int linenumber, const char *funcname);
1853 
1854 # ifdef PERL_CORE
1855 #  ifndef PERL_MEM_LOG_NOIMPL
1856 enum mem_log_type {
1857   MLT_ALLOC,
1858   MLT_REALLOC,
1859   MLT_FREE,
1860   MLT_NEW_SV,
1861   MLT_DEL_SV
1862 };
1863 #  endif
1864 #  if defined(PERL_IN_SV_C)  /* those are only used in sv.c */
1865 void Perl_mem_log_new_sv(const SV *sv, const char *filename, const int linenumber, const char *funcname);
1866 void Perl_mem_log_del_sv(const SV *sv, const char *filename, const int linenumber, const char *funcname);
1867 #  endif
1868 # endif
1869 
1870 #endif
1871 
1872 #ifdef PERL_MEM_LOG
1873 #define MEM_LOG_ALLOC(n,t,a)     Perl_mem_log_alloc(n,sizeof(t),STRINGIFY(t),a,__FILE__,__LINE__,FUNCTION__)
1874 #define MEM_LOG_REALLOC(n,t,v,a) Perl_mem_log_realloc(n,sizeof(t),STRINGIFY(t),v,a,__FILE__,__LINE__,FUNCTION__)
1875 #define MEM_LOG_FREE(a)          Perl_mem_log_free(a,__FILE__,__LINE__,FUNCTION__)
1876 #endif
1877 
1878 #ifndef MEM_LOG_ALLOC
1879 #define MEM_LOG_ALLOC(n,t,a)     (a)
1880 #endif
1881 #ifndef MEM_LOG_REALLOC
1882 #define MEM_LOG_REALLOC(n,t,v,a) (a)
1883 #endif
1884 #ifndef MEM_LOG_FREE
1885 #define MEM_LOG_FREE(a)          (a)
1886 #endif
1887 
1888 #define Newx(v,n,t)	(v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
1889 #define Newxc(v,n,t,c)	(v = (MEM_WRAP_CHECK_(n,t) (c*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
1890 #define Newxz(v,n,t)	(v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safecalloc((n),sizeof(t)))))
1891 
1892 #ifndef PERL_CORE
1893 /* pre 5.9.x compatibility */
1894 #define New(x,v,n,t)	Newx(v,n,t)
1895 #define Newc(x,v,n,t,c)	Newxc(v,n,t,c)
1896 #define Newz(x,v,n,t)	Newxz(v,n,t)
1897 #endif
1898 
1899 #define Renew(v,n,t) \
1900 	  (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
1901 #define Renewc(v,n,t,c) \
1902 	  (v = (MEM_WRAP_CHECK_(n,t) (c*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
1903 
1904 #ifdef PERL_POISON
1905 #define Safefree(d) \
1906   ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d))), Poison(&(d), 1, Malloc_t)) : (void) 0)
1907 #else
1908 #define Safefree(d)	safefree(MEM_LOG_FREE((Malloc_t)(d)))
1909 #endif
1910 
1911 #define Move(s,d,n,t)	(MEM_WRAP_CHECK_(n,t) (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
1912 #define Copy(s,d,n,t)	(MEM_WRAP_CHECK_(n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
1913 #define Zero(d,n,t)	(MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
1914 
1915 #define MoveD(s,d,n,t)	(MEM_WRAP_CHECK_(n,t) memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
1916 #define CopyD(s,d,n,t)	(MEM_WRAP_CHECK_(n,t) memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
1917 #ifdef HAS_MEMSET
1918 #define ZeroD(d,n,t)	(MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)))
1919 #else
1920 /* Using bzero(), which returns void.  */
1921 #define ZeroD(d,n,t)	(MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)),d)
1922 #endif
1923 
1924 #define PoisonWith(d,n,t,b)	(MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), (U8)(b), (n) * sizeof(t)))
1925 #define PoisonNew(d,n,t)	PoisonWith(d,n,t,0xAB)
1926 #define PoisonFree(d,n,t)	PoisonWith(d,n,t,0xEF)
1927 #define Poison(d,n,t)		PoisonFree(d,n,t)
1928 
1929 #ifdef PERL_POISON
1930 #  define PERL_POISON_EXPR(x) x
1931 #else
1932 #  define PERL_POISON_EXPR(x)
1933 #endif
1934 
1935 #ifdef USE_STRUCT_COPY
1936 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
1937 #else
1938 #define StructCopy(s,d,t) Copy(s,d,1,t)
1939 #endif
1940 
1941 /* C_ARRAY_LENGTH is the number of elements in the C array (so you
1942  * want your zero-based indices to be less than but not equal to).
1943  *
1944  * C_ARRAY_END is one past the last: half-open/half-closed range,
1945  * not last-inclusive range. */
1946 #define C_ARRAY_LENGTH(a)	(sizeof(a)/sizeof((a)[0]))
1947 #define C_ARRAY_END(a)		((a) + C_ARRAY_LENGTH(a))
1948 
1949 #ifdef NEED_VA_COPY
1950 # ifdef va_copy
1951 #  define Perl_va_copy(s, d) va_copy(d, s)
1952 # else
1953 #  if defined(__va_copy)
1954 #   define Perl_va_copy(s, d) __va_copy(d, s)
1955 #  else
1956 #   define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
1957 #  endif
1958 # endif
1959 #endif
1960 
1961 /* convenience debug macros */
1962 #ifdef USE_ITHREADS
1963 #define pTHX_FORMAT  "Perl interpreter: 0x%p"
1964 #define pTHX__FORMAT ", Perl interpreter: 0x%p"
1965 #define pTHX_VALUE_   (void *)my_perl,
1966 #define pTHX_VALUE    (void *)my_perl
1967 #define pTHX__VALUE_ ,(void *)my_perl,
1968 #define pTHX__VALUE  ,(void *)my_perl
1969 #else
1970 #define pTHX_FORMAT
1971 #define pTHX__FORMAT
1972 #define pTHX_VALUE_
1973 #define pTHX_VALUE
1974 #define pTHX__VALUE_
1975 #define pTHX__VALUE
1976 #endif /* USE_ITHREADS */
1977 
1978 /* Perl_deprecate was not part of the public API, and did not have a deprecate()
1979    shortcut macro defined without -DPERL_CORE. Neither codesearch.google.com nor
1980    CPAN::Unpack show any users outside the core.  */
1981 #ifdef PERL_CORE
1982 #  define deprecate(s) Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), "Use of " s " is deprecated")
1983 #endif
1984 
1985 /* Internal macros to deal with gids and uids */
1986 #ifdef PERL_CORE
1987 
1988 #  if Uid_t_size > IVSIZE
1989 #    define sv_setuid(sv, uid)       sv_setnv((sv), (NV)(uid))
1990 #    define SvUID(sv)                SvNV(sv)
1991 #  else
1992 #    if Uid_t_sign <= 0
1993 #      define sv_setuid(sv, uid)       sv_setiv((sv), (IV)(uid))
1994 #      define SvUID(sv)                SvIV(sv)
1995 #    else
1996 #      define sv_setuid(sv, uid)       sv_setuv((sv), (UV)(uid))
1997 #      define SvUID(sv)                SvUV(sv)
1998 #    endif
1999 #  endif /* Uid_t_size */
2000 
2001 #  if Gid_t_size > IVSIZE
2002 #    define sv_setgid(sv, gid)       sv_setnv((sv), (NV)(gid))
2003 #    define SvGID(sv)                SvNV(sv)
2004 #  else
2005 #    if Gid_t_sign <= 0
2006 #      define sv_setgid(sv, gid)       sv_setiv((sv), (IV)(gid))
2007 #      define SvGID(sv)                SvIV(sv)
2008 #    else
2009 #      define sv_setgid(sv, gid)       sv_setuv((sv), (UV)(gid))
2010 #      define SvGID(sv)                SvUV(sv)
2011 #    endif
2012 #  endif /* Gid_t_size */
2013 
2014 #endif
2015 
2016 #endif  /* HANDY_H */
2017 
2018 /*
2019  * Local variables:
2020  * c-indentation-style: bsd
2021  * c-basic-offset: 4
2022  * indent-tabs-mode: nil
2023  * End:
2024  *
2025  * ex: set ts=8 sts=4 sw=4 et:
2026  */
2027