This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change handy.h macro names to be C standard conformant
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
da5d8dbb 4 * 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2012 by Larry Wall and others
a687059c 5 *
6e21c824
LW
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.
8d063cd8 8 *
8d063cd8
LW
9 */
10
4650c663
KW
11/* IMPORTANT NOTE: Everything whose name begins with an underscore is for
12 * internal core Perl use only. */
13
6a5bc5ac
KW
14#ifndef PERL_HANDY_H_ /* Guard against nested #inclusion */
15#define PERL_HANDY_H_
9d745869 16
24792b8d
NC
17#ifndef PERL_CORE
18# define Null(type) ((type)NULL)
954c1994
GS
19
20/*
3f620621 21=for apidoc_section $string
78342678 22=for apidoc AmnU||Nullch
72d33970
FC
23Null character pointer. (No longer available when C<PERL_CORE> is
24defined.)
2307c6d0 25
3f620621 26=for apidoc_section $SV
78342678 27=for apidoc AmnU||Nullsv
72d33970 28Null SV pointer. (No longer available when C<PERL_CORE> is defined.)
954c1994
GS
29
30=cut
51b56f5c
KW
31
32Below are signatures of functions from config.h which can't easily be gleaned
33from it, and are very unlikely to change
34
3f620621 35=for apidoc_section $signals
51b56f5c
KW
36=for apidoc Am|int|Sigsetjmp|jmp_buf env|int savesigs
37=for apidoc Am|void|Siglongjmp|jmp_buf env|int val
38
3f620621 39=for apidoc_section $filesystem
51b56f5c
KW
40=for apidoc Am|void *|FILE_ptr|FILE * f
41=for apidoc Am|Size_t|FILE_cnt|FILE * f
42=for apidoc Am|void *|FILE_base|FILE * f
b290de04 43=for apidoc Am|Size_t|FILE_bufsiz|FILE *f
51b56f5c 44
3f620621 45=for apidoc_section $string
b290de04
KW
46=for apidoc Amu|token|CAT2|token x|token y
47=for apidoc Amu|string|STRINGIFY|token x
51b56f5c 48
3f620621 49=for apidoc_section $numeric
b290de04 50=for apidoc Am|double|Drand01
51b56f5c
KW
51=for apidoc Am|void|seedDrand01|Rand_seed_t x
52=for apidoc Am|char *|Gconvert|double x|Size_t n|bool t|char * b
53
54=cut
954c1994
GS
55*/
56
24792b8d
NC
57# define Nullch Null(char*)
58# define Nullfp Null(PerlIO*)
59# define Nullsv Null(SV*)
60#endif
8d063cd8 61
641d3f0b 62#ifdef TRUE
63#undef TRUE
64#endif
65#ifdef FALSE
66#undef FALSE
67#endif
68#define TRUE (1)
69#define FALSE (0)
70
df87895c 71/*
3f620621 72=for apidoc_section $SV
df87895c
KW
73=for apidoc Am|void *|MUTABLE_PTR|void * p
74=for apidoc_item |AV *|MUTABLE_AV|AV * p
75=for apidoc_item |CV *|MUTABLE_CV|CV * p
76=for apidoc_item |GV *|MUTABLE_GV|GV * p
77=for apidoc_item |HV *|MUTABLE_HV|HV * p
78=for apidoc_item |IO *|MUTABLE_IO|IO * p
79=for apidoc_item |SV *|MUTABLE_SV|SV * p
80
81The C<MUTABLE_I<*>>() macros cast pointers to the types shown, in such a way
82(compiler permitting) that casting away const-ness will give a warning;
83e.g.:
84
85 const SV *sv = ...;
86 AV *av1 = (AV*)sv; <== BAD: the const has been silently
87 cast away
88 AV *av2 = MUTABLE_AV(sv); <== GOOD: it may warn
89
54ba56f3
KW
90C<MUTABLE_PTR> is the base macro used to derive new casts. The other
91already-built-in ones return pointers to what their names indicate.
df87895c
KW
92
93=cut
cf3f0ffb
DM
94 */
95
041c1a23 96#if defined(PERL_USE_GCC_BRACE_GROUPS)
6c2255e0 97# define MUTABLE_PTR(p) ({ void *p_ = (p); p_; })
b1bc3f34
NC
98#else
99# define MUTABLE_PTR(p) ((void *) (p))
100#endif
101
a062e10d 102#define MUTABLE_AV(p) ((AV *)MUTABLE_PTR(p))
ea726b52 103#define MUTABLE_CV(p) ((CV *)MUTABLE_PTR(p))
159b6efe 104#define MUTABLE_GV(p) ((GV *)MUTABLE_PTR(p))
dbebbdb4 105#define MUTABLE_HV(p) ((HV *)MUTABLE_PTR(p))
a45c7426 106#define MUTABLE_IO(p) ((IO *)MUTABLE_PTR(p))
b1bc3f34 107#define MUTABLE_SV(p) ((SV *)MUTABLE_PTR(p))
27d4fb96 108
b1c011dc 109#ifndef __cplusplus
bd31be4b 110# include <stdbool.h>
bd31be4b
NC
111#endif
112
25ba28ce 113/*
3f620621 114=for apidoc_section $casting
25ba28ce
KW
115=for apidoc Am|bool|cBOOL|bool expr
116
9fa5fd96
KW
117Cast-to-bool. When Perl was able to be compiled on pre-C99 compilers, a
118C<(bool)> cast didn't necessarily do the right thing, so this macro was
119created (and made somewhat complicated to work around bugs in old
120compilers). Now, many years later, and C99 is used, this is no longer
121required, but is kept for backwards compatibility.
25ba28ce
KW
122
123=cut
124*/
9fa5fd96 125#define cBOOL(cbool) ((bool) (cbool))
f2338a2e 126
46c6c7e2 127/* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
e352bcff
JH
128 * XXX Should really be a Configure probe, with HAS__FUNCTION__
129 * and FUNCTION__ as results.
130 * XXX Similarly, a Configure probe for __FILE__ and __LINE__ is needed. */
46c6c7e2
JH
131#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
132# define FUNCTION__ __func__
7adf2470 133#elif (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used, but not modern enough cc (in Tur64, -c99 not known, only -std1). */
07798b17 134# define FUNCTION__ ""
46c6c7e2 135#else
07798b17 136# define FUNCTION__ __FUNCTION__ /* Common extension. */
46c6c7e2
JH
137#endif
138
27d4fb96 139/* XXX A note on the perl source internal type system. The
140 original intent was that I32 be *exactly* 32 bits.
141
142 Currently, we only guarantee that I32 is *at least* 32 bits.
143 Specifically, if int is 64 bits, then so is I32. (This is the case
144 for the Cray.) This has the advantage of meshing nicely with
145 standard library calls (where we pass an I32 and the library is
146 expecting an int), but the disadvantage that an I32 is not 32 bits.
147 Andy Dougherty August 1996
24fef2a7 148
dc45a647
MB
149 There is no guarantee that there is *any* integral type with
150 exactly 32 bits. It is perfectly legal for a system to have
151 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 152
dc45a647
MB
153 Similarly, there is no guarantee that I16 and U16 have exactly 16
154 bits.
693762b4 155
8e84507e
NIS
156 For dealing with issues that may arise from various 32/64-bit
157 systems, we will ask Configure to check out
8175356b 158
1604cfb0
MS
159 SHORTSIZE == sizeof(short)
160 INTSIZE == sizeof(int)
161 LONGSIZE == sizeof(long)
162 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
163 PTRSIZE == sizeof(void *)
164 DOUBLESIZE == sizeof(double)
165 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 166
27d4fb96 167*/
168
69512466
JH
169#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
170# include <inttypes.h>
dd0eed91
JH
171# ifdef INT32_MIN_BROKEN
172# undef INT32_MIN
173# define INT32_MIN (-2147483647-1)
174# endif
175# ifdef INT64_MIN_BROKEN
176# undef INT64_MIN
177# define INT64_MIN (-9223372036854775807LL-1)
178# endif
69512466
JH
179#endif
180
8175356b
JH
181typedef I8TYPE I8;
182typedef U8TYPE U8;
183typedef I16TYPE I16;
184typedef U16TYPE U16;
185typedef I32TYPE I32;
186typedef U32TYPE U32;
16d89be8 187
74b807c7 188#ifdef QUADKIND
8175356b
JH
189typedef I64TYPE I64;
190typedef U64TYPE U64;
16d89be8 191#endif
8175356b 192
5ff3f7a4
GS
193/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
194 Please search CHAR_MAX in perl.h for further details. */
26e4a9a9
KW
195#ifdef UINT8_MAX
196# define U8_MAX UINT8_MAX
0e983133 197#else
26e4a9a9 198# define U8_MAX PERL_UCHAR_MAX
0e983133 199#endif
26e4a9a9
KW
200#ifdef UINT8_MIN
201# define U8_MIN UINT8_MIN
5ff3f7a4 202#else
26e4a9a9
KW
203# define U8_MIN PERL_UCHAR_MIN
204#endif
5ff3f7a4 205
26e4a9a9
KW
206#ifdef INT16_MAX
207# define I16_MAX INT16_MAX
79072805 208#else
26e4a9a9
KW
209# define I16_MAX PERL_SHORT_MAX
210#endif
211#ifdef INT16_MIN
212# define I16_MIN INT16_MIN
213#else
214# define I16_MIN PERL_SHORT_MIN
215#endif
216#ifdef UINT16_MAX
217# define U16_MAX UINT16_MAX
218#else
219# define U16_MAX PERL_USHORT_MAX
220#endif
221#ifdef UINT16_MIN
222# define U16_MIN UINT16_MIN
223#else
224# define U16_MIN PERL_USHORT_MIN
79072805
LW
225#endif
226
26e4a9a9
KW
227#ifdef INT32_MAX
228# define I32_MAX INT32_MAX
229#elif LONGSIZE > 4
230# define I32_MAX PERL_INT_MAX
231#else
232# define I32_MAX PERL_LONG_MAX
233#endif
234#ifdef INT32_MIN
235# define I32_MIN INT32_MIN
236#elif LONGSIZE > 4
237# define I32_MIN PERL_INT_MIN
238#else
239# define I32_MIN PERL_LONG_MIN
240#endif
241#ifdef UINT32_MAX
242# ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
243# define U32_MAX UINT_MAX
244# else
245# define U32_MAX 4294967295U
246# endif
247#elif LONGSIZE > 4
248# define U32_MAX PERL_UINT_MAX
249#else
250# define U32_MAX PERL_ULONG_MAX
251#endif
252#ifdef UINT32_MIN
253# define U32_MIN UINT32_MIN
254#elif LONGSIZE > 4
255# define U32_MIN PERL_UINT_MIN
256#else
257# define U32_MIN PERL_ULONG_MIN
5ff3f7a4
GS
258#endif
259
23332c7d
KW
260/*
261=for apidoc_section $integer
262=for apidoc Ay|| PERL_INT_FAST8_T
263=for apidoc_item PERL_INT_FAST16_T
264=for apidoc_item PERL_UINT_FAST8_T
265=for apidoc_item PERL_UINT_FAST16_T
266
267These are equivalent to the correspondingly-named C99 typedefs on platforms
268that have those; they evaluate to C<int> and C<unsigned int> on platforms that
269don't, so that you can portably take advantage of this C99 feature.
270
271=cut
272*/
247cee9f
KW
273# ifdef I_STDINT
274 typedef int_fast8_t PERL_INT_FAST8_T;
275 typedef uint_fast8_t PERL_UINT_FAST8_T;
276 typedef int_fast16_t PERL_INT_FAST16_T;
277 typedef uint_fast16_t PERL_UINT_FAST16_T;
278# else
279 typedef int PERL_INT_FAST8_T;
280 typedef unsigned int PERL_UINT_FAST8_T;
281 typedef int PERL_INT_FAST16_T;
282 typedef unsigned int PERL_UINT_FAST16_T;
283# endif
247cee9f 284
464decb6 285/* log(2) (i.e., log base 10 of 2) is pretty close to 0.30103, just in case
ab350cbd
KW
286 * anyone is grepping for it. So BIT_DIGITS gives the number of decimal digits
287 * required to represent any possible unsigned number containing N bits.
288 * TYPE_DIGITS gives the number of decimal digits required to represent any
289 * possible unsigned number of type T. */
464decb6 290#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log10(2) =~ 146/485 */
fc36a67e 291#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
292#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
293
88794300 294/* Unused by core; should be deprecated */
ff68c719 295#define Ctl(ch) ((ch) & 037)
8d063cd8 296
98fce2a4
KW
297#if defined(PERL_CORE) || defined(PERL_EXT)
298# ifndef MIN
299# define MIN(a,b) ((a) < (b) ? (a) : (b))
300# endif
301# ifndef MAX
302# define MAX(a,b) ((a) > (b) ? (a) : (b))
303# endif
304#endif
305
84ff4fa9
KW
306/* Returns a boolean as to whether the input unsigned number is a power of 2
307 * (2**0, 2**1, etc). In other words if it has just a single bit set.
308 * If not, subtracting 1 would leave the uppermost bit set, so the & would
309 * yield non-zero */
310#if defined(PERL_CORE) || defined(PERL_EXT)
011b1419 311# define isPOWER_OF_2(n) ((n) && ((n) & ((n)-1)) == 0)
84ff4fa9
KW
312#endif
313
d223e1ea 314/* Returns a mask with the lowest n bits set */
fae1e72b 315#define nBIT_MASK(n) ((UINTMAX_C(1) << (n)) - 1)
d223e1ea 316
1381ccb1
KW
317/* The largest unsigned number that will fit into n bits */
318#define nBIT_UMAX(n) nBIT_MASK(n)
319
8d9433eb 320/*
3f620621 321=for apidoc_section $directives
d23778e6 322=for apidoc Am||__ASSERT_|bool expr
8d9433eb
KW
323
324This is a helper macro to avoid preprocessor issues, replaced by nothing
325unless under DEBUGGING, where it expands to an assert of its argument,
326followed by a comma (hence the comma operator). If we just used a straight
327assert(), we would get a comma with nothing before it when not DEBUGGING.
328
329=cut
330
803e4935 331We also use empty definition under Coverity since the __ASSERT_
8d9433eb
KW
332checks often check for things that Really Cannot Happen, and Coverity
333detects that and gets all excited. */
3e94db23 334
e7ae132e
KW
335#if defined(DEBUGGING) && !defined(__COVERITY__) \
336 && ! defined(PERL_SMALL_MACRO_BUFFER)
0f092d08
KW
337# define __ASSERT_(statement) assert(statement),
338#else
339# define __ASSERT_(statement)
340#endif
341
3fe05580 342/*
3f620621 343=for apidoc_section $SV
3fe05580 344
3bb9fd01 345=for apidoc Ama|SV*|newSVpvs|"literal string"
1568d13a 346Like C<newSVpvn>, but takes a literal string instead of a
30a15352 347string/length pair.
3fe05580 348
3bb9fd01 349=for apidoc Ama|SV*|newSVpvs_flags|"literal string"|U32 flags
1568d13a 350Like C<newSVpvn_flags>, but takes a literal string instead of
30a15352 351a string/length pair.
84bafc02 352
3bb9fd01 353=for apidoc Ama|SV*|newSVpvs_share|"literal string"
1568d13a 354Like C<newSVpvn_share>, but takes a literal string instead of
30a15352 355a string/length pair and omits the hash parameter.
3fe05580 356
3bb9fd01 357=for apidoc Am|void|sv_catpvs_flags|SV* sv|"literal string"|I32 flags
1568d13a 358Like C<sv_catpvn_flags>, but takes a literal string instead
30a15352 359of a string/length pair.
9dcc53ea 360
3bb9fd01 361=for apidoc Am|void|sv_catpvs_nomg|SV* sv|"literal string"
1568d13a 362Like C<sv_catpvn_nomg>, but takes a literal string instead of
0c395ea5 363a string/length pair.
9dcc53ea 364
3bb9fd01 365=for apidoc Am|void|sv_catpvs|SV* sv|"literal string"
1568d13a 366Like C<sv_catpvn>, but takes a literal string instead of a
0c395ea5 367string/length pair.
3fe05580 368
3bb9fd01 369=for apidoc Am|void|sv_catpvs_mg|SV* sv|"literal string"
1568d13a 370Like C<sv_catpvn_mg>, but takes a literal string instead of a
9dcc53ea
Z
371string/length pair.
372
3bb9fd01 373=for apidoc Am|SV *|sv_setref_pvs|SV *const rv|const char *const classname|"literal string"
1568d13a 374Like C<sv_setref_pvn>, but takes a literal string instead of
0c395ea5 375a string/length pair.
9dcc53ea 376
3f620621 377=for apidoc_section $string
3fe05580 378
3bb9fd01 379=for apidoc Ama|char*|savepvs|"literal string"
1568d13a 380Like C<savepvn>, but takes a literal string instead of a
30a15352 381string/length pair.
3fe05580 382
3bb9fd01 383=for apidoc Ama|char*|savesharedpvs|"literal string"
9dcc53ea
Z
384A version of C<savepvs()> which allocates the duplicate string in memory
385which is shared between threads.
386
3f620621 387=for apidoc_section $GV
3fe05580 388
3bb9fd01 389=for apidoc Am|HV*|gv_stashpvs|"name"|I32 create
1568d13a 390Like C<gv_stashpvn>, but takes a literal string instead of a
0c395ea5 391string/length pair.
3fe05580 392
3f620621 393=for apidoc_section $HV
3fe05580 394
3bb9fd01 395=for apidoc Am|SV**|hv_fetchs|HV* tb|"key"|I32 lval
1568d13a 396Like C<hv_fetch>, but takes a literal string instead of a
0c395ea5 397string/length pair.
3f620621 398=for apidoc_section $lexer
510966aa 399
3bb9fd01 400=for apidoc Amx|void|lex_stuff_pvs|"pv"|U32 flags
510966aa 401
1568d13a 402Like L</lex_stuff_pvn>, but takes a literal string instead of
0c395ea5 403a string/length pair.
510966aa 404
3fe05580
MHM
405=cut
406*/
407
ca0572d7
KW
408#define ASSERT_IS_LITERAL(s) ("" s "")
409
a34e53fc 410/*
3f620621 411=for apidoc_section $string
2efa8cc7 412
a34e53fc
KW
413=for apidoc Amu|pair|STR_WITH_LEN|"literal string"
414
415Returns two comma separated tokens of the input literal string, and its length.
416This is convenience macro which helps out in some API calls.
417Note that it can't be used as an argument to macros or functions that under
418some configurations might be macros, which means that it requires the full
419Perl_xxx(aTHX_ ...) form for any API calls where it's used.
420
421=cut
422*/
423
ca0572d7 424#define STR_WITH_LEN(s) ASSERT_IS_LITERAL(s), (sizeof(s)-1)
ba3a79e7
GA
425
426/* STR_WITH_LEN() shortcuts */
427#define newSVpvs(str) Perl_newSVpvn(aTHX_ STR_WITH_LEN(str))
84bafc02
NC
428#define newSVpvs_flags(str,flags) \
429 Perl_newSVpvn_flags(aTHX_ STR_WITH_LEN(str), flags)
ba3a79e7 430#define newSVpvs_share(str) Perl_newSVpvn_share(aTHX_ STR_WITH_LEN(str), 0)
9dcc53ea
Z
431#define sv_catpvs_flags(sv, str, flags) \
432 Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), flags)
433#define sv_catpvs_nomg(sv, str) \
434 Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), 0)
435#define sv_catpvs(sv, str) \
436 Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC)
437#define sv_catpvs_mg(sv, str) \
438 Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC|SV_SMAGIC)
3fe05580 439#define sv_setpvs(sv, str) Perl_sv_setpvn(aTHX_ sv, STR_WITH_LEN(str))
9dcc53ea
Z
440#define sv_setpvs_mg(sv, str) Perl_sv_setpvn_mg(aTHX_ sv, STR_WITH_LEN(str))
441#define sv_setref_pvs(rv, classname, str) \
442 Perl_sv_setref_pvn(aTHX_ rv, classname, STR_WITH_LEN(str))
ba3a79e7 443#define savepvs(str) Perl_savepvn(aTHX_ STR_WITH_LEN(str))
9dcc53ea
Z
444#define savesharedpvs(str) Perl_savesharedpvn(aTHX_ STR_WITH_LEN(str))
445#define gv_stashpvs(str, create) \
446 Perl_gv_stashpvn(aTHX_ STR_WITH_LEN(str), create)
3752a9fe
KW
447
448#define gv_fetchpvs(namebeg, flags, sv_type) \
449 Perl_gv_fetchpvn_flags(aTHX_ STR_WITH_LEN(namebeg), flags, sv_type)
72659597 450#define gv_fetchpvn gv_fetchpvn_flags
9dcc53ea
Z
451#define sv_catxmlpvs(dsv, str, utf8) \
452 Perl_sv_catxmlpvn(aTHX_ dsv, STR_WITH_LEN(str), utf8)
4ac46235 453
ba3a79e7 454
510966aa
Z
455#define lex_stuff_pvs(pv,flags) Perl_lex_stuff_pvn(aTHX_ STR_WITH_LEN(pv), flags)
456
b96d8cd9 457#define get_cvs(str, flags) \
1604cfb0 458 Perl_get_cvn_flags(aTHX_ STR_WITH_LEN(str), (flags))
5c1737d1 459
9b6e9510 460/* internal helpers */
4a1bbd3d 461/* Transitional */
d67f622b
N
462#ifndef PERL_VERSION_MAJOR
463# define PERL_VERSION_MAJOR PERL_REVISION
4a1bbd3d
KW
464#else
465# undef PERL_REVISION /* We don't want code to be using these */
466#endif
d67f622b
N
467#ifndef PERL_VERSION_MINOR
468# define PERL_VERSION_MINOR PERL_VERSION
4a1bbd3d
KW
469#else
470# undef PERL_VERSION
471#endif
d67f622b
N
472#ifndef PERL_VERSION_PATCH
473# define PERL_VERSION_PATCH PERL_SUBVERSION
4a1bbd3d
KW
474#else
475# undef PERL_SUBVERSION
476#endif
477
478#define PERL_JNP_TO_DECIMAL_(maJor,miNor,Patch) \
479 /* '10*' leaves room for things like alpha, beta, releases */ \
480 (10 * ((maJor) * 1000000) + ((miNor) * 1000) + (Patch))
9b6e9510 481#define PERL_DECIMAL_VERSION_ \
d67f622b
N
482 PERL_JNP_TO_DECIMAL_(PERL_VERSION_MAJOR, PERL_VERSION_MINOR, \
483 PERL_VERSION_PATCH)
9b6e9510
KW
484
485/*
3f620621 486=for apidoc_section $versioning
4a1bbd3d 487=for apidoc AmR|bool|PERL_VERSION_EQ|const U8 major|const U8 minor|const U8 patch
9de44d19
KW
488=for apidoc_item PERL_VERSION_NE
489=for apidoc_item PERL_VERSION_LT
490=for apidoc_item PERL_VERSION_LE
491=for apidoc_item PERL_VERSION_GT
492=for apidoc_item PERL_VERSION_GE
9b6e9510 493
4a1bbd3d 494Returns whether or not the perl currently being compiled has the specified
9b6e9510
KW
495relationship to the perl given by the parameters. For example,
496
497 #if PERL_VERSION_GT(5,24,2)
498 code that will only be compiled on perls after v5.24.2
499 #else
500 fallback code
501 #endif
502
503Note that this is usable in making compile-time decisions
504
4a1bbd3d
KW
505You may use the special value '*' for the final number to mean ALL possible
506values for it. Thus,
507
508 #if PERL_VERSION_EQ(5,31,'*')
509
510means all perls in the 5.31 series. And
511
512 #if PERL_VERSION_NE(5,24,'*')
513
514means all perls EXCEPT 5.24 ones. And
515
516 #if PERL_VERSION_LE(5,9,'*')
517
518is effectively
519
520 #if PERL_VERSION_LT(5,10,0)
521
522This means you don't have to think so much when converting from the existing
523deprecated C<PERL_VERSION> to using this macro:
524
525 #if PERL_VERSION <= 9
526
527becomes
528
529 #if PERL_VERSION_LE(5,9,'*')
530
9b6e9510
KW
531=cut
532*/
533
4a1bbd3d
KW
534/* N.B. These don't work if the patch version is 42 or 92, as those are what
535 * '*' is in ASCII and EBCDIC respectively */
536# define PERL_VERSION_EQ(j,n,p) \
537 (((p) == '*') \
d67f622b
N
538 ? ( (j) == PERL_VERSION_MAJOR \
539 && (n) == PERL_VERSION_MINOR) \
4a1bbd3d
KW
540 : (PERL_DECIMAL_VERSION_ == PERL_JNP_TO_DECIMAL_(j,n,p)))
541# define PERL_VERSION_NE(j,n,p) (! PERL_VERSION_EQ(j,n,p))
542
543# define PERL_VERSION_LT(j,n,p) /* < '*' effectively means < 0 */ \
544 (PERL_DECIMAL_VERSION_ < PERL_JNP_TO_DECIMAL_( (j), \
545 (n), \
546 (((p) == '*') ? 0 : p)))
547# define PERL_VERSION_GE(j,n,p) (! PERL_VERSION_LT(j,n,p))
548
549# define PERL_VERSION_LE(j,n,p) /* <= '*' effectively means < n+1 */ \
550 (PERL_DECIMAL_VERSION_ < PERL_JNP_TO_DECIMAL_( (j), \
551 (((p) == '*') ? ((n)+1) : (n)), \
552 (((p) == '*') ? 0 : p)))
553# define PERL_VERSION_GT(j,n,p) (! PERL_VERSION_LE(j,n,p))
9b6e9510 554
954c1994 555/*
3f620621 556=for apidoc_section $string
ccfc67b7 557
954c1994 558=for apidoc Am|bool|strNE|char* s1|char* s2
dc6b0978
KW
559Test two C<NUL>-terminated strings to see if they are different. Returns true
560or false.
954c1994
GS
561
562=for apidoc Am|bool|strEQ|char* s1|char* s2
dc6b0978
KW
563Test two C<NUL>-terminated strings to see if they are equal. Returns true or
564false.
954c1994
GS
565
566=for apidoc Am|bool|strLT|char* s1|char* s2
dc6b0978
KW
567Test two C<NUL>-terminated strings to see if the first, C<s1>, is less than the
568second, C<s2>. Returns true or false.
954c1994
GS
569
570=for apidoc Am|bool|strLE|char* s1|char* s2
dc6b0978
KW
571Test two C<NUL>-terminated strings to see if the first, C<s1>, is less than or
572equal to the second, C<s2>. Returns true or false.
954c1994
GS
573
574=for apidoc Am|bool|strGT|char* s1|char* s2
dc6b0978
KW
575Test two C<NUL>-terminated strings to see if the first, C<s1>, is greater than
576the second, C<s2>. Returns true or false.
954c1994
GS
577
578=for apidoc Am|bool|strGE|char* s1|char* s2
dc6b0978
KW
579Test two C<NUL>-terminated strings to see if the first, C<s1>, is greater than
580or equal to the second, C<s2>. Returns true or false.
954c1994
GS
581
582=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
dc6b0978
KW
583Test two C<NUL>-terminated strings to see if they are different. The C<len>
584parameter indicates the number of bytes to compare. Returns true or false. (A
954c1994
GS
585wrapper for C<strncmp>).
586
587=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
dc6b0978
KW
588Test two C<NUL>-terminated strings to see if they are equal. The C<len>
589parameter indicates the number of bytes to compare. Returns true or false. (A
590wrapper for C<strncmp>).
954c1994 591
bd18bd40
KW
592=for apidoc Am|bool|memEQ|char* s1|char* s2|STRLEN len
593Test two buffers (which may contain embedded C<NUL> characters, to see if they
594are equal. The C<len> parameter indicates the number of bytes to compare.
b96bd7bf
KW
595Returns true or false. It is undefined behavior if either of the buffers
596doesn't contain at least C<len> bytes.
bd18bd40 597
3bb9fd01 598=for apidoc Am|bool|memEQs|char* s1|STRLEN l1|"s2"
2d8eeddb
KW
599Like L</memEQ>, but the second string is a literal enclosed in double quotes,
600C<l1> gives the number of bytes in C<s1>.
b96bd7bf 601Returns true or false.
2d8eeddb 602
bd18bd40
KW
603=for apidoc Am|bool|memNE|char* s1|char* s2|STRLEN len
604Test two buffers (which may contain embedded C<NUL> characters, to see if they
605are not equal. The C<len> parameter indicates the number of bytes to compare.
b96bd7bf
KW
606Returns true or false. It is undefined behavior if either of the buffers
607doesn't contain at least C<len> bytes.
bd18bd40 608
3bb9fd01 609=for apidoc Am|bool|memNEs|char* s1|STRLEN l1|"s2"
2d8eeddb
KW
610Like L</memNE>, but the second string is a literal enclosed in double quotes,
611C<l1> gives the number of bytes in C<s1>.
b96bd7bf 612Returns true or false.
2d8eeddb 613
4aada8b9
KW
614=for apidoc Am|bool|memCHRs|"list"|char c
615Returns the position of the first occurence of the byte C<c> in the literal
616string C<"list">, or NULL if C<c> doesn't appear in C<"list">. All bytes are
617treated as unsigned char. Thus this macro can be used to determine if C<c> is
618in a set of particular characters. Unlike L<strchr(3)>, it works even if C<c>
619is C<NUL> (and the set doesn't include C<NUL>).
620
954c1994 621=cut
fc169e00
KW
622
623New macros should use the following conventions for their names (which are
624based on the underlying C library functions):
625
626 (mem | str n? ) (EQ | NE | LT | GT | GE | (( BEGIN | END ) P? )) l? s?
627
628 Each has two main parameters, string-like operands that are compared
629 against each other, as specified by the macro name. Some macros may
630 additionally have one or potentially even two length parameters. If a length
631 parameter applies to both string parameters, it will be positioned third;
632 otherwise any length parameter immediately follows the string parameter it
633 applies to.
634
635 If the prefix to the name is 'str', the string parameter is a pointer to a C
636 language string. Such a string does not contain embedded NUL bytes; its
637 length may be unknown, but can be calculated by C<strlen()>, since it is
638 terminated by a NUL, which isn't included in its length.
639
a3815e44 640 The optional 'n' following 'str' means that there is a third parameter,
fc169e00
KW
641 giving the maximum number of bytes to look at in each string. Even if both
642 strings are longer than the length parameter, those extra bytes will be
643 unexamined.
644
645 The 's' suffix means that the 2nd byte string parameter is a literal C
646 double-quoted string. Its length will automatically be calculated by the
647 macro, so no length parameter will ever be needed for it.
648
649 If the prefix is 'mem', the string parameters don't have to be C strings;
650 they may contain embedded NUL bytes, do not necessarily have a terminating
651 NUL, and their lengths can be known only through other means, which in
652 practice are additional parameter(s) passed to the function. All 'mem'
653 functions have at least one length parameter. Barring any 'l' or 's' suffix,
654 there is a single length parameter, in position 3, which applies to both
655 string parameters. The 's' suffix means, as described above, that the 2nd
656 string is a literal double-quoted C string (hence its length is calculated by
657 the macro, and the length parameter to the function applies just to the first
658 string parameter, and hence is positioned just after it). An 'l' suffix
659 means that the 2nd string parameter has its own length parameter, and the
660 signature will look like memFOOl(s1, l1, s2, l2).
661
662 BEGIN (and END) are for testing if the 2nd string is an initial (or final)
663 substring of the 1st string. 'P' if present indicates that the substring
664 must be a "proper" one in tha mathematical sense that the first one must be
665 strictly larger than the 2nd.
666
954c1994
GS
667*/
668
62946e08 669
75400963
KW
670#define strNE(s1,s2) (strcmp(s1,s2) != 0)
671#define strEQ(s1,s2) (strcmp(s1,s2) == 0)
8d063cd8
LW
672#define strLT(s1,s2) (strcmp(s1,s2) < 0)
673#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
674#define strGT(s1,s2) (strcmp(s1,s2) > 0)
675#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
62946e08 676
75400963
KW
677#define strnNE(s1,s2,l) (strncmp(s1,s2,l) != 0)
678#define strnEQ(s1,s2,l) (strncmp(s1,s2,l) == 0)
378cc40b 679
9d3980bc
KW
680#define memEQ(s1,s2,l) (memcmp(((const void *) (s1)), ((const void *) (s2)), l) == 0)
681#define memNE(s1,s2,l) (! memEQ(s1,s2,l))
36477c24 682
085b7534 683/* memEQ and memNE where second comparand is a string constant */
568a785a 684#define memEQs(s1, l, s2) \
ca0572d7 685 (((sizeof(s2)-1) == (l)) && memEQ((s1), ASSERT_IS_LITERAL(s2), (sizeof(s2)-1)))
5f50c6c9 686#define memNEs(s1, l, s2) (! memEQs(s1, l, s2))
568a785a 687
fdbb9a7c
KW
688/* Keep these private until we decide it was a good idea */
689#if defined(PERL_CORE) || defined(PERL_EXT) || defined(PERL_EXT_POSIX)
690
ca0572d7 691#define strBEGINs(s1,s2) (strncmp(s1,ASSERT_IS_LITERAL(s2), sizeof(s2)-1) == 0)
fdbb9a7c 692
bdb7e3f0 693#define memBEGINs(s1, l, s2) \
30a6480c 694 ( (Ptrdiff_t) (l) >= (Ptrdiff_t) sizeof(s2) - 1 \
ca0572d7 695 && memEQ(s1, ASSERT_IS_LITERAL(s2), sizeof(s2)-1))
de627158 696#define memBEGINPs(s1, l, s2) \
30a6480c 697 ( (Ptrdiff_t) (l) > (Ptrdiff_t) sizeof(s2) - 1 \
ca0572d7 698 && memEQ(s1, ASSERT_IS_LITERAL(s2), sizeof(s2)-1))
bdb7e3f0 699#define memENDs(s1, l, s2) \
30a6480c 700 ( (Ptrdiff_t) (l) >= (Ptrdiff_t) sizeof(s2) - 1 \
ca0572d7 701 && memEQ(s1 + (l) - (sizeof(s2) - 1), ASSERT_IS_LITERAL(s2), sizeof(s2)-1))
b80f8424 702#define memENDPs(s1, l, s2) \
30a6480c 703 ( (Ptrdiff_t) (l) > (Ptrdiff_t) sizeof(s2) \
ca0572d7 704 && memEQ(s1 + (l) - (sizeof(s2) - 1), ASSERT_IS_LITERAL(s2), sizeof(s2)-1))
fdbb9a7c 705#endif /* End of making macros private */
bdb7e3f0 706
062b6850
KW
707#define memLT(s1,s2,l) (memcmp(s1,s2,l) < 0)
708#define memLE(s1,s2,l) (memcmp(s1,s2,l) <= 0)
709#define memGT(s1,s2,l) (memcmp(s1,s2,l) > 0)
710#define memGE(s1,s2,l) (memcmp(s1,s2,l) >= 0)
711
ca0572d7 712#define memCHRs(s1,c) ((const char *) memchr(ASSERT_IS_LITERAL(s1) , c, sizeof(s1)-1))
4aada8b9 713
bbce6d69 714/*
715 * Character classes.
716 *
717 * Unfortunately, the introduction of locales means that we
718 * can't trust isupper(), etc. to tell the truth. And when
719 * it comes to /\w+/ with tainting enabled, we *must* be able
720 * to trust our character classes.
721 *
722 * Therefore, the default tests in the text of Perl will be
723 * independent of locale. Any code that wants to depend on
724 * the current locale will use the tests that begin with "lc".
725 */
726
553cc101 727#ifdef USE_LOCALE
2304df62
AD
728# ifndef CTYPE256
729# define CTYPE256
730# endif
731#endif
732
954c1994 733/*
ccfc67b7 734
dcccc8ff 735=head1 Character classification
243effed
KW
736This section is about functions (really macros) that classify characters
737into types, such as punctuation versus alphabetic, etc. Most of these are
738analogous to regular expression character classes. (See
739L<perlrecharclass/POSIX Character Classes>.) There are several variants for
740each class. (Not all macros have all variants; each item below lists the
741ones valid for it.) None are affected by C<use bytes>, and only the ones
742with C<LC> in the name are affected by the current locale.
743
d713f9d9
KW
744The base function, e.g., C<isALPHA()>, takes any signed or unsigned value,
745treating it as a code point, and returns a boolean as to whether or not the
746character represented by it is (or on non-ASCII platforms, corresponds to) an
6aff1f14
KW
747ASCII character in the named class based on platform, Unicode, and Perl rules.
748If the input is a number that doesn't fit in an octet, FALSE is returned.
243effed 749
c98722a4 750Variant C<isI<FOO>_A> (e.g., C<isALPHA_A()>) is identical to the base function
550da823
KW
751with no suffix C<"_A">. This variant is used to emphasize by its name that
752only ASCII-range characters can return TRUE.
4b9734bf 753
d60679e1 754Variant C<isI<FOO>_L1> imposes the Latin-1 (or EBCDIC equivalent) character set
4b9734bf
KW
755onto the platform. That is, the code points that are ASCII are unaffected,
756since ASCII is a subset of Latin-1. But the non-ASCII code points are treated
757as if they are Latin-1 characters. For example, C<isWORDCHAR_L1()> will return
758true when called with the code point 0xDF, which is a word character in both
4650c663 759ASCII and EBCDIC (though it represents different characters in each).
d713f9d9
KW
760If the input is a number that doesn't fit in an octet, FALSE is returned.
761(Perl's documentation uses a colloquial definition of Latin-1, to include all
762code points below 256.)
243effed 763
d713f9d9
KW
764Variant C<isI<FOO>_uvchr> is exactly like the C<isI<FOO>_L1> variant, for
765inputs below 256, but if the code point is larger than 255, Unicode rules are
766used to determine if it is in the character class. For example,
d0da05db 767C<isWORDCHAR_uvchr(0x100)> returns TRUE, since 0x100 is LATIN CAPITAL LETTER A
6aff1f14 768WITH MACRON in Unicode, and is a word character.
243effed 769
059703b0
KW
770Variants C<isI<FOO>_utf8> and C<isI<FOO>_utf8_safe> are like C<isI<FOO>_uvchr>,
771but are used for UTF-8 encoded strings. The two forms are different names for
772the same thing. Each call to one of these classifies the first character of
773the string starting at C<p>. The second parameter, C<e>, points to anywhere in
774the string beyond the first character, up to one byte past the end of the
775entire string. Although both variants are identical, the suffix C<_safe> in
776one name emphasizes that it will not attempt to read beyond S<C<e - 1>>,
777provided that the constraint S<C<s E<lt> e>> is true (this is asserted for in
778C<-DDEBUGGING> builds). If the UTF-8 for the input character is malformed in
779some way, the program may croak, or the function may return FALSE, at the
780discretion of the implementation, and subject to change in future releases.
243effed 781
d713f9d9
KW
782Variant C<isI<FOO>_LC> is like the C<isI<FOO>_A> and C<isI<FOO>_L1> variants,
783but the result is based on the current locale, which is what C<LC> in the name
784stands for. If Perl can determine that the current locale is a UTF-8 locale,
785it uses the published Unicode rules; otherwise, it uses the C library function
786that gives the named classification. For example, C<isDIGIT_LC()> when not in
787a UTF-8 locale returns the result of calling C<isdigit()>. FALSE is always
1a83413c
KW
788returned if the input won't fit into an octet. On some platforms where the C
789library function is known to be defective, Perl changes its result to follow
790the POSIX standard's rules.
243effed 791
d713f9d9
KW
792Variant C<isI<FOO>_LC_uvchr> acts exactly like C<isI<FOO>_LC> for inputs less
793than 256, but for larger ones it returns the Unicode classification of the code
794point.
243effed 795
059703b0
KW
796Variants C<isI<FOO>_LC_utf8> and C<isI<FOO>_LC_utf8_safe> are like
797C<isI<FOO>_LC_uvchr>, but are used for UTF-8 encoded strings. The two forms
798are different names for the same thing. Each call to one of these classifies
799the first character of the string starting at C<p>. The second parameter,
800C<e>, points to anywhere in the string beyond the first character, up to one
801byte past the end of the entire string. Although both variants are identical,
802the suffix C<_safe> in one name emphasizes that it will not attempt to read
803beyond S<C<e - 1>>, provided that the constraint S<C<s E<lt> e>> is true (this
804is asserted for in C<-DDEBUGGING> builds). If the UTF-8 for the input
805character is malformed in some way, the program may croak, or the function may
806return FALSE, at the discretion of the implementation, and subject to change in
807future releases.
ccfc67b7 808
6fdd32c3
KW
809=for apidoc Am|bool|isALPHA|UV ch
810=for apidoc_item ||isALPHA_A|UV ch
811=for apidoc_item ||isALPHA_L1|UV ch
812=for apidoc_item ||isALPHA_uvchr|UV ch
fa470d81
KW
813=for apidoc_item ||isALPHA_utf8_safe|U8 * s|U8 * end
814=for apidoc_item ||isALPHA_utf8|U8 * s|U8 * end
6fdd32c3
KW
815=for apidoc_item ||isALPHA_LC|UV ch
816=for apidoc_item ||isALPHA_LC_uvchr|UV ch
fa470d81 817=for apidoc_item ||isALPHA_LC_utf8_safe|U8 * s| U8 *end
d713f9d9
KW
818Returns a boolean indicating whether the specified input is one of C<[A-Za-z]>,
819analogous to C<m/[[:alpha:]]/>.
dcccc8ff 820See the L<top of this section|/Character classification> for an explanation of
fa470d81 821the variants.
8a58bdcf 822
f16858ed
KW
823=cut
824
f1460a66 825Here and below, we add the prototypes of these macros for downstream programs
f16858ed
KW
826that would be interested in them, such as Devel::PPPort
827
6fdd32c3
KW
828=for apidoc Am|bool|isALPHANUMERIC|UV ch
829=for apidoc_item ||isALPHANUMERIC_A|UV ch
830=for apidoc_item ||isALPHANUMERIC_L1|UV ch
831=for apidoc_item ||isALPHANUMERIC_uvchr|UV ch
fa470d81
KW
832=for apidoc_item ||isALPHANUMERIC_utf8_safe|U8 * s|U8 * end
833=for apidoc_item ||isALPHANUMERIC_utf8|U8 * s|U8 * end
6fdd32c3
KW
834=for apidoc_item ||isALPHANUMERIC_LC|UV ch
835=for apidoc_item ||isALPHANUMERIC_LC_uvchr|UV ch
fa470d81 836=for apidoc_item ||isALPHANUMERIC_LC_utf8_safe|U8 * s| U8 *end
6fdd32c3
KW
837=for apidoc_item ||isALNUMC|UV ch
838=for apidoc_item ||isALNUMC_A|UV ch
839=for apidoc_item ||isALNUMC_L1|UV ch
840=for apidoc_item ||isALNUMC_LC|UV ch
841=for apidoc_item ||isALNUMC_LC_uvchr|UV ch
d713f9d9
KW
842Returns a boolean indicating whether the specified character is one of
843C<[A-Za-z0-9]>, analogous to C<m/[[:alnum:]]/>.
dcccc8ff 844See the L<top of this section|/Character classification> for an explanation of
fa470d81 845the variants.
15861f94 846
255b632a
KW
847A (discouraged from use) synonym is C<isALNUMC> (where the C<C> suffix means
848this corresponds to the C language alphanumeric definition). Also
849there are the variants
850C<isALNUMC_A>, C<isALNUMC_L1>
851C<isALNUMC_LC>, and C<isALNUMC_LC_uvchr>.
852
6fdd32c3
KW
853=for apidoc Am|bool|isASCII|UV ch
854=for apidoc_item ||isASCII_A|UV ch
855=for apidoc_item ||isASCII_L1|UV ch
856=for apidoc_item ||isASCII_uvchr|UV ch
fa470d81
KW
857=for apidoc_item ||isASCII_utf8_safe|U8 * s|U8 * end
858=for apidoc_item ||isASCII_utf8|U8 * s|U8 * end
6fdd32c3
KW
859=for apidoc_item ||isASCII_LC|UV ch
860=for apidoc_item ||isASCII_LC_uvchr|UV ch
fa470d81 861=for apidoc_item ||isASCII_LC_utf8_safe|U8 * s| U8 *end
8a58bdcf 862Returns a boolean indicating whether the specified character is one of the 128
243effed 863characters in the ASCII character set, analogous to C<m/[[:ascii:]]/>.
e5ad6aba 864On non-ASCII platforms, it returns TRUE iff this
8a58bdcf
KW
865character corresponds to an ASCII character. Variants C<isASCII_A()> and
866C<isASCII_L1()> are identical to C<isASCII()>.
dcccc8ff 867See the L<top of this section|/Character classification> for an explanation of
fa470d81 868the variants.
059703b0
KW
869Note, however, that some platforms do not have the C library routine
870C<isascii()>. In these cases, the variants whose names contain C<LC> are the
871same as the corresponding ones without.
243effed 872
d98532ea
KW
873Also note, that because all ASCII characters are UTF-8 invariant (meaning they
874have the exact same representation (always a single byte) whether encoded in
875UTF-8 or not), C<isASCII> will give the correct results when called with any
059703b0
KW
876byte in any string encoded or not in UTF-8. And similarly C<isASCII_utf8> and
877C<isASCII_utf8_safe> will work properly on any string encoded or not in UTF-8.
d98532ea 878
6fdd32c3
KW
879=for apidoc Am|bool|isBLANK|UV ch
880=for apidoc_item ||isBLANK_A|UV ch
881=for apidoc_item ||isBLANK_L1|UV ch
882=for apidoc_item ||isBLANK_uvchr|UV ch
fa470d81
KW
883=for apidoc_item ||isBLANK_utf8_safe|U8 * s|U8 * end
884=for apidoc_item ||isBLANK_utf8|U8 * s|U8 * end
6fdd32c3
KW
885=for apidoc_item ||isBLANK_LC|UV ch
886=for apidoc_item ||isBLANK_LC_uvchr|UV ch
fa470d81 887=for apidoc_item ||isBLANK_LC_utf8_safe|U8 * s| U8 *end
243effed 888Returns a boolean indicating whether the specified character is a
6aff1f14 889character considered to be a blank, analogous to C<m/[[:blank:]]/>.
dcccc8ff 890See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
891the variants.
892Note,
da8c1a98
KW
893however, that some platforms do not have the C library routine
894C<isblank()>. In these cases, the variants whose names contain C<LC> are
895the same as the corresponding ones without.
243effed 896
6fdd32c3
KW
897=for apidoc Am|bool|isCNTRL|UV ch
898=for apidoc_item ||isCNTRL_A|UV ch
899=for apidoc_item ||isCNTRL_L1|UV ch
900=for apidoc_item ||isCNTRL_uvchr|UV ch
fa470d81
KW
901=for apidoc_item ||isCNTRL_utf8_safe|U8 * s|U8 * end
902=for apidoc_item ||isCNTRL_utf8|U8 * s|U8 * end
6fdd32c3
KW
903=for apidoc_item ||isCNTRL_LC|UV ch
904=for apidoc_item ||isCNTRL_LC_uvchr|UV ch
fa470d81 905=for apidoc_item ||isCNTRL_LC_utf8_safe|U8 * s| U8 *end
f16858ed 906
243effed 907Returns a boolean indicating whether the specified character is a
6aff1f14 908control character, analogous to C<m/[[:cntrl:]]/>.
dcccc8ff 909See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
910the variants.
911On EBCDIC platforms, you almost always want to use the C<isCNTRL_L1> variant.
912
6fdd32c3
KW
913=for apidoc Am|bool|isDIGIT|UV ch
914=for apidoc_item ||isDIGIT_A|UV ch
915=for apidoc_item ||isDIGIT_L1|UV ch
916=for apidoc_item ||isDIGIT_uvchr|UV ch
fa470d81
KW
917=for apidoc_item ||isDIGIT_utf8_safe|U8 * s|U8 * end
918=for apidoc_item ||isDIGIT_utf8|U8 * s|U8 * end
6fdd32c3
KW
919=for apidoc_item ||isDIGIT_LC|UV ch
920=for apidoc_item ||isDIGIT_LC_uvchr|UV ch
fa470d81
KW
921=for apidoc_item ||isDIGIT_LC_utf8_safe|U8 * s| U8 *end
922
2787a470 923Returns a boolean indicating whether the specified character is a
6aff1f14 924digit, analogous to C<m/[[:digit:]]/>.
8a58bdcf 925Variants C<isDIGIT_A> and C<isDIGIT_L1> are identical to C<isDIGIT>.
dcccc8ff 926See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
927the variants.
928
6fdd32c3
KW
929=for apidoc Am|bool|isGRAPH|UV ch
930=for apidoc_item ||isGRAPH_A|UV ch
931=for apidoc_item ||isGRAPH_L1|UV ch
932=for apidoc_item ||isGRAPH_uvchr|UV ch
fa470d81
KW
933=for apidoc_item ||isGRAPH_utf8_safe|U8 * s|U8 * end
934=for apidoc_item ||isGRAPH_utf8|U8 * s|U8 * end
6fdd32c3
KW
935=for apidoc_item ||isGRAPH_LC|UV ch
936=for apidoc_item ||isGRAPH_LC_uvchr|UV ch
fa470d81 937=for apidoc_item ||isGRAPH_LC_utf8_safe|U8 * s| U8 *end
243effed 938Returns a boolean indicating whether the specified character is a
6aff1f14 939graphic character, analogous to C<m/[[:graph:]]/>.
dcccc8ff 940See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
941the variants.
942
6fdd32c3
KW
943=for apidoc Am|bool|isLOWER|UV ch
944=for apidoc_item ||isLOWER_A|UV ch
945=for apidoc_item ||isLOWER_L1|UV ch
946=for apidoc_item ||isLOWER_uvchr|UV ch
fa470d81
KW
947=for apidoc_item ||isLOWER_utf8_safe|U8 * s|U8 * end
948=for apidoc_item ||isLOWER_utf8|U8 * s|U8 * end
6fdd32c3
KW
949=for apidoc_item ||isLOWER_LC|UV ch
950=for apidoc_item ||isLOWER_LC_uvchr|UV ch
fa470d81 951=for apidoc_item ||isLOWER_LC_utf8_safe|U8 * s| U8 *end
2787a470 952Returns a boolean indicating whether the specified character is a
6aff1f14 953lowercase character, analogous to C<m/[[:lower:]]/>.
dcccc8ff 954See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
955the variants
956
6fdd32c3
KW
957=for apidoc Am|bool|isOCTAL|UV ch
958=for apidoc_item ||isOCTAL_A|UV ch
959=for apidoc_item ||isOCTAL_L1|UV ch
2787a470 960Returns a boolean indicating whether the specified character is an
6aff1f14 961octal digit, [0-7].
243effed
KW
962The only two variants are C<isOCTAL_A> and C<isOCTAL_L1>; each is identical to
963C<isOCTAL>.
964
6fdd32c3
KW
965=for apidoc Am|bool|isPUNCT|UV ch
966=for apidoc_item ||isPUNCT_A|UV ch
967=for apidoc_item ||isPUNCT_L1|UV ch
968=for apidoc_item ||isPUNCT_uvchr|UV ch
fa470d81
KW
969=for apidoc_item ||isPUNCT_utf8_safe|U8 * s|U8 * end
970=for apidoc_item ||isPUNCT_utf8|U8 * s|U8 * end
6fdd32c3
KW
971=for apidoc_item ||isPUNCT_LC|UV ch
972=for apidoc_item ||isPUNCT_LC_uvchr|UV ch
fa470d81 973=for apidoc_item ||isPUNCT_LC_utf8_safe|U8 * s| U8 *end
243effed 974Returns a boolean indicating whether the specified character is a
6aff1f14
KW
975punctuation character, analogous to C<m/[[:punct:]]/>.
976Note that the definition of what is punctuation isn't as
243effed
KW
977straightforward as one might desire. See L<perlrecharclass/POSIX Character
978Classes> for details.
dcccc8ff 979See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
980the variants.
981
6fdd32c3
KW
982=for apidoc Am|bool|isSPACE|UV ch
983=for apidoc_item ||isSPACE_A|UV ch
984=for apidoc_item ||isSPACE_L1|UV ch
985=for apidoc_item ||isSPACE_uvchr|UV ch
fa470d81
KW
986=for apidoc_item ||isSPACE_utf8_safe|U8 * s|U8 * end
987=for apidoc_item ||isSPACE_utf8|U8 * s|U8 * end
6fdd32c3
KW
988=for apidoc_item ||isSPACE_LC|UV ch
989=for apidoc_item ||isSPACE_LC_uvchr|UV ch
fa470d81 990=for apidoc_item ||isSPACE_LC_utf8_safe|U8 * s| U8 *end
2787a470 991Returns a boolean indicating whether the specified character is a
6aff1f14 992whitespace character. This is analogous
398d098a 993to what C<m/\s/> matches in a regular expression. Starting in Perl 5.18
779cf272 994this also matches what C<m/[[:space:]]/> does. Prior to 5.18, only the
398d098a
KW
995locale forms of this macro (the ones with C<LC> in their names) matched
996precisely what C<m/[[:space:]]/> does. In those releases, the only difference,
997in the non-locale variants, was that C<isSPACE()> did not match a vertical tab.
998(See L</isPSXSPC> for a macro that matches a vertical tab in all releases.)
dcccc8ff 999See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1000the variants.
1001
6fdd32c3
KW
1002=for apidoc Am|bool|isPSXSPC|UV ch
1003=for apidoc_item ||isPSXSPC_A|UV ch
1004=for apidoc_item ||isPSXSPC_L1|UV ch
1005=for apidoc_item ||isPSXSPC_uvchr|UV ch
fa470d81
KW
1006=for apidoc_item ||isPSXSPC_utf8_safe|U8 * s|U8 * end
1007=for apidoc_item ||isPSXSPC_utf8|U8 * s|U8 * end
6fdd32c3
KW
1008=for apidoc_item ||isPSXSPC_LC|UV ch
1009=for apidoc_item ||isPSXSPC_LC_uvchr|UV ch
fa470d81 1010=for apidoc_item ||isPSXSPC_LC_utf8_safe|U8 * s| U8 *end
398d098a 1011(short for Posix Space)
779cf272
KW
1012Starting in 5.18, this is identical in all its forms to the
1013corresponding C<isSPACE()> macros.
398d098a
KW
1014The locale forms of this macro are identical to their corresponding
1015C<isSPACE()> forms in all Perl releases. In releases prior to 5.18, the
1016non-locale forms differ from their C<isSPACE()> forms only in that the
1017C<isSPACE()> forms don't match a Vertical Tab, and the C<isPSXSPC()> forms do.
1018Otherwise they are identical. Thus this macro is analogous to what
1019C<m/[[:space:]]/> matches in a regular expression.
dcccc8ff 1020See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1021the variants.
1022
6fdd32c3
KW
1023=for apidoc Am|bool|isUPPER|UV ch
1024=for apidoc_item ||isUPPER_A|UV ch
1025=for apidoc_item ||isUPPER_L1|UV ch
1026=for apidoc_item ||isUPPER_uvchr|UV ch
fa470d81
KW
1027=for apidoc_item ||isUPPER_utf8_safe|U8 * s|U8 * end
1028=for apidoc_item ||isUPPER_utf8|U8 * s|U8 * end
6fdd32c3
KW
1029=for apidoc_item ||isUPPER_LC|UV ch
1030=for apidoc_item ||isUPPER_LC_uvchr|UV ch
fa470d81 1031=for apidoc_item ||isUPPER_LC_utf8_safe|U8 * s| U8 *end
2787a470 1032Returns a boolean indicating whether the specified character is an
6aff1f14 1033uppercase character, analogous to C<m/[[:upper:]]/>.
dcccc8ff 1034See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1035the variants.
1036
6fdd32c3
KW
1037=for apidoc Am|bool|isPRINT|UV ch
1038=for apidoc_item ||isPRINT_A|UV ch
1039=for apidoc_item ||isPRINT_L1|UV ch
1040=for apidoc_item ||isPRINT_uvchr|UV ch
fa470d81
KW
1041=for apidoc_item ||isPRINT_utf8_safe|U8 * s|U8 * end
1042=for apidoc_item ||isPRINT_utf8|U8 * s|U8 * end
6fdd32c3
KW
1043=for apidoc_item ||isPRINT_LC|UV ch
1044=for apidoc_item ||isPRINT_LC_uvchr|UV ch
fa470d81 1045=for apidoc_item ||isPRINT_LC_utf8_safe|U8 * s| U8 *end
8eea39dd 1046Returns a boolean indicating whether the specified character is a
6aff1f14 1047printable character, analogous to C<m/[[:print:]]/>.
dcccc8ff 1048See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1049the variants.
1050
6fdd32c3
KW
1051=for apidoc Am|bool|isWORDCHAR|UV ch
1052=for apidoc_item ||isWORDCHAR_A|UV ch
1053=for apidoc_item ||isWORDCHAR_L1|UV ch
1054=for apidoc_item ||isWORDCHAR_uvchr|UV ch
fa470d81
KW
1055=for apidoc_item ||isWORDCHAR_utf8_safe|U8 * s|U8 * end
1056=for apidoc_item ||isWORDCHAR_utf8|U8 * s|U8 * end
6fdd32c3
KW
1057=for apidoc_item ||isWORDCHAR_LC|UV ch
1058=for apidoc_item ||isWORDCHAR_LC_uvchr|UV ch
fa470d81 1059=for apidoc_item ||isWORDCHAR_LC_utf8_safe|U8 * s| U8 *end
6fdd32c3
KW
1060=for apidoc_item ||isALNUM|UV ch
1061=for apidoc_item ||isALNUM_A|UV ch
1062=for apidoc_item ||isALNUM_LC|UV ch
1063=for apidoc_item ||isALNUM_LC_uvchr|UV ch
243effed
KW
1064Returns a boolean indicating whether the specified character is a character
1065that is a word character, analogous to what C<m/\w/> and C<m/[[:word:]]/> match
1066in a regular expression. A word character is an alphabetic character, a
1067decimal digit, a connecting punctuation character (such as an underscore), or
1068a "mark" character that attaches to one of those (like some sort of accent).
1069C<isALNUM()> is a synonym provided for backward compatibility, even though a
1070word character includes more than the standard C language meaning of
1071alphanumeric.
dcccc8ff 1072See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1073the variants.
1074C<isWORDCHAR_A>, C<isWORDCHAR_L1>, C<isWORDCHAR_uvchr>,
1075C<isWORDCHAR_LC>, C<isWORDCHAR_LC_uvchr>, C<isWORDCHAR_LC_utf8>, and
1076C<isWORDCHAR_LC_utf8_safe> are also as described there, but additionally
1077include the platform's native underscore.
1078
6fdd32c3
KW
1079=for apidoc Am|bool|isXDIGIT|UV ch
1080=for apidoc_item ||isXDIGIT_A|UV ch
1081=for apidoc_item ||isXDIGIT_L1|UV ch
1082=for apidoc_item ||isXDIGIT_uvchr|UV ch
fa470d81
KW
1083=for apidoc_item ||isXDIGIT_utf8_safe|U8 * s|U8 * end
1084=for apidoc_item ||isXDIGIT_utf8|U8 * s|U8 * end
6fdd32c3
KW
1085=for apidoc_item ||isXDIGIT_LC|UV ch
1086=for apidoc_item ||isXDIGIT_LC_uvchr|UV ch
fa470d81 1087=for apidoc_item ||isXDIGIT_LC_utf8_safe|U8 * s| U8 *end
8a58bdcf 1088Returns a boolean indicating whether the specified character is a hexadecimal
243effed
KW
1089digit. In the ASCII range these are C<[0-9A-Fa-f]>. Variants C<isXDIGIT_A()>
1090and C<isXDIGIT_L1()> are identical to C<isXDIGIT()>.
dcccc8ff 1091See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1092the variants.
1093
6fdd32c3
KW
1094=for apidoc Am|bool|isIDFIRST|UV ch
1095=for apidoc_item ||isIDFIRST_A|UV ch
1096=for apidoc_item ||isIDFIRST_L1|UV ch
1097=for apidoc_item ||isIDFIRST_uvchr|UV ch
fa470d81
KW
1098=for apidoc_item ||isIDFIRST_utf8_safe|U8 * s|U8 * end
1099=for apidoc_item ||isIDFIRST_utf8|U8 * s|U8 * end
6fdd32c3
KW
1100=for apidoc_item ||isIDFIRST_LC|UV ch
1101=for apidoc_item ||isIDFIRST_LC_uvchr|UV ch
fa470d81 1102=for apidoc_item ||isIDFIRST_LC_utf8_safe|U8 * s| U8 *end
3c3ecf18
KW
1103Returns a boolean indicating whether the specified character can be the first
1104character of an identifier. This is very close to, but not quite the same as
1105the official Unicode property C<XID_Start>. The difference is that this
1106returns true only if the input character also matches L</isWORDCHAR>.
dcccc8ff 1107See the L<top of this section|/Character classification> for an explanation of
fa470d81
KW
1108the variants.
1109
6fdd32c3
KW
1110=for apidoc Am|bool|isIDCONT|UV ch
1111=for apidoc_item ||isIDCONT_A|UV ch
1112=for apidoc_item ||isIDCONT_L1|UV ch
1113=for apidoc_item ||isIDCONT_uvchr|UV ch
fa470d81
KW
1114=for apidoc_item ||isIDCONT_utf8_safe|U8 * s|U8 * end
1115=for apidoc_item ||isIDCONT_utf8|U8 * s|U8 * end
6fdd32c3
KW
1116=for apidoc_item ||isIDCONT_LC|UV ch
1117=for apidoc_item ||isIDCONT_LC_uvchr|UV ch
fa470d81 1118=for apidoc_item ||isIDCONT_LC_utf8_safe|U8 * s| U8 *end
3c3ecf18
KW
1119Returns a boolean indicating whether the specified character can be the
1120second or succeeding character of an identifier. This is very close to, but
1121not quite the same as the official Unicode property C<XID_Continue>. The
1122difference is that this returns true only if the input character also matches
dcccc8ff 1123L</isWORDCHAR>. See the L<top of this section|/Character classification> for
fa470d81 1124an explanation of the variants.
f16858ed 1125
3f620621 1126=for apidoc_section $numeric
8eea39dd 1127
95a59cab 1128=for apidoc Am|U8|READ_XDIGIT|char str*
243effed 1129Returns the value of an ASCII-range hex digit and advances the string pointer.
95a59cab
YO
1130Behaviour is only well defined when isXDIGIT(*str) is true.
1131
e7c1e6c1 1132=head1 Character case changing
21da7284
KW
1133Perl uses "full" Unicode case mappings. This means that converting a single
1134character to another case may result in a sequence of more than one character.
1135For example, the uppercase of C<E<223>> (LATIN SMALL LETTER SHARP S) is the two
1136character sequence C<SS>. This presents some complications The lowercase of
1137all characters in the range 0..255 is a single character, and thus
1138C<L</toLOWER_L1>> is furnished. But, C<toUPPER_L1> can't exist, as it couldn't
1139return a valid result for all legal inputs. Instead C<L</toUPPER_uvchr>> has
1140an API that does allow every possible legal result to be returned.) Likewise
1141no other function that is crippled by not being able to give the correct
1142results for the full range of possible inputs has been implemented here.
e7c1e6c1 1143
45d6cb5e
KW
1144=for apidoc Am|UV|toUPPER|UV cp
1145=for apidoc_item |UV|toUPPER_A|UV cp
1146=for apidoc_item |UV|toUPPER_uvchr|UV cp|U8* s|STRLEN* lenp
1147=for apidoc_item |UV|toUPPER_utf8|U8* p|U8* e|U8* s|STRLEN* lenp
1148=for apidoc_item |UV|toUPPER_utf8_safe|U8* p|U8* e|U8* s|STRLEN* lenp
1149
1150These all return the uppercase of a character. The differences are what domain
1151they operate on, and whether the input is specified as a code point (those
1152forms with a C<cp> parameter) or as a UTF-8 string (the others). In the latter
1153case, the code point to use is the first one in the buffer of UTF-8 encoded
1154code points, delineated by the arguments S<C<p .. e - 1>>.
1155
1156C<toUPPER> and C<toUPPER_A> are synonyms of each other. They return the
1157uppercase of any lowercase ASCII-range code point. All other inputs are
1158returned unchanged. Since these are macros, the input type may be any integral
1159one, and the output will occupy the same number of bits as the input.
1160
1161There is no C<toUPPER_L1> nor C<toUPPER_LATIN1> as the uppercase of some code
1162points in the 0..255 range is above that range or consists of multiple
1163characters. Instead use C<toUPPER_uvchr>.
1164
1165C<toUPPER_uvchr> returns the uppercase of any Unicode code point. The return
1166value is identical to that of C<toUPPER_A> for input code points in the ASCII
1167range. The uppercase of the vast majority of Unicode code points is the same
1168as the code point itself. For these, and for code points above the legal
1169Unicode maximum, this returns the input code point unchanged. It additionally
1170stores the UTF-8 of the result into the buffer beginning at C<s>, and its
1171length in bytes into C<*lenp>. The caller must have made C<s> large enough to
1172contain at least C<UTF8_MAXBYTES_CASE+1> bytes to avoid possible overflow.
1173
1174NOTE: the uppercase of a code point may be more than one code point. The
1175return value of this function is only the first of these. The entire uppercase
1176is returned in C<s>. To determine if the result is more than a single code
1177point, you can do something like this:
1178
1179 uc = toUPPER_uvchr(cp, s, &len);
1180 if (len > UTF8SKIP(s)) { is multiple code points }
1181 else { is a single code point }
1182
1183C<toUPPER_utf8> and C<toUPPER_utf8_safe> are synonyms of each other. The only
1184difference between these and C<toUPPER_uvchr> is that the source for these is
1185encoded in UTF-8, instead of being a code point. It is passed as a buffer
1186starting at C<p>, with C<e> pointing to one byte beyond its end. The C<p>
1187buffer may certainly contain more than one code point; but only the first one
1188(up through S<C<e - 1>>) is examined. If the UTF-8 for the input character is
1189malformed in some way, the program may croak, or the function may return the
1190REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to
1191change in future releases.
1192
1193=for apidoc Am|UV|toFOLD|UV cp
1194=for apidoc_item |UV|toFOLD_A|UV cp
1195=for apidoc_item |UV|toFOLD_uvchr|UV cp|U8* s|STRLEN* lenp
1196=for apidoc_item |UV|toFOLD_utf8|U8* p|U8* e|U8* s|STRLEN* lenp
1197=for apidoc_item |UV|toFOLD_utf8_safe|U8* p|U8* e|U8* s|STRLEN* lenp
1198
1199These all return the foldcase of a character. "foldcase" is an internal case
1200for C</i> pattern matching. If the foldcase of character A and the foldcase of
1201character B are the same, they match caselessly; otherwise they don't.
1202
1203The differences in the forms are what domain they operate on, and whether the
1204input is specified as a code point (those forms with a C<cp> parameter) or as a
1205UTF-8 string (the others). In the latter case, the code point to use is the
1206first one in the buffer of UTF-8 encoded code points, delineated by the
1207arguments S<C<p .. e - 1>>.
1208
1209C<toFOLD> and C<toFOLD_A> are synonyms of each other. They return the
1210foldcase of any ASCII-range code point. In this range, the foldcase is
1211identical to the lowercase. All other inputs are returned unchanged. Since
1212these are macros, the input type may be any integral one, and the output will
1213occupy the same number of bits as the input.
1214
1215There is no C<toFOLD_L1> nor C<toFOLD_LATIN1> as the foldcase of some code
1216points in the 0..255 range is above that range or consists of multiple
1217characters. Instead use C<toFOLD_uvchr>.
1218
1219C<toFOLD_uvchr> returns the foldcase of any Unicode code point. The return
1220value is identical to that of C<toFOLD_A> for input code points in the ASCII
1221range. The foldcase of the vast majority of Unicode code points is the same
1222as the code point itself. For these, and for code points above the legal
1223Unicode maximum, this returns the input code point unchanged. It additionally
1224stores the UTF-8 of the result into the buffer beginning at C<s>, and its
1225length in bytes into C<*lenp>. The caller must have made C<s> large enough to
1226contain at least C<UTF8_MAXBYTES_CASE+1> bytes to avoid possible overflow.
1227
1228NOTE: the foldcase of a code point may be more than one code point. The
1229return value of this function is only the first of these. The entire foldcase
1230is returned in C<s>. To determine if the result is more than a single code
1231point, you can do something like this:
1232
1233 uc = toFOLD_uvchr(cp, s, &len);
1234 if (len > UTF8SKIP(s)) { is multiple code points }
1235 else { is a single code point }
1236
1237C<toFOLD_utf8> and C<toFOLD_utf8_safe> are synonyms of each other. The only
1238difference between these and C<toFOLD_uvchr> is that the source for these is
1239encoded in UTF-8, instead of being a code point. It is passed as a buffer
1240starting at C<p>, with C<e> pointing to one byte beyond its end. The C<p>
1241buffer may certainly contain more than one code point; but only the first one
1242(up through S<C<e - 1>>) is examined. If the UTF-8 for the input character is
1243malformed in some way, the program may croak, or the function may return the
1244REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to
1245change in future releases.
1f607577 1246
3cb048e5
KW
1247=for apidoc Am|UV|toLOWER|UV cp
1248=for apidoc_item |UV|toLOWER_A|UV cp
1249=for apidoc_item |UV|toLOWER_L1|UV cp
1250=for apidoc_item |UV|toLOWER_LATIN1|UV cp
1251=for apidoc_item |UV|toLOWER_LC|UV cp
1252=for apidoc_item |UV|toLOWER_uvchr|UV cp|U8* s|STRLEN* lenp
1253=for apidoc_item |UV|toLOWER_utf8|U8* p|U8* e|U8* s|STRLEN* lenp
1254=for apidoc_item |UV|toLOWER_utf8_safe|U8* p|U8* e|U8* s|STRLEN* lenp
1255
1256These all return the lowercase of a character. The differences are what domain
1257they operate on, and whether the input is specified as a code point (those
1258forms with a C<cp> parameter) or as a UTF-8 string (the others). In the latter
1259case, the code point to use is the first one in the buffer of UTF-8 encoded
1260code points, delineated by the arguments S<C<p .. e - 1>>.
1261
1262C<toLOWER> and C<toLOWER_A> are synonyms of each other. They return the
1263lowercase of any uppercase ASCII-range code point. All other inputs are
1264returned unchanged. Since these are macros, the input type may be any integral
1265one, and the output will occupy the same number of bits as the input.
1266
1267C<toLOWER_L1> and C<toLOWER_LATIN1> are synonyms of each other. They behave
1268identically as C<toLOWER> for ASCII-range input. But additionally will return
1269the lowercase of any uppercase code point in the entire 0..255 range, assuming
1270a Latin-1 encoding (or the EBCDIC equivalent on such platforms).
1271
1272C<toLOWER_LC> returns the lowercase of the input code point according to the
1273rules of the current POSIX locale. Input code points outside the range 0..255
1274are returned unchanged.
1275
1276C<toLOWER_uvchr> returns the lowercase of any Unicode code point. The return
1277value is identical to that of C<toLOWER_L1> for input code points in the 0..255
1278range. The lowercase of the vast majority of Unicode code points is the same
1279as the code point itself. For these, and for code points above the legal
1280Unicode maximum, this returns the input code point unchanged. It additionally
1281stores the UTF-8 of the result into the buffer beginning at C<s>, and its
1282length in bytes into C<*lenp>. The caller must have made C<s> large enough to
1283contain at least C<UTF8_MAXBYTES_CASE+1> bytes to avoid possible overflow.
1284
1285NOTE: the lowercase of a code point may be more than one code point. The
1286return value of this function is only the first of these. The entire lowercase
1287is returned in C<s>. To determine if the result is more than a single code
1288point, you can do something like this:
1289
1290 uc = toLOWER_uvchr(cp, s, &len);
1291 if (len > UTF8SKIP(s)) { is multiple code points }
1292 else { is a single code point }
1293
1294C<toLOWER_utf8> and C<toLOWER_utf8_safe> are synonyms of each other. The only
1295difference between these and C<toLOWER_uvchr> is that the source for these is
1296encoded in UTF-8, instead of being a code point. It is passed as a buffer
1297starting at C<p>, with C<e> pointing to one byte beyond its end. The C<p>
1298buffer may certainly contain more than one code point; but only the first one
1299(up through S<C<e - 1>>) is examined. If the UTF-8 for the input character is
1300malformed in some way, the program may croak, or the function may return the
1301REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to
1302change in future releases.
1f607577 1303
45d6cb5e
KW
1304=for apidoc Am|UV|toTITLE|UV cp
1305=for apidoc_item |UV|toTITLE_A|UV cp
1306=for apidoc_item |UV|toTITLE_uvchr|UV cp|U8* s|STRLEN* lenp
1307=for apidoc_item |UV|toTITLE_utf8|U8* p|U8* e|U8* s|STRLEN* lenp
1308=for apidoc_item |UV|toTITLE_utf8_safe|U8* p|U8* e|U8* s|STRLEN* lenp
1309
1310These all return the titlecase of a character. The differences are what domain
1311they operate on, and whether the input is specified as a code point (those
1312forms with a C<cp> parameter) or as a UTF-8 string (the others). In the latter
1313case, the code point to use is the first one in the buffer of UTF-8 encoded
1314code points, delineated by the arguments S<C<p .. e - 1>>.
1315
1316C<toTITLE> and C<toTITLE_A> are synonyms of each other. They return the
1317titlecase of any lowercase ASCII-range code point. In this range, the
1318titlecase is identical to the uppercase. All other inputs are returned
1319unchanged. Since these are macros, the input type may be any integral one, and
1320the output will occupy the same number of bits as the input.
1321
1322There is no C<toTITLE_L1> nor C<toTITLE_LATIN1> as the titlecase of some code
1323points in the 0..255 range is above that range or consists of multiple
1324characters. Instead use C<toTITLE_uvchr>.
1325
1326C<toTITLE_uvchr> returns the titlecase of any Unicode code point. The return
1327value is identical to that of C<toTITLE_A> for input code points in the ASCII
1328range. The titlecase of the vast majority of Unicode code points is the same
1329as the code point itself. For these, and for code points above the legal
1330Unicode maximum, this returns the input code point unchanged. It additionally
1331stores the UTF-8 of the result into the buffer beginning at C<s>, and its
1332length in bytes into C<*lenp>. The caller must have made C<s> large enough to
1333contain at least C<UTF8_MAXBYTES_CASE+1> bytes to avoid possible overflow.
1334
1335NOTE: the titlecase of a code point may be more than one code point. The
1336return value of this function is only the first of these. The entire titlecase
1337is returned in C<s>. To determine if the result is more than a single code
1338point, you can do something like this:
1339
1340 uc = toTITLE_uvchr(cp, s, &len);
1341 if (len > UTF8SKIP(s)) { is multiple code points }
1342 else { is a single code point }
1343
1344C<toTITLE_utf8> and C<toTITLE_utf8_safe> are synonyms of each other. The only
1345difference between these and C<toTITLE_uvchr> is that the source for these is
1346encoded in UTF-8, instead of being a code point. It is passed as a buffer
1347starting at C<p>, with C<e> pointing to one byte beyond its end. The C<p>
1348buffer may certainly contain more than one code point; but only the first one
1349(up through S<C<e - 1>>) is examined. If the UTF-8 for the input character is
1350malformed in some way, the program may croak, or the function may return the
1351REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to
1352change in future releases.
1f607577 1353
954c1994 1354=cut
353c9b6f 1355
d0da05db 1356XXX Still undocumented isVERTWS_uvchr and _utf8; it's unclear what their names
1e222e4f
KW
1357really should be. Also toUPPER_LC and toFOLD_LC, which are subject to change,
1358and aren't general purpose as they don't work on U+DF, and assert against that.
243effed 1359
8a58bdcf 1360Note that these macros are repeated in Devel::PPPort, so should also be
62fa66b6
KW
1361patched there. The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
1362
954c1994
GS
1363*/
1364
8f5283f4
KW
1365/*
1366 void below because that's the best fit, and works for Devel::PPPort
3f620621 1367=for apidoc_section $integer
545bca17 1368=for apidoc AyT||WIDEST_UTYPE
8f5283f4
KW
1369
1370Yields the widest unsigned integer type on the platform, currently either
326c768d 1371C<U32> or C<U64>. This can be used in declarations such as
8f5283f4
KW
1372
1373 WIDEST_UTYPE my_uv;
1374
1375or casts
1376
1377 my_uv = (WIDEST_UTYPE) val;
1378
1379=cut
1380
1381*/
bbe73871 1382#define WIDEST_UTYPE PERL_UINTMAX_T
7c062697 1383
de40ad3f
KW
1384/* Where there could be some confusion, use this as a static assert in macros
1385 * to make sure that a parameter isn't a pointer. But some compilers can't
1386 * handle this. The only one known so far that doesn't is gcc 3.3.6; the check
1387 * below isn't thorough for such an old compiler, so may have to be revised if
1388 * experience so dictates. */
1389#if ! PERL_IS_GCC || PERL_GCC_VERSION_GT(3,3,6)
1390# define ASSERT_NOT_PTR(x) ((x) | 0)
1391#else
1392# define ASSERT_NOT_PTR(x) (x)
1393#endif
296969d3 1394
08e4e7bf 1395/* Likewise, this is effectively a static assert to be used to guarantee the
43131e10
KW
1396 * parameter is a pointer
1397 *
1398 * NOT suitable for void*
1399 */
08e4e7bf
KW
1400#define ASSERT_IS_PTR(x) (__ASSERT_(sizeof(*(x))) (x))
1401
3912bc88
KW
1402/* FITS_IN_8_BITS(c) returns true if c doesn't have a bit set other than in
1403 * the lower 8. It is designed to be hopefully bomb-proof, making sure that no
1404 * bits of information are lost even on a 64-bit machine, but to get the
1405 * compiler to optimize it out if possible. This is because Configure makes
1406 * sure that the machine has an 8-bit byte, so if c is stored in a byte, the
1407 * sizeof() guarantees that this evaluates to a constant true at compile time.
7e75d1a1
JH
1408 *
1409 * For Coverity, be always true, because otherwise Coverity thinks
1410 * it finds several expressions that are always true, independent
1411 * of operands. Well, they are, but that is kind of the point.
220c71bf 1412 */
7e75d1a1 1413#ifndef __COVERITY__
231a6d16
KW
1414 /* The '| 0' part in ASSERT_NOT_PTR ensures a compiler error if c is not
1415 * integer (like e.g., a pointer) */
1416# define FITS_IN_8_BITS(c) ( (sizeof(c) == 1) \
1417 || (((WIDEST_UTYPE) ASSERT_NOT_PTR(c)) >> 8) == 0)
7e75d1a1 1418#else
9555181b 1419# define FITS_IN_8_BITS(c) (1)
7e75d1a1 1420#endif
cf301eb7 1421
45f4bb73 1422/* Returns true if l <= c <= (l + n), where 'l' and 'n' are non-negative
833b0f46 1423 * Written this way so that after optimization, only one conditional test is
76d3ad4c
KW
1424 * needed. (The NV casts stop any warnings about comparison always being true
1425 * if called with an unsigned. The cast preserves the sign, which is all we
1426 * care about.) */
92a0bb24
KW
1427#define withinCOUNT(c, l, n) (__ASSERT_((NV) (l) >= 0) \
1428 __ASSERT_((NV) (n) >= 0) \
1429 withinCOUNT_KNOWN_VALID_((c), (l), (n)))
1430
1431/* For internal use only, this can be used in places where it is known that the
1432 * parameters to withinCOUNT() are valid, to avoid the asserts. For example,
1433 * inRANGE() below, calls this several times, but does all the necessary
1434 * asserts itself, once. The reason that this is necessary is that the
1435 * duplicate asserts were exceeding the internal limits of some compilers */
1436#define withinCOUNT_KNOWN_VALID_(c, l, n) \
296969d3
KW
1437 ((((WIDEST_UTYPE) (c)) - ASSERT_NOT_PTR(l)) \
1438 <= ((WIDEST_UTYPE) ASSERT_NOT_PTR(n)))
833b0f46 1439
94250c4f
KW
1440/* Returns true if c is in the range l..u, where 'l' is non-negative
1441 * Written this way so that after optimization, only one conditional test is
4758c20d 1442 * needed. */
92a0bb24
KW
1443#define inRANGE(c, l, u) (__ASSERT_((NV) (l) >= 0) __ASSERT_((u) >= (l)) \
1444 ( (sizeof(c) == sizeof(U8)) ? inRANGE_helper_(U8, (c), (l), ((u))) \
1442da54 1445 : (sizeof(c) == sizeof(U16)) ? inRANGE_helper_(U16,(c), (l), ((u))) \
92a0bb24
KW
1446 : (sizeof(c) == sizeof(U32)) ? inRANGE_helper_(U32,(c), (l), ((u))) \
1447 : (__ASSERT_(sizeof(c) == sizeof(WIDEST_UTYPE)) \
1448 inRANGE_helper_(WIDEST_UTYPE,(c), (l), ((u))))))
1449
1450/* For internal use, this is used by machine-generated code which generates
1451 * known valid calls, with a known sizeof(). This avoids the extra code and
1452 * asserts that were exceeding internal limits of some compilers. */
1453#define inRANGE_helper_(cast, c, l, u) \
1454 withinCOUNT_KNOWN_VALID_(((cast) (c)), (l), ((u) - (l)))
305fe86e 1455
41f43cc2 1456#ifdef EBCDIC
b6340bd0 1457# ifndef _ALL_SOURCE
0852beac
KW
1458 /* The native libc isascii() et.al. functions return the wrong results
1459 * on at least z/OS unless this is defined. */
b6340bd0
KW
1460# error _ALL_SOURCE should probably be defined
1461# endif
41f43cc2 1462#else
0852beac
KW
1463 /* There is a simple definition of ASCII for ASCII platforms. But the
1464 * EBCDIC one isn't so simple, so is defined using table look-up like the
9c903d59 1465 * other macros below.
3f3c579d
KW
1466 *
1467 * The cast here is used instead of '(c) >= 0', because some compilers emit
1468 * a warning that that test is always true when the parameter is an
1469 * unsigned type. khw supposes that it could be written as
1470 * && ((c) == '\0' || (c) > 0)
1471 * to avoid the message, but the cast will likely avoid extra branches even
296969d3
KW
1472 * with stupid compilers. */
1473# define isASCII(c) (((WIDEST_UTYPE) ASSERT_NOT_PTR(c)) < 128)
41f43cc2
KW
1474#endif
1475
38694112
KW
1476/* Take the eight possible bit patterns of the lower 3 bits and you get the
1477 * lower 3 bits of the 8 octal digits, in both ASCII and EBCDIC, so those bits
1478 * can be ignored. If the rest match '0', we have an octal */
296969d3 1479#define isOCTAL_A(c) ((((WIDEST_UTYPE) ASSERT_NOT_PTR(c)) & ~7) == '0')
c2da0b36 1480
9fb1bf9d 1481#ifdef H_PERL /* If have access to perl.h, lookup in its table */
f4cdb42c 1482
a500dc72
KW
1483/* Character class numbers. For internal core Perl use only. The ones less
1484 * than 32 are used in PL_charclass[] and the ones up through the one that
1485 * corresponds to <_HIGHEST_REGCOMP_DOT_H_SYNC> are used by regcomp.h and
1486 * related files. PL_charclass ones use names used in l1_char_class_tab.h but
1487 * their actual definitions are here. If that file has a name not used here,
1488 * it won't compile.
1709d539
KW
1489 *
1490 * The first group of these is ordered in what I (khw) estimate to be the
31c7f561 1491 * frequency of their use. This gives a slight edge to exiting a loop earlier
58a3ba2c
KW
1492 * (in reginclass() in regexec.c). Except \v should be last, as it isn't a
1493 * real Posix character class, and some (small) inefficiencies in regular
1494 * expression handling would be introduced by putting it in the middle of those
1495 * that are. Also, cntrl and ascii come after the others as it may be useful
1496 * to group these which have no members that match above Latin1, (or above
1497 * ASCII in the latter case) */
1498
1709d539
KW
1499# define _CC_WORDCHAR 0 /* \w and [:word:] */
1500# define _CC_DIGIT 1 /* \d and [:digit:] */
1501# define _CC_ALPHA 2 /* [:alpha:] */
1502# define _CC_LOWER 3 /* [:lower:] */
1503# define _CC_UPPER 4 /* [:upper:] */
1504# define _CC_PUNCT 5 /* [:punct:] */
1505# define _CC_PRINT 6 /* [:print:] */
15861f94 1506# define _CC_ALPHANUMERIC 7 /* [:alnum:] */
1709d539 1507# define _CC_GRAPH 8 /* [:graph:] */
359b005e 1508# define _CC_CASED 9 /* [:lower:] or [:upper:] under /i */
779cf272 1509# define _CC_SPACE 10 /* \s, [:space:] */
b0d691b2
KW
1510# define _CC_BLANK 11 /* [:blank:] */
1511# define _CC_XDIGIT 12 /* [:xdigit:] */
779cf272
KW
1512# define _CC_CNTRL 13 /* [:cntrl:] */
1513# define _CC_ASCII 14 /* [:ascii:] */
1514# define _CC_VERTSPACE 15 /* \v */
1709d539 1515
a0947d7b
KW
1516# define _HIGHEST_REGCOMP_DOT_H_SYNC _CC_VERTSPACE
1517
1709d539 1518/* The members of the third group below do not need to be coordinated with data
3ffc8c70 1519 * structures in regcomp.[ch] and regexec.c. */
779cf272
KW
1520# define _CC_IDFIRST 16
1521# define _CC_CHARNAME_CONT 17
1522# define _CC_NONLATIN1_FOLD 18
1523# define _CC_NONLATIN1_SIMPLE_FOLD 19
1524# define _CC_QUOTEMETA 20
1525# define _CC_NON_FINAL_FOLD 21
1526# define _CC_IS_IN_SOME_FOLD 22
2ae9030c
KW
1527# define _CC_BINDIGIT 23
1528# define _CC_OCTDIGIT 24
1529# define _CC_MNEMONIC_CNTRL 25
073c22b3 1530
51b58dba 1531/* Unused: 26-31
f4cdb42c
KW
1532 * If more bits are needed, one could add a second word for non-64bit
1533 * QUAD_IS_INT systems, using some #ifdefs to distinguish between having a 2nd
37ede926
KW
1534 * word or not. The IS_IN_SOME_FOLD bit is the most easily expendable, as it
1535 * is used only for optimization (as of this writing), and differs in the
1536 * Latin1 range from the ALPHA bit only in two relatively unimportant
a500dc72 1537 * characters: the masculine and feminine ordinal indicators, so removing it
073c22b3
KW
1538 * would just cause /i regexes which match them to run less efficiently.
1539 * Similarly the EBCDIC-only bits are used just for speed, and could be
1540 * replaced by other means */
96ac0975 1541
3a371f2f
KW
1542#if defined(PERL_CORE) || defined(PERL_EXT)
1543/* An enum version of the character class numbers, to help compilers
1544 * optimize */
1545typedef enum {
3a371f2f 1546 _CC_ENUM_ALPHA = _CC_ALPHA,
e8d596e0
KW
1547 _CC_ENUM_ALPHANUMERIC = _CC_ALPHANUMERIC,
1548 _CC_ENUM_ASCII = _CC_ASCII,
1549 _CC_ENUM_BLANK = _CC_BLANK,
b0d691b2 1550 _CC_ENUM_CASED = _CC_CASED,
e8d596e0 1551 _CC_ENUM_CNTRL = _CC_CNTRL,
3a371f2f
KW
1552 _CC_ENUM_DIGIT = _CC_DIGIT,
1553 _CC_ENUM_GRAPH = _CC_GRAPH,
1554 _CC_ENUM_LOWER = _CC_LOWER,
1555 _CC_ENUM_PRINT = _CC_PRINT,
1556 _CC_ENUM_PUNCT = _CC_PUNCT,
e8d596e0 1557 _CC_ENUM_SPACE = _CC_SPACE,
3a371f2f 1558 _CC_ENUM_UPPER = _CC_UPPER,
e8d596e0 1559 _CC_ENUM_VERTSPACE = _CC_VERTSPACE,
3a371f2f 1560 _CC_ENUM_WORDCHAR = _CC_WORDCHAR,
e8d596e0 1561 _CC_ENUM_XDIGIT = _CC_XDIGIT
3a371f2f
KW
1562} _char_class_number;
1563#endif
1564
86f72d56 1565#define POSIX_CC_COUNT (_HIGHEST_REGCOMP_DOT_H_SYNC + 1)
63c61c3f 1566
6635f04f 1567START_EXTERN_C
96ac0975
NC
1568# ifdef DOINIT
1569EXTCONST U32 PL_charclass[] = {
1570# include "l1_char_class_tab.h"
1571};
1572
1573# else /* ! DOINIT */
1574EXTCONST U32 PL_charclass[];
1575# endif
6635f04f 1576END_EXTERN_C
96ac0975 1577
265c1f46 1578 /* The 1U keeps Solaris from griping when shifting sets the uppermost bit */
430b7c70 1579# define _CC_mask(classnum) (1U << (classnum))
4650c663
KW
1580
1581 /* For internal core Perl use only: the base macro for defining macros like
1582 * isALPHA */
6eb62d23 1583# define generic_isCC_(c, classnum) cBOOL(FITS_IN_8_BITS(c) \
f4cd282c 1584 && (PL_charclass[(U8) (c)] & _CC_mask(classnum)))
4eeeb416 1585
f4cdb42c
KW
1586 /* The mask for the _A versions of the macros; it just adds in the bit for
1587 * ASCII. */
1588# define _CC_mask_A(classnum) (_CC_mask(classnum) | _CC_mask(_CC_ASCII))
1589
4650c663
KW
1590 /* For internal core Perl use only: the base macro for defining macros like
1591 * isALPHA_A. The foo_A version makes sure that both the desired bit and
1592 * the ASCII bit are present */
6eb62d23 1593# define generic_isCC_A_(c, classnum) (FITS_IN_8_BITS(c) \
b7d90381
KW
1594 && ((PL_charclass[(U8) (c)] & _CC_mask_A(classnum)) \
1595 == _CC_mask_A(classnum)))
f4cdb42c 1596
26c1d9d8
KW
1597/* On ASCII platforms certain classes form a single range. It's faster to
1598 * special case these. isDIGIT is a single range on all platforms */
b877c1ff 1599# ifdef EBCDIC
6eb62d23
KW
1600# define isALPHA_A(c) generic_isCC_A_(c, _CC_ALPHA)
1601# define isGRAPH_A(c) generic_isCC_A_(c, _CC_GRAPH)
1602# define isLOWER_A(c) generic_isCC_A_(c, _CC_LOWER)
1603# define isPRINT_A(c) generic_isCC_A_(c, _CC_PRINT)
1604# define isUPPER_A(c) generic_isCC_A_(c, _CC_UPPER)
b877c1ff 1605# else
26c1d9d8 1606 /* By folding the upper and lowercase, we can use a single range */
b877c1ff 1607# define isALPHA_A(c) inRANGE((~('A' ^ 'a') & (c)), 'A', 'Z')
26c1d9d8 1608# define isGRAPH_A(c) inRANGE(c, ' ' + 1, 0x7e)
b877c1ff
KW
1609# define isLOWER_A(c) inRANGE(c, 'a', 'z')
1610# define isPRINT_A(c) inRANGE(c, ' ', 0x7e)
1611# define isUPPER_A(c) inRANGE(c, 'A', 'Z')
1612# endif
6eb62d23
KW
1613# define isALPHANUMERIC_A(c) generic_isCC_A_(c, _CC_ALPHANUMERIC)
1614# define isBLANK_A(c) generic_isCC_A_(c, _CC_BLANK)
1615# define isCNTRL_A(c) generic_isCC_A_(c, _CC_CNTRL)
b877c1ff 1616# define isDIGIT_A(c) inRANGE(c, '0', '9')
6eb62d23
KW
1617# define isPUNCT_A(c) generic_isCC_A_(c, _CC_PUNCT)
1618# define isSPACE_A(c) generic_isCC_A_(c, _CC_SPACE)
1619# define isWORDCHAR_A(c) generic_isCC_A_(c, _CC_WORDCHAR)
1620# define isXDIGIT_A(c) generic_isCC_(c, _CC_XDIGIT) /* No non-ASCII xdigits
b7d90381 1621 */
6eb62d23
KW
1622# define isIDFIRST_A(c) generic_isCC_A_(c, _CC_IDFIRST)
1623# define isALPHA_L1(c) generic_isCC_(c, _CC_ALPHA)
1624# define isALPHANUMERIC_L1(c) generic_isCC_(c, _CC_ALPHANUMERIC)
1625# define isBLANK_L1(c) generic_isCC_(c, _CC_BLANK)
3ded5eb0
KW
1626
1627 /* continuation character for legal NAME in \N{NAME} */
6eb62d23 1628# define isCHARNAME_CONT(c) generic_isCC_(c, _CC_CHARNAME_CONT)
3ded5eb0 1629
6eb62d23
KW
1630# define isCNTRL_L1(c) generic_isCC_(c, _CC_CNTRL)
1631# define isGRAPH_L1(c) generic_isCC_(c, _CC_GRAPH)
1632# define isLOWER_L1(c) generic_isCC_(c, _CC_LOWER)
1633# define isPRINT_L1(c) generic_isCC_(c, _CC_PRINT)
b7d90381 1634# define isPSXSPC_L1(c) isSPACE_L1(c)
6eb62d23
KW
1635# define isPUNCT_L1(c) generic_isCC_(c, _CC_PUNCT)
1636# define isSPACE_L1(c) generic_isCC_(c, _CC_SPACE)
1637# define isUPPER_L1(c) generic_isCC_(c, _CC_UPPER)
1638# define isWORDCHAR_L1(c) generic_isCC_(c, _CC_WORDCHAR)
1639# define isIDFIRST_L1(c) generic_isCC_(c, _CC_IDFIRST)
f4cdb42c 1640
0852beac 1641# ifdef EBCDIC
6eb62d23 1642# define isASCII(c) generic_isCC_(c, _CC_ASCII)
0852beac
KW
1643# endif
1644
f12c0118 1645 /* Participates in a single-character fold with a character above 255 */
c62fdeb7
KW
1646# if defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
1647# define HAS_NONLATIN1_SIMPLE_FOLD_CLOSURE(c) \
1648 (( ! cBOOL(FITS_IN_8_BITS(c))) \
1649 || (PL_charclass[(U8) (c)] & _CC_mask(_CC_NONLATIN1_SIMPLE_FOLD)))
1650
6eb62d23
KW
1651# define IS_NON_FINAL_FOLD(c) generic_isCC_(c, _CC_NON_FINAL_FOLD)
1652# define IS_IN_SOME_FOLD_L1(c) generic_isCC_(c, _CC_IS_IN_SOME_FOLD)
c62fdeb7 1653# endif
f12c0118
KW
1654
1655 /* Like the above, but also can be part of a multi-char fold */
c62fdeb7
KW
1656# define HAS_NONLATIN1_FOLD_CLOSURE(c) \
1657 ( (! cBOOL(FITS_IN_8_BITS(c))) \
1658 || (PL_charclass[(U8) (c)] & _CC_mask(_CC_NONLATIN1_FOLD)))
430b7c70 1659
6eb62d23 1660# define _isQUOTEMETA(c) generic_isCC_(c, _CC_QUOTEMETA)
5e6ebb12
KW
1661
1662/* is c a control character for which we have a mnemonic? */
1663# if defined(PERL_CORE) || defined(PERL_EXT)
6eb62d23 1664# define isMNEMONIC_CNTRL(c) generic_isCC_(c, _CC_MNEMONIC_CNTRL)
5e6ebb12 1665# endif
687c8d01 1666#else /* else we don't have perl.h H_PERL */
3ded5eb0
KW
1667
1668 /* If we don't have perl.h, we are compiling a utility program. Below we
1669 * hard-code various macro definitions that wouldn't otherwise be available
fc273927 1670 * to it. Most are coded based on first principles. These are written to
74665a89 1671 * avoid EBCDIC vs. ASCII #ifdef's as much as possible. */
182c4ace 1672# define isDIGIT_A(c) inRANGE(c, '0', '9')
0852beac 1673# define isBLANK_A(c) ((c) == ' ' || (c) == '\t')
74665a89
KW
1674# define isSPACE_A(c) (isBLANK_A(c) \
1675 || (c) == '\n' \
1676 || (c) == '\r' \
1677 || (c) == '\v' \
0852beac 1678 || (c) == '\f')
74665a89
KW
1679 /* On EBCDIC, there are gaps between 'i' and 'j'; 'r' and 's'. Same for
1680 * uppercase. The tests for those aren't necessary on ASCII, but hurt only
1681 * performance (if optimization isn't on), and allow the same code to be
1682 * used for both platform types */
182c4ace
KW
1683# define isLOWER_A(c) inRANGE((c), 'a', 'i') \
1684 || inRANGE((c), 'j', 'r') \
1685 || inRANGE((c), 's', 'z')
1686# define isUPPER_A(c) inRANGE((c), 'A', 'I') \
1687 || inRANGE((c), 'J', 'R') \
1688 || inRANGE((c), 'S', 'Z')
a4d7a999
KW
1689# define isALPHA_A(c) (isUPPER_A(c) || isLOWER_A(c))
1690# define isALPHANUMERIC_A(c) (isALPHA_A(c) || isDIGIT_A(c))
3ded5eb0 1691# define isWORDCHAR_A(c) (isALPHANUMERIC_A(c) || (c) == '_')
0852beac 1692# define isIDFIRST_A(c) (isALPHA_A(c) || (c) == '_')
182c4ace
KW
1693# define isXDIGIT_A(c) ( isDIGIT_A(c) \
1694 || inRANGE((c), 'a', 'f') \
1695 || inRANGE((c), 'A', 'F')
74665a89
KW
1696# define isPUNCT_A(c) ((c) == '-' || (c) == '!' || (c) == '"' \
1697 || (c) == '#' || (c) == '$' || (c) == '%' \
1698 || (c) == '&' || (c) == '\'' || (c) == '(' \
1699 || (c) == ')' || (c) == '*' || (c) == '+' \
1700 || (c) == ',' || (c) == '.' || (c) == '/' \
1701 || (c) == ':' || (c) == ';' || (c) == '<' \
1702 || (c) == '=' || (c) == '>' || (c) == '?' \
1703 || (c) == '@' || (c) == '[' || (c) == '\\' \
1704 || (c) == ']' || (c) == '^' || (c) == '_' \
1705 || (c) == '`' || (c) == '{' || (c) == '|' \
1706 || (c) == '}' || (c) == '~')
1707# define isGRAPH_A(c) (isALPHANUMERIC_A(c) || isPUNCT_A(c))
1708# define isPRINT_A(c) (isGRAPH_A(c) || (c) == ' ')
3ded5eb0 1709
0852beac 1710# ifdef EBCDIC
74665a89
KW
1711 /* The below is accurate for the 3 EBCDIC code pages traditionally
1712 * supported by perl. The only difference between them in the controls
1713 * is the position of \n, and that is represented symbolically below */
1714# define isCNTRL_A(c) ((c) == '\0' || (c) == '\a' || (c) == '\b' \
1715 || (c) == '\f' || (c) == '\n' || (c) == '\r' \
1716 || (c) == '\t' || (c) == '\v' \
182c4ace 1717 || inRANGE((c), 1, 3) /* SOH, STX, ETX */ \
8ec0a736 1718 || (c) == 7F /* U+7F DEL */ \
182c4ace
KW
1719 || inRANGE((c), 0x0E, 0x13) /* SO SI DLE \
1720 DC[1-3] */ \
74665a89
KW
1721 || (c) == 0x18 /* U+18 CAN */ \
1722 || (c) == 0x19 /* U+19 EOM */ \
182c4ace 1723 || inRANGE((c), 0x1C, 0x1F) /* [FGRU]S */ \
74665a89
KW
1724 || (c) == 0x26 /* U+17 ETB */ \
1725 || (c) == 0x27 /* U+1B ESC */ \
1726 || (c) == 0x2D /* U+05 ENQ */ \
1727 || (c) == 0x2E /* U+06 ACK */ \
1728 || (c) == 0x32 /* U+16 SYN */ \
1729 || (c) == 0x37 /* U+04 EOT */ \
1730 || (c) == 0x3C /* U+14 DC4 */ \
1731 || (c) == 0x3D /* U+15 NAK */ \
1732 || (c) == 0x3F)/* U+1A SUB */
0852beac 1733# define isASCII(c) (isCNTRL_A(c) || isPRINT_A(c))
74665a89
KW
1734# else /* isASCII is already defined for ASCII platforms, so can use that to
1735 define isCNTRL */
1736# define isCNTRL_A(c) (isASCII(c) && ! isPRINT_A(c))
0852beac
KW
1737# endif
1738
3ffc8c70 1739 /* The _L1 macros may be unnecessary for the utilities; I (khw) added them
caa94d35
KW
1740 * during debugging, and it seems best to keep them. We may be called
1741 * without NATIVE_TO_LATIN1 being defined. On ASCII platforms, it doesn't
1742 * do anything anyway, so make it not a problem */
1743# if ! defined(EBCDIC) && ! defined(NATIVE_TO_LATIN1)
1744# define NATIVE_TO_LATIN1(ch) (ch)
1745# endif
3ded5eb0
KW
1746# define isALPHA_L1(c) (isUPPER_L1(c) || isLOWER_L1(c))
1747# define isALPHANUMERIC_L1(c) (isALPHA_L1(c) || isDIGIT_A(c))
1748# define isBLANK_L1(c) (isBLANK_A(c) \
1749 || (FITS_IN_8_BITS(c) \
1750 && NATIVE_TO_LATIN1((U8) c) == 0xA0))
1751# define isCNTRL_L1(c) (FITS_IN_8_BITS(c) && (! isPRINT_L1(c)))
1752# define isGRAPH_L1(c) (isPRINT_L1(c) && (! isBLANK_L1(c)))
1753# define isLOWER_L1(c) (isLOWER_A(c) \
1754 || (FITS_IN_8_BITS(c) \
ae683a5f 1755 && (( NATIVE_TO_LATIN1((U8) c) >= 0xDF \
3ded5eb0
KW
1756 && NATIVE_TO_LATIN1((U8) c) != 0xF7) \
1757 || NATIVE_TO_LATIN1((U8) c) == 0xAA \
1758 || NATIVE_TO_LATIN1((U8) c) == 0xBA \
1759 || NATIVE_TO_LATIN1((U8) c) == 0xB5)))
1760# define isPRINT_L1(c) (isPRINT_A(c) \
1761 || (FITS_IN_8_BITS(c) \
1762 && NATIVE_TO_LATIN1((U8) c) >= 0xA0))
3ded5eb0
KW
1763# define isPUNCT_L1(c) (isPUNCT_A(c) \
1764 || (FITS_IN_8_BITS(c) \
ae683a5f 1765 && ( NATIVE_TO_LATIN1((U8) c) == 0xA1 \
3ded5eb0
KW
1766 || NATIVE_TO_LATIN1((U8) c) == 0xA7 \
1767 || NATIVE_TO_LATIN1((U8) c) == 0xAB \
1768 || NATIVE_TO_LATIN1((U8) c) == 0xB6 \
1769 || NATIVE_TO_LATIN1((U8) c) == 0xB7 \
1770 || NATIVE_TO_LATIN1((U8) c) == 0xBB \
1771 || NATIVE_TO_LATIN1((U8) c) == 0xBF)))
1772# define isSPACE_L1(c) (isSPACE_A(c) \
1773 || (FITS_IN_8_BITS(c) \
ae683a5f 1774 && ( NATIVE_TO_LATIN1((U8) c) == 0x85 \
3ded5eb0
KW
1775 || NATIVE_TO_LATIN1((U8) c) == 0xA0)))
1776# define isUPPER_L1(c) (isUPPER_A(c) \
1777 || (FITS_IN_8_BITS(c) \
182c4ace
KW
1778 && ( IN_RANGE(NATIVE_TO_LATIN1((U8) c), \
1779 0xC0, 0xDE) \
3ded5eb0
KW
1780 && NATIVE_TO_LATIN1((U8) c) != 0xD7)))
1781# define isWORDCHAR_L1(c) (isIDFIRST_L1(c) || isDIGIT_A(c))
1782# define isIDFIRST_L1(c) (isALPHA_L1(c) || NATIVE_TO_LATIN1(c) == '_')
1783# define isCHARNAME_CONT(c) (isWORDCHAR_L1(c) \
1784 || isBLANK_L1(c) \
1785 || (c) == '-' \
1786 || (c) == '(' \
1787 || (c) == ')')
1788 /* The following are not fully accurate in the above-ASCII range. I (khw)
1789 * don't think it's necessary to be so for the purposes where this gets
1790 * compiled */
6eb62d23 1791# define isQUOTEMETA_(c) (FITS_IN_8_BITS(c) && ! isWORDCHAR_L1(c))
3ded5eb0
KW
1792# define _IS_IN_SOME_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) isALPHA_L1(c)
1793
1794 /* And these aren't accurate at all. They are useful only for above
1795 * Latin1, which utilities and bootstrapping don't deal with */
1796# define _IS_NON_FINAL_FOLD_ONLY_FOR_USE_BY_REGCOMP_DOT_C(c) 0
6838b41e 1797# define _HAS_NONLATIN1_SIMPLE_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(c) 0
3ded5eb0
KW
1798# define _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(c) 0
1799
1800 /* Many of the macros later in this file are defined in terms of these. By
1801 * implementing them with a function, which converts the class number into
1802 * a call to the desired macro, all of the later ones work. However, that
1803 * function won't be actually defined when building a utility program (no
1804 * perl.h), and so a compiler error will be generated if one is attempted
1805 * to be used. And the above-Latin1 code points require Unicode tables to
1806 * be present, something unlikely to be the case when bootstrapping */
6eb62d23 1807# define generic_isCC_(c, classnum) \
3ded5eb0 1808 (FITS_IN_8_BITS(c) && S_bootstrap_ctype((U8) (c), (classnum), TRUE))
6eb62d23 1809# define generic_isCC_A_(c, classnum) \
3ded5eb0 1810 (FITS_IN_8_BITS(c) && S_bootstrap_ctype((U8) (c), (classnum), FALSE))
687c8d01 1811#endif /* End of no perl.h H_PERL */
8a58bdcf 1812
e66b99e9
KW
1813#define isALPHANUMERIC(c) isALPHANUMERIC_A(c)
1814#define isALPHA(c) isALPHA_A(c)
0852beac
KW
1815#define isASCII_A(c) isASCII(c)
1816#define isASCII_L1(c) isASCII(c)
e66b99e9
KW
1817#define isBLANK(c) isBLANK_A(c)
1818#define isCNTRL(c) isCNTRL_A(c)
1819#define isDIGIT(c) isDIGIT_A(c)
1820#define isGRAPH(c) isGRAPH_A(c)
1821#define isIDFIRST(c) isIDFIRST_A(c)
1822#define isLOWER(c) isLOWER_A(c)
1823#define isPRINT(c) isPRINT_A(c)
779cf272 1824#define isPSXSPC_A(c) isSPACE_A(c)
e66b99e9 1825#define isPSXSPC(c) isPSXSPC_A(c)
779cf272 1826#define isPSXSPC_L1(c) isSPACE_L1(c)
e66b99e9
KW
1827#define isPUNCT(c) isPUNCT_A(c)
1828#define isSPACE(c) isSPACE_A(c)
1829#define isUPPER(c) isUPPER_A(c)
1830#define isWORDCHAR(c) isWORDCHAR_A(c)
1831#define isXDIGIT(c) isXDIGIT_A(c)
1832
1833/* ASCII casing. These could also be written as
1834 #define toLOWER(c) (isASCII(c) ? toLOWER_LATIN1(c) : (c))
1835 #define toUPPER(c) (isASCII(c) ? toUPPER_LATIN1_MOD(c) : (c))
1836 which uses table lookup and mask instead of subtraction. (This would
c5e9991e
KW
1837 work because the _MOD does not apply in the ASCII range).
1838
1839 These actually are UTF-8 invariant casing, not just ASCII, as any non-ASCII
1840 UTF-8 invariants are neither upper nor lower. (Only on EBCDIC platforms are
1841 there non-ASCII invariants, and all of them are controls.) */
68067e4e
DM
1842#define toLOWER(c) (isUPPER(c) ? (U8)((c) + ('a' - 'A')) : (c))
1843#define toUPPER(c) (isLOWER(c) ? (U8)((c) - ('a' - 'A')) : (c))
bbce6d69 1844
25200305
KW
1845/* In the ASCII range, these are equivalent to what they're here defined to be.
1846 * But by creating these definitions, other code doesn't have to be aware of
c5e9991e
KW
1847 * this detail. Actually this works for all UTF-8 invariants, not just the
1848 * ASCII range. (EBCDIC platforms can have non-ASCII invariants.) */
25200305 1849#define toFOLD(c) toLOWER(c)
25200305
KW
1850#define toTITLE(c) toUPPER(c)
1851
c753c8d3
KW
1852#define toLOWER_A(c) toLOWER(c)
1853#define toUPPER_A(c) toUPPER(c)
25200305
KW
1854#define toFOLD_A(c) toFOLD(c)
1855#define toTITLE_A(c) toTITLE(c)
1a0901db 1856
4650c663 1857/* Use table lookup for speed; returns the input itself if is out-of-range */
b2bf251f 1858#define toLOWER_LATIN1(c) ((! FITS_IN_8_BITS(c)) \
8e7c6e7d 1859 ? (c) \
f4cd282c 1860 : PL_latin1_lc[ (U8) (c) ])
c753c8d3
KW
1861#define toLOWER_L1(c) toLOWER_LATIN1(c) /* Synonym for consistency */
1862
1a0901db 1863/* Modified uc. Is correct uc except for three non-ascii chars which are
4650c663
KW
1864 * all mapped to one of them, and these need special handling; returns the
1865 * input itself if is out-of-range */
b2bf251f 1866#define toUPPER_LATIN1_MOD(c) ((! FITS_IN_8_BITS(c)) \
8e7c6e7d 1867 ? (c) \
f4cd282c 1868 : PL_mod_latin1_uc[ (U8) (c) ])
31f05a37 1869#define IN_UTF8_CTYPE_LOCALE PL_in_utf8_CTYPE_locale
84061b6a 1870
beab9ebe
KW
1871/* Use foo_LC_uvchr() instead of these for beyond the Latin1 range */
1872
1873/* For internal core Perl use only: the base macro for defining macros like
1874 * isALPHA_LC, which uses the current LC_CTYPE locale. 'c' is the code point
31f05a37
KW
1875 * (0-255) to check. In a UTF-8 locale, the result is the same as calling
1876 * isFOO_L1(); the 'utf8_locale_classnum' parameter is something like
1877 * _CC_UPPER, which gives the class number for doing this. For non-UTF-8
1878 * locales, the code to actually do the test this is passed in 'non_utf8'. If
1879 * 'c' is above 255, 0 is returned. For accessing the full range of possible
6eb62d23 1880 * code points under locale rules, use the macros based on generic_LC_uvchr_
31f05a37 1881 * instead of this. */
6eb62d23 1882#define generic_LC_base_(c, utf8_locale_classnum, non_utf8) \
beab9ebe
KW
1883 (! FITS_IN_8_BITS(c) \
1884 ? 0 \
31f05a37
KW
1885 : IN_UTF8_CTYPE_LOCALE \
1886 ? cBOOL(PL_charclass[(U8) (c)] & _CC_mask(utf8_locale_classnum)) \
beab9ebe
KW
1887 : cBOOL(non_utf8))
1888
1889/* For internal core Perl use only: a helper macro for defining macros like
1890 * isALPHA_LC. 'c' is the code point (0-255) to check. The function name to
1891 * actually do this test is passed in 'non_utf8_func', which is called on 'c',
6eb62d23
KW
1892 * casting 'c' to the macro LC_CAST_, which should not be parenthesized. See
1893 * generic_LC_base_ for more info */
1894#define generic_LC_(c, utf8_locale_classnum, non_utf8_func) \
1895 generic_LC_base_(c,utf8_locale_classnum, \
1896 non_utf8_func( (LC_CAST_) (c)))
beab9ebe 1897
6eb62d23 1898/* For internal core Perl use only: like generic_LC_, but also returns TRUE if
beab9ebe 1899 * 'c' is the platform's native underscore character */
6eb62d23
KW
1900#define generic_LC_underscore_(c,utf8_locale_classnum,non_utf8_func) \
1901 generic_LC_base_(c, utf8_locale_classnum, \
1902 (non_utf8_func( (LC_CAST_) (c)) \
beab9ebe
KW
1903 || (char)(c) == '_'))
1904
1905/* These next three are also for internal core Perl use only: case-change
247985d4
KW
1906 * helper macros. The reason for using the PL_latin arrays is in case the
1907 * system function is defective; it ensures uniform results that conform to the
b257a28c 1908 * Unicod standard. It does not handle the anomalies in UTF-8 Turkic locales */
6eb62d23 1909#define generic_toLOWER_LC_(c, function, cast) (! FITS_IN_8_BITS(c) \
beab9ebe 1910 ? (c) \
31f05a37
KW
1911 : (IN_UTF8_CTYPE_LOCALE) \
1912 ? PL_latin1_lc[ (U8) (c) ] \
5a10328c 1913 : (cast)function((cast)(c)))
beab9ebe 1914
31f05a37
KW
1915/* Note that the result can be larger than a byte in a UTF-8 locale. It
1916 * returns a single value, so can't adequately return the upper case of LATIN
1917 * SMALL LETTER SHARP S in a UTF-8 locale (which should be a string of two
1918 * values "SS"); instead it asserts against that under DEBUGGING, and
b257a28c
KW
1919 * otherwise returns its input. It does not handle the anomalies in UTF-8
1920 * Turkic locales. */
6eb62d23 1921#define generic_toUPPER_LC_(c, function, cast) \
beab9ebe
KW
1922 (! FITS_IN_8_BITS(c) \
1923 ? (c) \
31f05a37 1924 : ((! IN_UTF8_CTYPE_LOCALE) \
b7d90381 1925 ? (cast)function((cast)(c)) \
31f05a37
KW
1926 : ((((U8)(c)) == MICRO_SIGN) \
1927 ? GREEK_CAPITAL_LETTER_MU \
1928 : ((((U8)(c)) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) \
1929 ? LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS \
1930 : ((((U8)(c)) == LATIN_SMALL_LETTER_SHARP_S) \
1931 ? (__ASSERT_(0) (c)) \
1932 : PL_mod_latin1_uc[ (U8) (c) ])))))
1933
1934/* Note that the result can be larger than a byte in a UTF-8 locale. It
1935 * returns a single value, so can't adequately return the fold case of LATIN
1936 * SMALL LETTER SHARP S in a UTF-8 locale (which should be a string of two
1937 * values "ss"); instead it asserts against that under DEBUGGING, and
b257a28c
KW
1938 * otherwise returns its input. It does not handle the anomalies in UTF-8
1939 * Turkic locales */
6eb62d23 1940#define generic_toFOLD_LC_(c, function, cast) \
4d7db1b9 1941 ((UNLIKELY((c) == MICRO_SIGN) && IN_UTF8_CTYPE_LOCALE) \
31f05a37 1942 ? GREEK_SMALL_LETTER_MU \
4d7db1b9
KW
1943 : (__ASSERT_(! IN_UTF8_CTYPE_LOCALE \
1944 || (c) != LATIN_SMALL_LETTER_SHARP_S) \
6eb62d23 1945 generic_toLOWER_LC_(c, function, cast)))
beab9ebe 1946
84061b6a 1947/* Use the libc versions for these if available. */
f05550c0 1948#if defined(HAS_ISASCII)
84061b6a
KW
1949# define isASCII_LC(c) (FITS_IN_8_BITS(c) && isascii( (U8) (c)))
1950#else
1951# define isASCII_LC(c) isASCII(c)
1952#endif
1953
f05550c0 1954#if defined(HAS_ISBLANK)
6eb62d23 1955# define isBLANK_LC(c) generic_LC_(c, _CC_BLANK, isblank)
95f34b6f 1956#else /* Unlike isASCII, varies if in a UTF-8 locale */
278b2b56 1957# define isBLANK_LC(c) ((IN_UTF8_CTYPE_LOCALE) ? isBLANK_L1(c) : isBLANK(c))
84061b6a
KW
1958#endif
1959
6eb62d23 1960#define LC_CAST_ U8
bbce6d69 1961
f05550c0 1962#ifdef WIN32
375f5f06
KW
1963 /* The Windows functions don't bother to follow the POSIX standard, which
1964 * for example says that something can't both be a printable and a control.
1965 * But Windows treats the \t control as a printable, and does such things
1966 * as making superscripts into both digits and punctuation. This tames
1967 * these flaws by assuming that the definitions of both controls and space
1968 * are correct, and then making sure that other definitions don't have
1969 * weirdnesses, by making sure that isalnum() isn't also ispunct(), etc.
1970 * Not all possible weirdnesses are checked for, just the ones that were
1971 * detected on actual Microsoft code pages */
1972
6eb62d23
KW
1973# define isCNTRL_LC(c) generic_LC_(c, _CC_CNTRL, iscntrl)
1974# define isSPACE_LC(c) generic_LC_(c, _CC_SPACE, isspace)
b7d90381 1975
6eb62d23 1976# define isALPHA_LC(c) (generic_LC_(c, _CC_ALPHA, isalpha) \
b7d90381 1977 && isALPHANUMERIC_LC(c))
6eb62d23 1978# define isALPHANUMERIC_LC(c) (generic_LC_(c, _CC_ALPHANUMERIC, isalnum) && \
b7d90381 1979 ! isPUNCT_LC(c))
6eb62d23 1980# define isDIGIT_LC(c) (generic_LC_(c, _CC_DIGIT, isdigit) && \
b7d90381 1981 isALPHANUMERIC_LC(c))
6eb62d23 1982# define isGRAPH_LC(c) (generic_LC_(c, _CC_GRAPH, isgraph) && isPRINT_LC(c))
b7d90381 1983# define isIDFIRST_LC(c) (((c) == '_') \
6eb62d23
KW
1984 || (generic_LC_(c, _CC_IDFIRST, isalpha) && ! isPUNCT_LC(c)))
1985# define isLOWER_LC(c) (generic_LC_(c, _CC_LOWER, islower) && isALPHA_LC(c))
1986# define isPRINT_LC(c) (generic_LC_(c, _CC_PRINT, isprint) && ! isCNTRL_LC(c))
1987# define isPUNCT_LC(c) (generic_LC_(c, _CC_PUNCT, ispunct) && ! isCNTRL_LC(c))
1988# define isUPPER_LC(c) (generic_LC_(c, _CC_UPPER, isupper) && isALPHA_LC(c))
f05550c0 1989# define isWORDCHAR_LC(c) (((c) == '_') || isALPHANUMERIC_LC(c))
6eb62d23 1990# define isXDIGIT_LC(c) (generic_LC_(c, _CC_XDIGIT, isxdigit) \
b7d90381 1991 && isALPHANUMERIC_LC(c))
f05550c0 1992
6eb62d23
KW
1993# define toLOWER_LC(c) generic_toLOWER_LC_((c), tolower, U8)
1994# define toUPPER_LC(c) generic_toUPPER_LC_((c), toupper, U8)
1995# define toFOLD_LC(c) generic_toFOLD_LC_((c), tolower, U8)
f05550c0
BF
1996
1997#elif defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
4650c663 1998 /* For most other platforms */
beab9ebe 1999
6eb62d23
KW
2000# define isALPHA_LC(c) generic_LC_(c, _CC_ALPHA, isalpha)
2001# define isALPHANUMERIC_LC(c) generic_LC_(c, _CC_ALPHANUMERIC, isalnum)
2002# define isCNTRL_LC(c) generic_LC_(c, _CC_CNTRL, iscntrl)
2003# define isDIGIT_LC(c) generic_LC_(c, _CC_DIGIT, isdigit)
05319f56 2004# ifdef OS390 /* This system considers NBSP to be a graph */
6eb62d23 2005# define isGRAPH_LC(c) generic_LC_(c, _CC_GRAPH, isgraph) \
05319f56
KW
2006 && ! isSPACE_LC(c)
2007# else
6eb62d23 2008# define isGRAPH_LC(c) generic_LC_(c, _CC_GRAPH, isgraph)
05319f56 2009# endif
6eb62d23
KW
2010# define isIDFIRST_LC(c) generic_LC_underscore_(c, _CC_IDFIRST, isalpha)
2011# define isLOWER_LC(c) generic_LC_(c, _CC_LOWER, islower)
2012# define isPRINT_LC(c) generic_LC_(c, _CC_PRINT, isprint)
2013# define isPUNCT_LC(c) generic_LC_(c, _CC_PUNCT, ispunct)
2014# define isSPACE_LC(c) generic_LC_(c, _CC_SPACE, isspace)
2015# define isUPPER_LC(c) generic_LC_(c, _CC_UPPER, isupper)
2016# define isWORDCHAR_LC(c) generic_LC_underscore_(c, _CC_WORDCHAR, isalnum)
2017# define isXDIGIT_LC(c) generic_LC_(c, _CC_XDIGIT, isxdigit)
f05550c0
BF
2018
2019
6eb62d23
KW
2020# define toLOWER_LC(c) generic_toLOWER_LC_((c), tolower, U8)
2021# define toUPPER_LC(c) generic_toUPPER_LC_((c), toupper, U8)
2022# define toFOLD_LC(c) generic_toFOLD_LC_((c), tolower, U8)
f05550c0
BF
2023
2024#else /* The final fallback position */
2025
b7d90381
KW
2026# define isALPHA_LC(c) (isascii(c) && isalpha(c))
2027# define isALPHANUMERIC_LC(c) (isascii(c) && isalnum(c))
2028# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
2029# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
2030# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
f05550c0 2031# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
b7d90381
KW
2032# define isLOWER_LC(c) (isascii(c) && islower(c))
2033# define isPRINT_LC(c) (isascii(c) && isprint(c))
2034# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
2035# define isSPACE_LC(c) (isascii(c) && isspace(c))
2036# define isUPPER_LC(c) (isascii(c) && isupper(c))
f05550c0 2037# define isWORDCHAR_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
b7d90381 2038# define isXDIGIT_LC(c) (isascii(c) && isxdigit(c))
f05550c0
BF
2039
2040# define toLOWER_LC(c) (isascii(c) ? tolower(c) : (c))
2041# define toUPPER_LC(c) (isascii(c) ? toupper(c) : (c))
2042# define toFOLD_LC(c) (isascii(c) ? tolower(c) : (c))
bbce6d69 2043
f05550c0 2044#endif
55204971 2045
eba68aa0
KW
2046#define isIDCONT(c) isWORDCHAR(c)
2047#define isIDCONT_A(c) isWORDCHAR_A(c)
2048#define isIDCONT_L1(c) isWORDCHAR_L1(c)
2049#define isIDCONT_LC(c) isWORDCHAR_LC(c)
13380643 2050#define isPSXSPC_LC(c) isSPACE_LC(c)
aaa51d5e 2051
4650c663 2052/* For internal core Perl use only: the base macros for defining macros like
d0da05db 2053 * isALPHA_uvchr. 'c' is the code point to check. 'classnum' is the POSIX class
6eb62d23 2054 * number defined earlier in this file. generic_uvchr_() is used for POSIX
4650c663
KW
2055 * classes where there is a macro or function 'above_latin1' that takes the
2056 * single argument 'c' and returns the desired value. These exist for those
2366ba44 2057 * classes which have simple definitions, avoiding the overhead of an inversion
6eb62d23 2058 * list binary search. generic_invlist_uvchr_() can be used
4650c663 2059 * for classes where that overhead is faster than a direct lookup.
6eb62d23
KW
2060 * generic_uvchr_() won't compile if 'c' isn't unsigned, as it won't match the
2061 * 'above_latin1' prototype. generic_isCC_() macro does bounds checking, so
4650c663
KW
2062 * have duplicate checks here, so could create versions of the macros that
2063 * don't, but experiments show that gcc optimizes them out anyway. */
66c17564
KW
2064
2065/* Note that all ignore 'use bytes' */
6eb62d23
KW
2066#define generic_uvchr_(classnum, above_latin1, c) ((c) < 256 \
2067 ? generic_isCC_(c, classnum) \
cd500f2f 2068 : above_latin1(c))
6eb62d23
KW
2069#define generic_invlist_uvchr_(classnum, c) ((c) < 256 \
2070 ? generic_isCC_(c, classnum) \
922e8cb4 2071 : _is_uni_FOO(classnum, c))
6eb62d23
KW
2072#define isALPHA_uvchr(c) generic_invlist_uvchr_(_CC_ALPHA, c)
2073#define isALPHANUMERIC_uvchr(c) generic_invlist_uvchr_(_CC_ALPHANUMERIC, c)
d0da05db 2074#define isASCII_uvchr(c) isASCII(c)
6eb62d23 2075#define isBLANK_uvchr(c) generic_uvchr_(_CC_BLANK, is_HORIZWS_cp_high, c)
d0da05db 2076#define isCNTRL_uvchr(c) isCNTRL_L1(c) /* All controls are in Latin1 */
6eb62d23
KW
2077#define isDIGIT_uvchr(c) generic_invlist_uvchr_(_CC_DIGIT, c)
2078#define isGRAPH_uvchr(c) generic_invlist_uvchr_(_CC_GRAPH, c)
1e222e4f 2079#define isIDCONT_uvchr(c) \
6eb62d23 2080 generic_uvchr_(_CC_WORDCHAR, _is_uni_perl_idcont, c)
1e222e4f 2081#define isIDFIRST_uvchr(c) \
6eb62d23
KW
2082 generic_uvchr_(_CC_IDFIRST, _is_uni_perl_idstart, c)
2083#define isLOWER_uvchr(c) generic_invlist_uvchr_(_CC_LOWER, c)
2084#define isPRINT_uvchr(c) generic_invlist_uvchr_(_CC_PRINT, c)
d0da05db 2085
6eb62d23
KW
2086#define isPUNCT_uvchr(c) generic_invlist_uvchr_(_CC_PUNCT, c)
2087#define isSPACE_uvchr(c) generic_uvchr_(_CC_SPACE, is_XPERLSPACE_cp_high, c)
d0da05db
KW
2088#define isPSXSPC_uvchr(c) isSPACE_uvchr(c)
2089
6eb62d23
KW
2090#define isUPPER_uvchr(c) generic_invlist_uvchr_(_CC_UPPER, c)
2091#define isVERTWS_uvchr(c) generic_uvchr_(_CC_VERTSPACE, is_VERTWS_cp_high, c)
2092#define isWORDCHAR_uvchr(c) generic_invlist_uvchr_(_CC_WORDCHAR, c)
2093#define isXDIGIT_uvchr(c) generic_uvchr_(_CC_XDIGIT, is_XDIGIT_cp_high, c)
d0da05db
KW
2094
2095#define toFOLD_uvchr(c,s,l) to_uni_fold(c,s,l)
2096#define toLOWER_uvchr(c,s,l) to_uni_lower(c,s,l)
2097#define toTITLE_uvchr(c,s,l) to_uni_title(c,s,l)
2098#define toUPPER_uvchr(c,s,l) to_uni_upper(c,s,l)
2099
2100/* For backwards compatibility, even though '_uni' should mean official Unicode
2101 * code points, in Perl it means native for those below 256 */
2102#define isALPHA_uni(c) isALPHA_uvchr(c)
2103#define isALPHANUMERIC_uni(c) isALPHANUMERIC_uvchr(c)
2104#define isASCII_uni(c) isASCII_uvchr(c)
2105#define isBLANK_uni(c) isBLANK_uvchr(c)
2106#define isCNTRL_uni(c) isCNTRL_uvchr(c)
2107#define isDIGIT_uni(c) isDIGIT_uvchr(c)
2108#define isGRAPH_uni(c) isGRAPH_uvchr(c)
2109#define isIDCONT_uni(c) isIDCONT_uvchr(c)
2110#define isIDFIRST_uni(c) isIDFIRST_uvchr(c)
2111#define isLOWER_uni(c) isLOWER_uvchr(c)
2112#define isPRINT_uni(c) isPRINT_uvchr(c)
2113#define isPUNCT_uni(c) isPUNCT_uvchr(c)
2114#define isSPACE_uni(c) isSPACE_uvchr(c)
2115#define isPSXSPC_uni(c) isPSXSPC_uvchr(c)
2116#define isUPPER_uni(c) isUPPER_uvchr(c)
2117#define isVERTWS_uni(c) isVERTWS_uvchr(c)
2118#define isWORDCHAR_uni(c) isWORDCHAR_uvchr(c)
2119#define isXDIGIT_uni(c) isXDIGIT_uvchr(c)
2120#define toFOLD_uni(c,s,l) toFOLD_uvchr(c,s,l)
2121#define toLOWER_uni(c,s,l) toLOWER_uvchr(c,s,l)
2122#define toTITLE_uni(c,s,l) toTITLE_uvchr(c,s,l)
2123#define toUPPER_uni(c,s,l) toUPPER_uvchr(c,s,l)
a0ed51b3 2124
4650c663
KW
2125/* For internal core Perl use only: the base macros for defining macros like
2126 * isALPHA_LC_uvchr. These are like isALPHA_LC, but the input can be any code
6eb62d23 2127 * point, not just 0-255. Like generic_uvchr_, there are two versions, one for
4650c663 2128 * simple class definitions; the other for more complex. These are like
6eb62d23
KW
2129 * generic_uvchr_, so see it for more info. */
2130#define generic_LC_uvchr_(latin1, above_latin1, c) \
cd500f2f 2131 (c < 256 ? latin1(c) : above_latin1(c))
6eb62d23 2132#define generic_LC_invlist_uvchr_(latin1, classnum, c) \
cd500f2f
KW
2133 (c < 256 ? latin1(c) : _is_uni_FOO(classnum, c))
2134
6eb62d23
KW
2135#define isALPHA_LC_uvchr(c) generic_LC_invlist_uvchr_(isALPHA_LC, _CC_ALPHA, c)
2136#define isALPHANUMERIC_LC_uvchr(c) generic_LC_invlist_uvchr_(isALPHANUMERIC_LC, \
cd500f2f 2137 _CC_ALPHANUMERIC, c)
b7d90381 2138#define isASCII_LC_uvchr(c) isASCII_LC(c)
6eb62d23 2139#define isBLANK_LC_uvchr(c) generic_LC_uvchr_(isBLANK_LC, \
b7d90381 2140 is_HORIZWS_cp_high, c)
feeab5a9 2141#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : 0)
6eb62d23
KW
2142#define isDIGIT_LC_uvchr(c) generic_LC_invlist_uvchr_(isDIGIT_LC, _CC_DIGIT, c)
2143#define isGRAPH_LC_uvchr(c) generic_LC_invlist_uvchr_(isGRAPH_LC, _CC_GRAPH, c)
2144#define isIDCONT_LC_uvchr(c) generic_LC_uvchr_(isIDCONT_LC, \
eba68aa0 2145 _is_uni_perl_idcont, c)
6eb62d23 2146#define isIDFIRST_LC_uvchr(c) generic_LC_uvchr_(isIDFIRST_LC, \
cd500f2f 2147 _is_uni_perl_idstart, c)
6eb62d23
KW
2148#define isLOWER_LC_uvchr(c) generic_LC_invlist_uvchr_(isLOWER_LC, _CC_LOWER, c)
2149#define isPRINT_LC_uvchr(c) generic_LC_invlist_uvchr_(isPRINT_LC, _CC_PRINT, c)
b7d90381 2150#define isPSXSPC_LC_uvchr(c) isSPACE_LC_uvchr(c)
6eb62d23
KW
2151#define isPUNCT_LC_uvchr(c) generic_LC_invlist_uvchr_(isPUNCT_LC, _CC_PUNCT, c)
2152#define isSPACE_LC_uvchr(c) generic_LC_uvchr_(isSPACE_LC, \
509fb054 2153 is_XPERLSPACE_cp_high, c)
6eb62d23
KW
2154#define isUPPER_LC_uvchr(c) generic_LC_invlist_uvchr_(isUPPER_LC, _CC_UPPER, c)
2155#define isWORDCHAR_LC_uvchr(c) generic_LC_invlist_uvchr_(isWORDCHAR_LC, \
cd500f2f 2156 _CC_WORDCHAR, c)
6eb62d23 2157#define isXDIGIT_LC_uvchr(c) generic_LC_uvchr_(isXDIGIT_LC, \
b7d90381 2158 is_XDIGIT_cp_high, c)
e712593e 2159
b7d90381 2160#define isBLANK_LC_uni(c) isBLANK_LC_uvchr(UNI_TO_NATIVE(c))
aaa51d5e 2161
da8c1a98
KW
2162/* The "_safe" macros make sure that we don't attempt to read beyond 'e', but
2163 * they don't otherwise go out of their way to look for malformed UTF-8. If
2164 * they can return accurate results without knowing if the input is otherwise
2165 * malformed, they do so. For example isASCII is accurate in spite of any
2166 * non-length malformations because it looks only at a single byte. Likewise
2167 * isDIGIT looks just at the first byte for code points 0-255, as all UTF-8
2168 * variant ones return FALSE. But, if the input has to be well-formed in order
2169 * for the results to be accurate, the macros will test and if malformed will
2170 * call a routine to die
2171 *
2172 * Except for toke.c, the macros do assume that e > p, asserting that on
2173 * DEBUGGING builds. Much code that calls these depends on this being true,
2174 * for other reasons. toke.c is treated specially as using the regular
2175 * assertion breaks it in many ways. All strings that these operate on there
2176 * are supposed to have an extra NUL character at the end, so that *e = \0. A
2177 * bunch of code in toke.c assumes that this is true, so the assertion allows
2178 * for that */
2179#ifdef PERL_IN_TOKE_C
2180# define _utf8_safe_assert(p,e) ((e) > (p) || ((e) == (p) && *(p) == '\0'))
2181#else
2182# define _utf8_safe_assert(p,e) ((e) > (p))
2183#endif
2184
6eb62d23 2185#define generic_utf8_safe_(classnum, p, e, above_latin1) \
c81b3562
KW
2186 ((! _utf8_safe_assert(p, e)) \
2187 ? (_force_out_malformed_utf8_message((U8 *) (p), (U8 *) (e), 0, 1), 0)\
2188 : (UTF8_IS_INVARIANT(*(p))) \
6eb62d23 2189 ? generic_isCC_(*(p), classnum) \
da8c1a98
KW
2190 : (UTF8_IS_DOWNGRADEABLE_START(*(p)) \
2191 ? ((LIKELY((e) - (p) > 1 && UTF8_IS_CONTINUATION(*((p)+1)))) \
6eb62d23 2192 ? generic_isCC_(EIGHT_BIT_UTF8_TO_NATIVE(*(p), *((p)+1 )), \
da8c1a98
KW
2193 classnum) \
2194 : (_force_out_malformed_utf8_message( \
2195 (U8 *) (p), (U8 *) (e), 0, 1), 0)) \
2196 : above_latin1))
b7d90381
KW
2197/* Like the above, but calls 'above_latin1(p)' to get the utf8 value.
2198 * 'above_latin1' can be a macro */
6eb62d23
KW
2199#define generic_func_utf8_safe_(classnum, above_latin1, p, e) \
2200 generic_utf8_safe_(classnum, p, e, above_latin1(p, e))
2201#define generic_non_invlist_utf8_safe_(classnum, above_latin1, p, e) \
2202 generic_utf8_safe_(classnum, p, e, \
da8c1a98
KW
2203 (UNLIKELY((e) - (p) < UTF8SKIP(p)) \
2204 ? (_force_out_malformed_utf8_message( \
2205 (U8 *) (p), (U8 *) (e), 0, 1), 0) \
2206 : above_latin1(p)))
2366ba44
KW
2207/* Like the above, but passes classnum to _isFOO_utf8(), instead of having an
2208 * 'above_latin1' parameter */
6eb62d23
KW
2209#define generic_invlist_utf8_safe_(classnum, p, e) \
2210 generic_utf8_safe_(classnum, p, e, _is_utf8_FOO(classnum, p, e))
922e8cb4 2211
cc8ab7c0 2212/* Like the above, but should be used only when it is known that there are no
ff7ecfc3
KW
2213 * characters in the upper-Latin1 range (128-255 on ASCII platforms) which the
2214 * class is TRUE for. Hence it can skip the tests for this range.
2215 * 'above_latin1' should include its arguments */
6eb62d23 2216#define generic_utf8_safe_no_upper_latin1_(classnum, p, e, above_latin1) \
da8c1a98 2217 (__ASSERT_(_utf8_safe_assert(p, e)) \
2d8dd9eb 2218 (isASCII(*(p))) \
6eb62d23 2219 ? generic_isCC_(*(p), classnum) \
da8c1a98
KW
2220 : (UTF8_IS_DOWNGRADEABLE_START(*(p))) \
2221 ? 0 /* Note that doesn't check validity for latin1 */ \
2222 : above_latin1)
2223
84238efa 2224
059703b0
KW
2225#define isALPHA_utf8(p, e) isALPHA_utf8_safe(p, e)
2226#define isALPHANUMERIC_utf8(p, e) isALPHANUMERIC_utf8_safe(p, e)
2227#define isASCII_utf8(p, e) isASCII_utf8_safe(p, e)
2228#define isBLANK_utf8(p, e) isBLANK_utf8_safe(p, e)
2229#define isCNTRL_utf8(p, e) isCNTRL_utf8_safe(p, e)
2230#define isDIGIT_utf8(p, e) isDIGIT_utf8_safe(p, e)
2231#define isGRAPH_utf8(p, e) isGRAPH_utf8_safe(p, e)
2232#define isIDCONT_utf8(p, e) isIDCONT_utf8_safe(p, e)
2233#define isIDFIRST_utf8(p, e) isIDFIRST_utf8_safe(p, e)
2234#define isLOWER_utf8(p, e) isLOWER_utf8_safe(p, e)
2235#define isPRINT_utf8(p, e) isPRINT_utf8_safe(p, e)
2236#define isPSXSPC_utf8(p, e) isPSXSPC_utf8_safe(p, e)
2237#define isPUNCT_utf8(p, e) isPUNCT_utf8_safe(p, e)
2238#define isSPACE_utf8(p, e) isSPACE_utf8_safe(p, e)
2239#define isUPPER_utf8(p, e) isUPPER_utf8_safe(p, e)
2240#define isVERTWS_utf8(p, e) isVERTWS_utf8_safe(p, e)
2241#define isWORDCHAR_utf8(p, e) isWORDCHAR_utf8_safe(p, e)
2242#define isXDIGIT_utf8(p, e) isXDIGIT_utf8_safe(p, e)
e8fa43e2 2243
6eb62d23 2244#define isALPHA_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_ALPHA, p, e)
da8c1a98 2245#define isALPHANUMERIC_utf8_safe(p, e) \
6eb62d23 2246 generic_invlist_utf8_safe_(_CC_ALPHANUMERIC, p, e)
da8c1a98
KW
2247#define isASCII_utf8_safe(p, e) \
2248 /* Because ASCII is invariant under utf8, the non-utf8 macro \
2249 * works */ \
2250 (__ASSERT_(_utf8_safe_assert(p, e)) isASCII(*(p)))
2251#define isBLANK_utf8_safe(p, e) \
6eb62d23 2252 generic_non_invlist_utf8_safe_(_CC_BLANK, is_HORIZWS_high, p, e)
da8c1a98 2253
e8fa43e2
KW
2254#ifdef EBCDIC
2255 /* Because all controls are UTF-8 invariants in EBCDIC, we can use this
2256 * more efficient macro instead of the more general one */
da8c1a98 2257# define isCNTRL_utf8_safe(p, e) \
56d02b8c 2258 (__ASSERT_(_utf8_safe_assert(p, e)) isCNTRL_L1(*(p)))
e8fa43e2 2259#else
6eb62d23 2260# define isCNTRL_utf8_safe(p, e) generic_utf8_safe_(_CC_CNTRL, p, e, 0)
e8fa43e2
KW
2261#endif
2262
da8c1a98 2263#define isDIGIT_utf8_safe(p, e) \
6eb62d23 2264 generic_utf8_safe_no_upper_latin1_(_CC_DIGIT, p, e, \
2366ba44 2265 _is_utf8_FOO(_CC_DIGIT, p, e))
6eb62d23
KW
2266#define isGRAPH_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_GRAPH, p, e)
2267#define isIDCONT_utf8_safe(p, e) generic_func_utf8_safe_(_CC_WORDCHAR, \
dd1a3ba7 2268 _is_utf8_perl_idcont, p, e)
e5dcd934 2269
c11ff943
KW
2270/* To prevent S_scan_word in toke.c from hanging, we have to make sure that
2271 * IDFIRST is an alnum. See
8034715d 2272 * https://github.com/Perl/perl5/issues/10275 for more detail than you
f91dcd13
KW
2273 * ever wanted to know about. (In the ASCII range, there isn't a difference.)
2274 * This used to be not the XID version, but we decided to go with the more
2275 * modern Unicode definition */
da8c1a98 2276#define isIDFIRST_utf8_safe(p, e) \
6eb62d23 2277 generic_func_utf8_safe_(_CC_IDFIRST, \
dd1a3ba7 2278 _is_utf8_perl_idstart, (U8 *) (p), (U8 *) (e))
da8c1a98 2279
6eb62d23
KW
2280#define isLOWER_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_LOWER, p, e)
2281#define isPRINT_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_PRINT, p, e)
da8c1a98 2282#define isPSXSPC_utf8_safe(p, e) isSPACE_utf8_safe(p, e)
6eb62d23 2283#define isPUNCT_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_PUNCT, p, e)
da8c1a98 2284#define isSPACE_utf8_safe(p, e) \
6eb62d23
KW
2285 generic_non_invlist_utf8_safe_(_CC_SPACE, is_XPERLSPACE_high, p, e)
2286#define isUPPER_utf8_safe(p, e) generic_invlist_utf8_safe_(_CC_UPPER, p, e)
da8c1a98 2287#define isVERTWS_utf8_safe(p, e) \
6eb62d23 2288 generic_non_invlist_utf8_safe_(_CC_VERTSPACE, is_VERTWS_high, p, e)
da8c1a98 2289#define isWORDCHAR_utf8_safe(p, e) \
6eb62d23 2290 generic_invlist_utf8_safe_(_CC_WORDCHAR, p, e)
da8c1a98 2291#define isXDIGIT_utf8_safe(p, e) \
6eb62d23 2292 generic_utf8_safe_no_upper_latin1_(_CC_XDIGIT, p, e, \
da8c1a98
KW
2293 (UNLIKELY((e) - (p) < UTF8SKIP(p)) \
2294 ? (_force_out_malformed_utf8_message( \
2295 (U8 *) (p), (U8 *) (e), 0, 1), 0) \
2296 : is_XDIGIT_high(p)))
a0ed51b3 2297
059703b0
KW
2298#define toFOLD_utf8(p,e,s,l) toFOLD_utf8_safe(p,e,s,l)
2299#define toLOWER_utf8(p,e,s,l) toLOWER_utf8_safe(p,e,s,l)
2300#define toTITLE_utf8(p,e,s,l) toTITLE_utf8_safe(p,e,s,l)
2301#define toUPPER_utf8(p,e,s,l) toUPPER_utf8_safe(p,e,s,l)
2e8adce6 2302
567b353c 2303/* For internal core use only, subject to change */
059703b0
KW
2304#define _toFOLD_utf8_flags(p,e,s,l,f) _to_utf8_fold_flags (p,e,s,l,f)
2305#define _toLOWER_utf8_flags(p,e,s,l,f) _to_utf8_lower_flags(p,e,s,l,f)
2306#define _toTITLE_utf8_flags(p,e,s,l,f) _to_utf8_title_flags(p,e,s,l,f)
2307#define _toUPPER_utf8_flags(p,e,s,l,f) _to_utf8_upper_flags(p,e,s,l,f)
a1a5ec35
KW
2308
2309#define toFOLD_utf8_safe(p,e,s,l) _toFOLD_utf8_flags(p,e,s,l, FOLD_FLAGS_FULL)
2310#define toLOWER_utf8_safe(p,e,s,l) _toLOWER_utf8_flags(p,e,s,l, 0)
2311#define toTITLE_utf8_safe(p,e,s,l) _toTITLE_utf8_flags(p,e,s,l, 0)
2312#define toUPPER_utf8_safe(p,e,s,l) _toUPPER_utf8_flags(p,e,s,l, 0)
567b353c 2313
059703b0
KW
2314#define isALPHA_LC_utf8(p, e) isALPHA_LC_utf8_safe(p, e)
2315#define isALPHANUMERIC_LC_utf8(p, e) isALPHANUMERIC_LC_utf8_safe(p, e)
2316#define isASCII_LC_utf8(p, e) isASCII_LC_utf8_safe(p, e)
2317#define isBLANK_LC_utf8(p, e) isBLANK_LC_utf8_safe(p, e)
2318#define isCNTRL_LC_utf8(p, e) isCNTRL_LC_utf8_safe(p, e)
2319#define isDIGIT_LC_utf8(p, e) isDIGIT_LC_utf8_safe(p, e)
2320#define isGRAPH_LC_utf8(p, e) isGRAPH_LC_utf8_safe(p, e)
2321#define isIDCONT_LC_utf8(p, e) isIDCONT_LC_utf8_safe(p, e)
2322#define isIDFIRST_LC_utf8(p, e) isIDFIRST_LC_utf8_safe(p, e)
2323#define isLOWER_LC_utf8(p, e) isLOWER_LC_utf8_safe(p, e)
2324#define isPRINT_LC_utf8(p, e) isPRINT_LC_utf8_safe(p, e)
2325#define isPSXSPC_LC_utf8(p, e) isPSXSPC_LC_utf8_safe(p, e)
2326#define isPUNCT_LC_utf8(p, e) isPUNCT_LC_utf8_safe(p, e)
2327#define isSPACE_LC_utf8(p, e) isSPACE_LC_utf8_safe(p, e)
2328#define isUPPER_LC_utf8(p, e) isUPPER_LC_utf8_safe(p, e)
2329#define isWORDCHAR_LC_utf8(p, e) isWORDCHAR_LC_utf8_safe(p, e)
2330#define isXDIGIT_LC_utf8(p, e) isXDIGIT_LC_utf8_safe(p, e)
34aeb2e9 2331
da8c1a98 2332/* For internal core Perl use only: the base macros for defining macros like
6eb62d23 2333 * isALPHA_LC_utf8_safe. These are like generic_utf8_, but if the first code
da8c1a98
KW
2334 * point in 'p' is within the 0-255 range, it uses locale rules from the
2335 * passed-in 'macro' parameter */
6eb62d23 2336#define generic_LC_utf8_safe_(macro, p, e, above_latin1) \
da8c1a98
KW
2337 (__ASSERT_(_utf8_safe_assert(p, e)) \
2338 (UTF8_IS_INVARIANT(*(p))) \
2339 ? macro(*(p)) \
2340 : (UTF8_IS_DOWNGRADEABLE_START(*(p)) \
2341 ? ((LIKELY((e) - (p) > 1 && UTF8_IS_CONTINUATION(*((p)+1)))) \
2342 ? macro(EIGHT_BIT_UTF8_TO_NATIVE(*(p), *((p)+1))) \
2343 : (_force_out_malformed_utf8_message( \
2344 (U8 *) (p), (U8 *) (e), 0, 1), 0)) \
2345 : above_latin1))
2346
6eb62d23
KW
2347#define generic_LC_invlist_utf8_safe_(macro, classnum, p, e) \
2348 generic_LC_utf8_safe_(macro, p, e, \
2366ba44 2349 _is_utf8_FOO(classnum, p, e))
da8c1a98 2350
6eb62d23
KW
2351#define generic_LC_func_utf8_safe_(macro, above_latin1, p, e) \
2352 generic_LC_utf8_safe_(macro, p, e, above_latin1(p, e))
da8c1a98 2353
6eb62d23
KW
2354#define generic_LC_non_invlist_utf8_safe_(classnum, above_latin1, p, e) \
2355 generic_LC_utf8_safe_(classnum, p, e, \
da8c1a98
KW
2356 (UNLIKELY((e) - (p) < UTF8SKIP(p)) \
2357 ? (_force_out_malformed_utf8_message( \
2358 (U8 *) (p), (U8 *) (e), 0, 1), 0) \
2359 : above_latin1(p)))
2360
2361#define isALPHANUMERIC_LC_utf8_safe(p, e) \
6eb62d23 2362 generic_LC_invlist_utf8_safe_(isALPHANUMERIC_LC, \
da8c1a98
KW
2363 _CC_ALPHANUMERIC, p, e)
2364#define isALPHA_LC_utf8_safe(p, e) \
6eb62d23 2365 generic_LC_invlist_utf8_safe_(isALPHA_LC, _CC_ALPHA, p, e)
da8c1a98
KW
2366#define isASCII_LC_utf8_safe(p, e) \
2367 (__ASSERT_(_utf8_safe_assert(p, e)) isASCII_LC(*(p)))
2368#define isBLANK_LC_utf8_safe(p, e) \
6eb62d23 2369 generic_LC_non_invlist_utf8_safe_(isBLANK_LC, is_HORIZWS_high, p, e)
da8c1a98 2370#define isCNTRL_LC_utf8_safe(p, e) \
6eb62d23 2371 generic_LC_utf8_safe_(isCNTRL_LC, p, e, 0)
da8c1a98 2372#define isDIGIT_LC_utf8_safe(p, e) \
6eb62d23 2373 generic_LC_invlist_utf8_safe_(isDIGIT_LC, _CC_DIGIT, p, e)
da8c1a98 2374#define isGRAPH_LC_utf8_safe(p, e) \
6eb62d23 2375 generic_LC_invlist_utf8_safe_(isGRAPH_LC, _CC_GRAPH, p, e)
da8c1a98 2376#define isIDCONT_LC_utf8_safe(p, e) \
6eb62d23 2377 generic_LC_func_utf8_safe_(isIDCONT_LC, \
dd1a3ba7 2378 _is_utf8_perl_idcont, p, e)
da8c1a98 2379#define isIDFIRST_LC_utf8_safe(p, e) \
6eb62d23 2380 generic_LC_func_utf8_safe_(isIDFIRST_LC, \
dd1a3ba7 2381 _is_utf8_perl_idstart, p, e)
da8c1a98 2382#define isLOWER_LC_utf8_safe(p, e) \
6eb62d23 2383 generic_LC_invlist_utf8_safe_(isLOWER_LC, _CC_LOWER, p, e)
da8c1a98 2384#define isPRINT_LC_utf8_safe(p, e) \
6eb62d23 2385 generic_LC_invlist_utf8_safe_(isPRINT_LC, _CC_PRINT, p, e)
da8c1a98
KW
2386#define isPSXSPC_LC_utf8_safe(p, e) isSPACE_LC_utf8_safe(p, e)
2387#define isPUNCT_LC_utf8_safe(p, e) \
6eb62d23 2388 generic_LC_invlist_utf8_safe_(isPUNCT_LC, _CC_PUNCT, p, e)
da8c1a98 2389#define isSPACE_LC_utf8_safe(p, e) \
6eb62d23 2390 generic_LC_non_invlist_utf8_safe_(isSPACE_LC, is_XPERLSPACE_high, p, e)
da8c1a98 2391#define isUPPER_LC_utf8_safe(p, e) \
6eb62d23 2392 generic_LC_invlist_utf8_safe_(isUPPER_LC, _CC_UPPER, p, e)
da8c1a98 2393#define isWORDCHAR_LC_utf8_safe(p, e) \
6eb62d23 2394 generic_LC_invlist_utf8_safe_(isWORDCHAR_LC, _CC_WORDCHAR, p, e)
da8c1a98 2395#define isXDIGIT_LC_utf8_safe(p, e) \
6eb62d23 2396 generic_LC_non_invlist_utf8_safe_(isXDIGIT_LC, is_XDIGIT_high, p, e)
aaa51d5e 2397
fbc19f27
KW
2398/* Macros for backwards compatibility and for completeness when the ASCII and
2399 * Latin1 values are identical */
b7d90381
KW
2400#define isALPHAU(c) isALPHA_L1(c)
2401#define isDIGIT_L1(c) isDIGIT_A(c)
2402#define isOCTAL(c) isOCTAL_A(c)
2403#define isOCTAL_L1(c) isOCTAL_A(c)
2404#define isXDIGIT_L1(c) isXDIGIT_A(c)
2405#define isALNUM(c) isWORDCHAR(c)
a377c856 2406#define isALNUM_A(c) isALNUM(c)
b7d90381
KW
2407#define isALNUMU(c) isWORDCHAR_L1(c)
2408#define isALNUM_LC(c) isWORDCHAR_LC(c)
2409#define isALNUM_uni(c) isWORDCHAR_uni(c)
2e28f0b9 2410#define isALNUM_LC_uvchr(c) isWORDCHAR_LC_uvchr(c)
059703b0 2411#define isALNUM_utf8(p,e) isWORDCHAR_utf8(p,e)
4c1d9526 2412#define isALNUM_utf8_safe(p,e) isWORDCHAR_utf8_safe(p,e)
059703b0 2413#define isALNUM_LC_utf8(p,e)isWORDCHAR_LC_utf8(p,e)
4c1d9526 2414#define isALNUM_LC_utf8_safe(p,e)isWORDCHAR_LC_utf8_safe(p,e)
b7d90381
KW
2415#define isALNUMC_A(c) isALPHANUMERIC_A(c) /* Mnemonic: "C's alnum" */
2416#define isALNUMC_L1(c) isALPHANUMERIC_L1(c)
2417#define isALNUMC(c) isALPHANUMERIC(c)
2418#define isALNUMC_LC(c) isALPHANUMERIC_LC(c)
2419#define isALNUMC_uni(c) isALPHANUMERIC_uni(c)
15861f94 2420#define isALNUMC_LC_uvchr(c) isALPHANUMERIC_LC_uvchr(c)
059703b0 2421#define isALNUMC_utf8(p,e) isALPHANUMERIC_utf8(p,e)
4c1d9526
KW
2422#define isALNUMC_utf8_safe(p,e) isALPHANUMERIC_utf8_safe(p,e)
2423#define isALNUMC_LC_utf8_safe(p,e) isALPHANUMERIC_LC_utf8_safe(p,e)
fbc19f27 2424
2bd1cbf6
KW
2425/* On EBCDIC platforms, CTRL-@ is 0, CTRL-A is 1, etc, just like on ASCII,
2426 * except that they don't necessarily mean the same characters, e.g. CTRL-D is
2427 * 4 on both systems, but that is EOT on ASCII; ST on EBCDIC.
2428 * '?' is special-cased on EBCDIC to APC, which is the control there that is
2429 * the outlier from the block that contains the other controls, just like
2430 * toCTRL('?') on ASCII yields DEL, the control that is the outlier from the C0
2431 * block. If it weren't special cased, it would yield a non-control.
88794300
KW
2432 * The conversion works both ways, so toCTRL('D') is 4, and toCTRL(4) is D,
2433 * etc. */
2bd1cbf6 2434#ifndef EBCDIC
75763b3a 2435# define toCTRL(c) (__ASSERT_(FITS_IN_8_BITS(c)) toUPPER(((U8)(c))) ^ 64)
2bd1cbf6 2436#else
75763b3a
KW
2437# define toCTRL(c) (__ASSERT_(FITS_IN_8_BITS(c)) \
2438 ((isPRINT_A(c)) \
2439 ? (UNLIKELY((c) == '?') \
2440 ? QUESTION_MARK_CTRL \
2441 : (NATIVE_TO_LATIN1(toUPPER((U8) (c))) ^ 64)) \
2442 : (UNLIKELY((c) == QUESTION_MARK_CTRL) \
2443 ? '?' \
2444 : (LATIN1_TO_NATIVE(((U8) (c)) ^ 64)))))
2bd1cbf6 2445#endif
bbce6d69 2446
837781cc
KW
2447/*
2448=for apidoc Ay||line_t
2449The typedef to use to declare variables that are to hold line numbers.
2450
2451=cut
2452
2453 Line numbers are unsigned, 32 bits.
2454*/
dea28490 2455typedef U32 line_t;
e5dcd934 2456#define NOLINE ((line_t) 4294967295UL) /* = FFFFFFFF */
378cc40b 2457
91152fc1
DG
2458/* Helpful alias for version prescan */
2459#define is_LAX_VERSION(a,b) \
1604cfb0 2460 (a != Perl_prescan_version(aTHX_ a, FALSE, b, NULL, NULL, NULL, NULL))
91152fc1
DG
2461
2462#define is_STRICT_VERSION(a,b) \
1604cfb0 2463 (a != Perl_prescan_version(aTHX_ a, TRUE, b, NULL, NULL, NULL, NULL))
91152fc1
DG
2464
2465#define BADVERSION(a,b,c) \
1604cfb0
MS
2466 if (b) { \
2467 *b = c; \
2468 } \
2469 return a;
8c52afec 2470
1ce77b7d
KW
2471/* Converts a character KNOWN to represent a hexadecimal digit (0-9, A-F, or
2472 * a-f) to its numeric value without using any branches. The input is
2473 * validated only by an assert() in DEBUGGING builds.
2474 *
2475 * It works by right shifting and isolating the bit that is 0 for the digits,
2476 * and 1 for at least the alphas A-F, a-f. The bit is shifted to the ones
2477 * position, and then to the eights position. Both are added together to form
2478 * 0 if the input is '0'-'9' and to form 9 if alpha. This is added to the
2479 * final four bits of the input to form the correct value. */
2480#define XDIGIT_VALUE(c) (__ASSERT_(isXDIGIT(c)) \
2481 ((NATIVE_TO_LATIN1(c) >> 6) & 1) /* 1 if alpha; 0 if not */ \
2482 + ((NATIVE_TO_LATIN1(c) >> 3) & 8) /* 8 if alpha; 0 if not */ \
2483 + ((c) & 0xF)) /* 0-9 if input valid hex digit */
2484
2485/* The argument is a string pointer, which is advanced. */
2486#define READ_XDIGIT(s) ((s)++, XDIGIT_VALUE(*((s) - 1)))
95a59cab 2487
cb27eebd
KW
2488/* Converts a character known to represent an octal digit (0-7) to its numeric
2489 * value. The input is validated only by an assert() in DEBUGGING builds. In
2490 * both ASCII and EBCDIC the last 3 bits of the octal digits range from 0-7. */
2491#define OCTAL_VALUE(c) (__ASSERT_(isOCTAL(c)) (7 & (c)))
2492
305b8651 2493/* Efficiently returns a boolean as to if two native characters are equivalent
f1460a66 2494 * case-insensitively. At least one of the characters must be one of [A-Za-z];
305b8651
KW
2495 * the ALPHA in the name is to remind you of that. This is asserted() in
2496 * DEBUGGING builds. Because [A-Za-z] are invariant under UTF-8, this macro
2497 * works (on valid input) for both non- and UTF-8-encoded bytes.
2498 *
2499 * When one of the inputs is a compile-time constant and gets folded by the
2500 * compiler, this reduces to an AND and a TEST. On both EBCDIC and ASCII
2501 * machines, 'A' and 'a' differ by a single bit; the same with the upper and
2502 * lower case of all other ASCII-range alphabetics. On ASCII platforms, they
96ca48da
KW
2503 * are 32 apart; on EBCDIC, they are 64. At compile time, this uses an
2504 * exclusive 'or' to find that bit and then inverts it to form a mask, with
2505 * just a single 0, in the bit position where the upper- and lowercase differ.
2506 * */
305b8651
KW
2507#define isALPHA_FOLD_EQ(c1, c2) \
2508 (__ASSERT_(isALPHA_A(c1) || isALPHA_A(c2)) \
2509 ((c1) & ~('A' ^ 'a')) == ((c2) & ~('A' ^ 'a')))
2510#define isALPHA_FOLD_NE(c1, c2) (! isALPHA_FOLD_EQ((c1), (c2)))
2511
8e84507e 2512/*
3f620621 2513=for apidoc_section $memory
ccfc67b7 2514
a02a5408 2515=for apidoc Am|void|Newx|void* ptr|int nitems|type
25a3e84c 2516=for apidoc_item |void*|safemalloc|size_t size
c372fad0 2517
954c1994
GS
2518The XSUB-writer's interface to the C C<malloc> function.
2519
596f7718 2520Memory obtained by this should B<ONLY> be freed with L</"Safefree">.
0d7b2759 2521
c5008215
JC
2522In 5.9.3, Newx() and friends replace the older New() API, and drops
2523the first parameter, I<x>, a debug aid which allowed callers to identify
37b8b4c9 2524themselves. This aid has been superseded by a new build option,
d10b4965 2525PERL_MEM_LOG (see L<perlhacktips/PERL_MEM_LOG>). The older API is still
c5008215
JC
2526there for use in XS modules supporting older perls.
2527
a02a5408 2528=for apidoc Am|void|Newxc|void* ptr|int nitems|type|cast
954c1994 2529The XSUB-writer's interface to the C C<malloc> function, with
fbe13c60 2530cast. See also C<L</Newx>>.
954c1994 2531
596f7718 2532Memory obtained by this should B<ONLY> be freed with L</"Safefree">.
0d7b2759 2533
a02a5408 2534=for apidoc Am|void|Newxz|void* ptr|int nitems|type
c372fad0
TK
2535=for apidoc_item |void*|safecalloc|size_t nitems|size_t item_size
2536
954c1994 2537The XSUB-writer's interface to the C C<malloc> function. The allocated
fbe13c60 2538memory is zeroed with C<memzero>. See also C<L</Newx>>.
a02a5408 2539
596f7718 2540Memory obtained by this should B<ONLY> be freed with L</"Safefree">.
0d7b2759 2541
954c1994 2542=for apidoc Am|void|Renew|void* ptr|int nitems|type
c372fad0
TK
2543=for apidoc_item |void*|saferealloc|void *ptr|size_t size
2544
954c1994
GS
2545The XSUB-writer's interface to the C C<realloc> function.
2546
596f7718 2547Memory obtained by this should B<ONLY> be freed with L</"Safefree">.
0d7b2759 2548
954c1994
GS
2549=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
2550The XSUB-writer's interface to the C C<realloc> function, with
2551cast.
2552
596f7718 2553Memory obtained by this should B<ONLY> be freed with L</"Safefree">.
0d7b2759 2554
49b8b560 2555=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
2556The XSUB-writer's interface to the C C<free> function.
2557
596f7718 2558This should B<ONLY> be used on memory obtained using L</"Newx"> and friends.
0d7b2759 2559
3f620621 2560=for apidoc_section $string
75b94e77
KW
2561=for apidoc Am|void |Move |void* src|void* dest|int nitems|type
2562=for apidoc_item |void *|MoveD|void* src|void* dest|int nitems|type
954c1994 2563The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
926bb54c 2564source, C<dest> is the destination, C<nitems> is the number of items, and
fbe13c60 2565C<type> is the type. Can do overlapping moves. See also C<L</Copy>>.
954c1994 2566
75b94e77 2567C<MoveD> is like C<Move> but returns C<dest>. Useful
72d33970 2568for encouraging compilers to tail-call
e90e2364
NC
2569optimise.
2570
75b94e77
KW
2571=for apidoc Am|void |Copy |void* src|void* dest|int nitems|type
2572=for apidoc_item |void *|CopyD|void* src|void* dest|int nitems|type
954c1994 2573The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
926bb54c 2574source, C<dest> is the destination, C<nitems> is the number of items, and
fbe13c60 2575C<type> is the type. May fail on overlapping copies. See also C<L</Move>>.
954c1994 2576
75b94e77 2577C<CopyD> is like C<Copy> but returns C<dest>. Useful
72d33970 2578for encouraging compilers to tail-call
e90e2364
NC
2579optimise.
2580
75b94e77
KW
2581=for apidoc Am|void |Zero |void* dest|int nitems|type
2582=for apidoc_item |void *|ZeroD|void* dest|int nitems|type
954c1994
GS
2583
2584The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
2585destination, C<nitems> is the number of items, and C<type> is the type.
2586
75b94e77 2587C<ZeroD> is like C<Zero> but returns C<dest>. Useful
72d33970 2588for encouraging compilers to tail-call
e90e2364
NC
2589optimise.
2590
3f620621 2591=for apidoc_section $utility
e538104b 2592=for apidoc Amu|void|StructCopy|type *src|type *dest|type
4375e838 2593This is an architecture-independent macro to copy one structure to another.
954c1994 2594
7e337ee0
JH
2595=for apidoc Am|void|PoisonWith|void* dest|int nitems|type|U8 byte
2596
2597Fill up memory with a byte pattern (a byte repeated over and over
2598again) that hopefully catches attempts to access uninitialized memory.
2599
2600=for apidoc Am|void|PoisonNew|void* dest|int nitems|type
2601
2602PoisonWith(0xAB) for catching access to allocated but uninitialized memory.
2603
1c12ffb4 2604=for apidoc Am|void|PoisonFree|void* dest|int nitems|type
7e337ee0
JH
2605
2606PoisonWith(0xEF) for catching access to freed memory.
2607
9965345d
JH
2608=for apidoc Am|void|Poison|void* dest|int nitems|type
2609
7e337ee0 2610PoisonWith(0xEF) for catching access to freed memory.
9965345d
JH
2611
2612=cut */
954c1994 2613
561b68a9
SH
2614/* Maintained for backwards-compatibility only. Use newSV() instead. */
2615#ifndef PERL_CORE
ff06c60c 2616#define NEWSV(x,len) newSV(len)
561b68a9 2617#endif
ff06c60c 2618
b7112dce 2619#define MEM_SIZE_MAX ((MEM_SIZE)-1)
19a94d75 2620
a500027b 2621#define _PERL_STRLEN_ROUNDUP_UNCHECKED(n) (((n) - 1 + PERL_STRLEN_ROUNDUP_QUANTUM) & ~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM - 1))
e6bdf523 2622
27d5b266 2623#ifdef PERL_MALLOC_WRAP
e6bdf523
DM
2624
2625/* This expression will be constant-folded at compile time. It checks
2626 * whether or not the type of the count n is so small (e.g. U8 or U16, or
2627 * U32 on 64-bit systems) that there's no way a wrap-around could occur.
2628 * As well as avoiding the need for a run-time check in some cases, it's
2629 * designed to avoid compiler warnings like:
2630 * comparison is always false due to limited range of data type
73e8ff00
DM
2631 * It's mathematically equivalent to
2632 * max(n) * sizeof(t) > MEM_SIZE_MAX
e6bdf523
DM
2633 */
2634
2635# define _MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) \
445198b9
LM
2636 ( sizeof(MEM_SIZE) < sizeof(n) \
2637 || sizeof(t) > ((MEM_SIZE)1 << 8*(sizeof(MEM_SIZE) - sizeof(n))))
e6bdf523 2638
88f9f128 2639/* This is written in a slightly odd way to avoid various spurious
d98e5cde
DM
2640 * compiler warnings. We *want* to write the expression as
2641 * _MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) && (n > C)
2642 * (for some compile-time constant C), but even when the LHS
2643 * constant-folds to false at compile-time, g++ insists on emitting
2644 * warnings about the RHS (e.g. "comparison is always false"), so instead
2645 * we write it as
e6bdf523 2646 *
d98e5cde 2647 * (cond ? n : X) > C
88f9f128 2648 *
d98e5cde
DM
2649 * where X is a constant with X > C always false. Choosing a value for X
2650 * is tricky. If 0, some compilers will complain about 0 > C always being
2651 * false; if 1, Coverity complains when n happens to be the constant value
2652 * '1', that cond ? 1 : 1 has the same value on both branches; so use C
2653 * for X and hope that nothing else whines.
e6bdf523
DM
2654 */
2655
2656# define _MEM_WRAP_WILL_WRAP(n,t) \
88f9f128
DM
2657 ((_MEM_WRAP_NEEDS_RUNTIME_CHECK(n,t) ? (MEM_SIZE)(n) : \
2658 MEM_SIZE_MAX/sizeof(t)) > MEM_SIZE_MAX/sizeof(t))
e6bdf523
DM
2659
2660# define MEM_WRAP_CHECK(n,t) \
1604cfb0 2661 (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
e6bdf523
DM
2662 && (croak_memory_wrap(),0))
2663
2664# define MEM_WRAP_CHECK_1(n,t,a) \
1604cfb0
MS
2665 (void)(UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
2666 && (Perl_croak_nocontext("%s",(a)),0))
e6bdf523 2667
814eedc8
DD
2668/* "a" arg must be a string literal */
2669# define MEM_WRAP_CHECK_s(n,t,a) \
ca0572d7
KW
2670 ( (void) (UNLIKELY(_MEM_WRAP_WILL_WRAP(n,t)) \
2671 && (Perl_croak_nocontext(ASSERT_IS_LITERAL(a)), 0)))
814eedc8 2672
0622ec7a 2673# define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
27d5b266 2674
0622ec7a 2675# define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > MEM_SIZE_MAX - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (croak_memory_wrap(),0) : 0), _PERL_STRLEN_ROUNDUP_UNCHECKED(n))
27d5b266
JH
2676#else
2677
0622ec7a
KW
2678# define MEM_WRAP_CHECK(n,t)
2679# define MEM_WRAP_CHECK_1(n,t,a)
2680# define MEM_WRAP_CHECK_s(n,t,a)
2681# define MEM_WRAP_CHECK_(n,t)
8b44ba4c 2682
0622ec7a 2683# define PERL_STRLEN_ROUNDUP(n) _PERL_STRLEN_ROUNDUP_UNCHECKED(n)
27d5b266 2684
1936d2a7 2685#endif
8b44ba4c 2686
fe4f188c 2687#ifdef PERL_MEM_LOG
46c6c7e2 2688/*
9f653bb5 2689 * If PERL_MEM_LOG is defined, all Newx()s, Renew()s, and Safefree()s
46c6c7e2
JH
2690 * go through functions, which are handy for debugging breakpoints, but
2691 * which more importantly get the immediate calling environment (file and
e352bcff
JH
2692 * line number, and C function name if available) passed in. This info can
2693 * then be used for logging the calls, for which one gets a sample
73d1d973 2694 * implementation unless -DPERL_MEM_LOG_NOIMPL is also defined.
3609ea0d 2695 *
46c6c7e2 2696 * Known problems:
94e892a6 2697 * - not all memory allocs get logged, only those
46c6c7e2 2698 * that go through Newx() and derivatives (while all
94e892a6 2699 * Safefrees do get logged)
46c6c7e2
JH
2700 * - __FILE__ and __LINE__ do not work everywhere
2701 * - __func__ or __FUNCTION__ even less so
2702 * - I think more goes on after the perlio frees but
2703 * the thing is that STDERR gets closed (as do all
2704 * the file descriptors)
2705 * - no deeper calling stack than the caller of the Newx()
2706 * or the kind, but do I look like a C reflection/introspection
2707 * utility to you?
2708 * - the function prototypes for the logging functions
2709 * probably should maybe be somewhere else than handy.h
2710 * - one could consider inlining (macrofying) the logging
2711 * for speed, but I am too lazy
2712 * - one could imagine recording the allocations in a hash,
2713 * (keyed by the allocation address?), and maintain that
2714 * through reallocs and frees, but how to do that without
2715 * any News() happening...?
73d1d973 2716 * - lots of -Ddefines to get useful/controllable output
b953482e 2717 * - lots of ENV reads
46c6c7e2
JH
2718 */
2719
0b0ab801 2720# ifdef PERL_CORE
73d1d973 2721# ifndef PERL_MEM_LOG_NOIMPL
0b0ab801
MHM
2722enum mem_log_type {
2723 MLT_ALLOC,
2724 MLT_REALLOC,
d7a2c63c
MHM
2725 MLT_FREE,
2726 MLT_NEW_SV,
2727 MLT_DEL_SV
0b0ab801
MHM
2728};
2729# endif
12754f92 2730# if defined(PERL_IN_SV_C) /* those are only used in sv.c */
d7a2c63c
MHM
2731void Perl_mem_log_new_sv(const SV *sv, const char *filename, const int linenumber, const char *funcname);
2732void Perl_mem_log_del_sv(const SV *sv, const char *filename, const int linenumber, const char *funcname);
12754f92 2733# endif
0b0ab801
MHM
2734# endif
2735
fe4f188c
JH
2736#endif
2737
2738#ifdef PERL_MEM_LOG
d1401ee9
MHM
2739#define MEM_LOG_ALLOC(n,t,a) Perl_mem_log_alloc(n,sizeof(t),STRINGIFY(t),a,__FILE__,__LINE__,FUNCTION__)
2740#define MEM_LOG_REALLOC(n,t,v,a) Perl_mem_log_realloc(n,sizeof(t),STRINGIFY(t),v,a,__FILE__,__LINE__,FUNCTION__)
46c6c7e2 2741#define MEM_LOG_FREE(a) Perl_mem_log_free(a,__FILE__,__LINE__,FUNCTION__)
fe4f188c
JH
2742#endif
2743
2744#ifndef MEM_LOG_ALLOC
2745#define MEM_LOG_ALLOC(n,t,a) (a)
2746#endif
2747#ifndef MEM_LOG_REALLOC
2748#define MEM_LOG_REALLOC(n,t,v,a) (a)
2749#endif
2750#ifndef MEM_LOG_FREE
2751#define MEM_LOG_FREE(a) (a)
2752#endif
2753
d1401ee9
MHM
2754#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
2755#define Newxc(v,n,t,c) (v = (MEM_WRAP_CHECK_(n,t) (c*)MEM_LOG_ALLOC(n,t,safemalloc((MEM_SIZE)((n)*sizeof(t))))))
2756#define Newxz(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_ALLOC(n,t,safecalloc((n),sizeof(t)))))
a6f6820f
NC
2757
2758#ifndef PERL_CORE
a02a5408
JC
2759/* pre 5.9.x compatibility */
2760#define New(x,v,n,t) Newx(v,n,t)
2761#define Newc(x,v,n,t,c) Newxc(v,n,t,c)
4541904d 2762#define Newz(x,v,n,t) Newxz(v,n,t)
a6f6820f 2763#endif
a02a5408 2764
ff68c719 2765#define Renew(v,n,t) \
1604cfb0 2766 (v = (MEM_WRAP_CHECK_(n,t) (t*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
ff68c719 2767#define Renewc(v,n,t,c) \
1604cfb0 2768 (v = (MEM_WRAP_CHECK_(n,t) (c*)MEM_LOG_REALLOC(n,t,v,saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
94010e71
NC
2769
2770#ifdef PERL_POISON
2771#define Safefree(d) \
06c0cc96 2772 ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d))), Poison(&(d), 1, Malloc_t)) : (void) 0)
94010e71 2773#else
fe4f188c 2774#define Safefree(d) safefree(MEM_LOG_FREE((Malloc_t)(d)))
94010e71 2775#endif
55497cff 2776
dbb57106
YO
2777/* assert that a valid ptr has been supplied - use this instead of assert(ptr) *
2778 * as it handles cases like constant string arguments without throwing warnings *
2779 * the cast is required, as is the inequality check, to avoid warnings */
45908e4d 2780#define perl_assert_ptr(p) assert( ((void*)(p)) != 0 )
55497cff 2781
45908e4d
YO
2782
2783#define Move(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
2784#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
2785#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), (void)memzero((char*)(d), (n) * sizeof(t)))
2786
bdd1531d 2787/* Like above, but returns a pointer to 'd' */
45908e4d
YO
2788#define MoveD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
2789#define CopyD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
45908e4d 2790#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), memzero((char*)(d), (n) * sizeof(t)))
e90e2364 2791
7e337ee0
JH
2792#define PoisonWith(d,n,t,b) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), (U8)(b), (n) * sizeof(t)))
2793#define PoisonNew(d,n,t) PoisonWith(d,n,t,0xAB)
2794#define PoisonFree(d,n,t) PoisonWith(d,n,t,0xEF)
2795#define Poison(d,n,t) PoisonFree(d,n,t)
27d5b266 2796
caa674f3
DD
2797#ifdef PERL_POISON
2798# define PERL_POISON_EXPR(x) x
2799#else
2800# define PERL_POISON_EXPR(x)
2801#endif
2802
be319be3 2803/* Shallow copy */
ff68c719 2804#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
2cc61e15 2805
1b7e2294 2806/*
3f620621 2807=for apidoc_section $utility
1b7e2294
KW
2808
2809=for apidoc Am|STRLEN|C_ARRAY_LENGTH|void *a
2810
2811Returns the number of elements in the input C array (so you want your
2812zero-based indices to be less than but not equal to).
2813
2814=for apidoc Am|void *|C_ARRAY_END|void *a
2815
2816Returns a pointer to one element past the final element of the input C array.
2817
2818=cut
2819
2820C_ARRAY_END is one past the last: half-open/half-closed range, not
2821last-inclusive range.
2822*/
622913ab 2823#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
c3caa5c3 2824#define C_ARRAY_END(a) ((a) + C_ARRAY_LENGTH(a))
622913ab 2825
ff00d73b
KW
2826#if defined(PERL_CORE) || defined(PERL_EXT_RE_BUILD)
2827/* strlen() of a literal string constant. Restricting this to core, in part
2828 * because it can generate compiler warnings about comparing unlike signs */
2829# define STRLENs(s) (sizeof("" s "") - 1)
2830#endif
2831
2cc61e15
DD
2832#ifdef NEED_VA_COPY
2833# ifdef va_copy
2834# define Perl_va_copy(s, d) va_copy(d, s)
07798b17
AC
2835# elif defined(__va_copy)
2836# define Perl_va_copy(s, d) __va_copy(d, s)
2cc61e15 2837# else
07798b17 2838# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
2cc61e15
DD
2839# endif
2840#endif
2841
472d47bc
SB
2842/* convenience debug macros */
2843#ifdef USE_ITHREADS
2844#define pTHX_FORMAT "Perl interpreter: 0x%p"
2845#define pTHX__FORMAT ", Perl interpreter: 0x%p"
f54cb97a
AL
2846#define pTHX_VALUE_ (void *)my_perl,
2847#define pTHX_VALUE (void *)my_perl
2848#define pTHX__VALUE_ ,(void *)my_perl,
2849#define pTHX__VALUE ,(void *)my_perl
472d47bc 2850#else
3609ea0d 2851#define pTHX_FORMAT
472d47bc 2852#define pTHX__FORMAT
3609ea0d 2853#define pTHX_VALUE_
472d47bc 2854#define pTHX_VALUE
3609ea0d 2855#define pTHX__VALUE_
472d47bc
SB
2856#define pTHX__VALUE
2857#endif /* USE_ITHREADS */
3609ea0d 2858
2acdbac1
NC
2859/* Perl_deprecate was not part of the public API, and did not have a deprecate()
2860 shortcut macro defined without -DPERL_CORE. Neither codesearch.google.com nor
2861 CPAN::Unpack show any users outside the core. */
2862#ifdef PERL_CORE
dc6e8de0
A
2863# define deprecate(s) Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), \
2864 "Use of " s " is deprecated")
c9680906
A
2865# define deprecate_disappears_in(when,message) \
2866 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), \
81a93f7e 2867 message " is deprecated, and will disappear in Perl " when)
ac641426
A
2868# define deprecate_fatal_in(when,message) \
2869 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), \
81a93f7e 2870 message " is deprecated, and will become fatal in Perl " when)
2acdbac1
NC
2871#endif
2872
dfff4baf
BF
2873/* Internal macros to deal with gids and uids */
2874#ifdef PERL_CORE
2875
2876# if Uid_t_size > IVSIZE
2877# define sv_setuid(sv, uid) sv_setnv((sv), (NV)(uid))
2878# define SvUID(sv) SvNV(sv)
07798b17
AC
2879# elif Uid_t_sign <= 0
2880# define sv_setuid(sv, uid) sv_setiv((sv), (IV)(uid))
2881# define SvUID(sv) SvIV(sv)
dfff4baf 2882# else
07798b17
AC
2883# define sv_setuid(sv, uid) sv_setuv((sv), (UV)(uid))
2884# define SvUID(sv) SvUV(sv)
dfff4baf
BF
2885# endif /* Uid_t_size */
2886
2887# if Gid_t_size > IVSIZE
2888# define sv_setgid(sv, gid) sv_setnv((sv), (NV)(gid))
2889# define SvGID(sv) SvNV(sv)
07798b17
AC
2890# elif Gid_t_sign <= 0
2891# define sv_setgid(sv, gid) sv_setiv((sv), (IV)(gid))
2892# define SvGID(sv) SvIV(sv)
dfff4baf 2893# else
07798b17
AC
2894# define sv_setgid(sv, gid) sv_setuv((sv), (UV)(gid))
2895# define SvGID(sv) SvUV(sv)
dfff4baf
BF
2896# endif /* Gid_t_size */
2897
2898#endif
2899
6a5bc5ac 2900#endif /* PERL_HANDY_H_ */
9d745869 2901
e9a8c099 2902/*
14d04a33 2903 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 2904 */