This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_pad_findlex has side effects, so its return value may be ignored
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
1d325971 4 * 2000, 2001, 2002, 2004, 2005 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
85e6fe83 11#if !defined(__STDC__)
378cc40b
LW
12#ifdef NULL
13#undef NULL
14#endif
a687059c
LW
15#ifndef I286
16# define NULL 0
17#else
18# define NULL 0L
19#endif
85e6fe83
LW
20#endif
21
378cc40b 22#define Null(type) ((type)NULL)
954c1994
GS
23
24/*
ccfc67b7 25=head1 Handy Values
954c1994 26
ccfc67b7
JH
27=for apidoc AmU||Nullch
28Null character pointer.
2307c6d0 29
954c1994
GS
30=for apidoc AmU||Nullsv
31Null SV pointer.
32
33=cut
34*/
35
8d063cd8 36#define Nullch Null(char*)
760ac839 37#define Nullfp Null(PerlIO*)
79072805 38#define Nullsv Null(SV*)
8d063cd8 39
641d3f0b
PP
40#ifdef TRUE
41#undef TRUE
42#endif
43#ifdef FALSE
44#undef FALSE
45#endif
46#define TRUE (1)
47#define FALSE (0)
48
27d4fb96
PP
49
50/* XXX Configure ought to have a test for a boolean type, if I can
51 just figure out all the headers such a test needs.
52 Andy Dougherty August 1996
53*/
8e84507e 54/* bool is built-in for g++-2.6.3 and later, which might be used
c1d22f6b
GS
55 for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
56 be sure _G_config.h will be included before this file. _G_config.h
8e84507e 57 also defines _G_HAVE_BOOL for both gcc and g++, but only g++
c1d22f6b
GS
58 actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
59 g++ can be identified by __GNUG__.
60 Andy Dougherty February 2000
5d94fbed 61*/
c1d22f6b 62#ifdef __GNUG__ /* GNU g++ has bool built-in */
5d94fbed 63# ifndef HAS_BOOL
c1d22f6b 64# define HAS_BOOL 1
5d94fbed 65# endif
5d94fbed
AD
66#endif
67
641d3f0b
PP
68/* The NeXT dynamic loader headers will not build with the bool macro
69 So declare them now to clear confusion.
70*/
8f1f23e8 71#if defined(NeXT) || defined(__NeXT__)
641d3f0b
PP
72# undef FALSE
73# undef TRUE
74 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
75# define ENUM_BOOL 1
76# ifndef HAS_BOOL
77# define HAS_BOOL 1
78# endif /* !HAS_BOOL */
8f1f23e8 79#endif /* NeXT || __NeXT__ */
641d3f0b 80
5d94fbed 81#ifndef HAS_BOOL
61bb5906 82# if defined(UTS) || defined(VMS)
5d94fbed
AD
83# define bool int
84# else
85# define bool char
86# endif
c1d22f6b 87# define HAS_BOOL 1
a687059c 88#endif
0d3e774c 89
46c6c7e2
JH
90/* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
91 * XXX Should really be a Configure probe. */
92#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
93# define FUNCTION__ __func__
94#else
95# if (defined(_MSC_VER) && _MSC_VER < 1300) || /* Pre-MSVC 7.0 has neither __func__ nor __FUNCTION and no good workarounds, either. */ \
96 (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used. */
97# define FUNCTION__ ""
98# else
99# define FUNCTION__ __FUNCTION__ /* Common extension. */
100# endif
101#endif
102
27d4fb96
PP
103/* XXX A note on the perl source internal type system. The
104 original intent was that I32 be *exactly* 32 bits.
105
106 Currently, we only guarantee that I32 is *at least* 32 bits.
107 Specifically, if int is 64 bits, then so is I32. (This is the case
108 for the Cray.) This has the advantage of meshing nicely with
109 standard library calls (where we pass an I32 and the library is
110 expecting an int), but the disadvantage that an I32 is not 32 bits.
111 Andy Dougherty August 1996
24fef2a7 112
dc45a647
MB
113 There is no guarantee that there is *any* integral type with
114 exactly 32 bits. It is perfectly legal for a system to have
115 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 116
dc45a647
MB
117 Similarly, there is no guarantee that I16 and U16 have exactly 16
118 bits.
693762b4 119
8e84507e
NIS
120 For dealing with issues that may arise from various 32/64-bit
121 systems, we will ask Configure to check out
8175356b 122
dc45a647
MB
123 SHORTSIZE == sizeof(short)
124 INTSIZE == sizeof(int)
125 LONGSIZE == sizeof(long)
126 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
127 PTRSIZE == sizeof(void *)
128 DOUBLESIZE == sizeof(double)
129 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 130
27d4fb96
PP
131*/
132
69512466
JH
133#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
134# include <inttypes.h>
dd0eed91
JH
135# ifdef INT32_MIN_BROKEN
136# undef INT32_MIN
137# define INT32_MIN (-2147483647-1)
138# endif
139# ifdef INT64_MIN_BROKEN
140# undef INT64_MIN
141# define INT64_MIN (-9223372036854775807LL-1)
142# endif
69512466
JH
143#endif
144
8175356b
JH
145typedef I8TYPE I8;
146typedef U8TYPE U8;
147typedef I16TYPE I16;
148typedef U16TYPE U16;
149typedef I32TYPE I32;
150typedef U32TYPE U32;
6b8eaf93
JH
151#ifdef PERL_CORE
152# ifdef HAS_QUAD
8175356b
JH
153typedef I64TYPE I64;
154typedef U64TYPE U64;
6b8eaf93
JH
155# endif
156#endif /* PERL_CORE */
8175356b 157
69512466
JH
158#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
159# ifndef UINT64_C /* usually from <inttypes.h> */
160# if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
161# define INT64_C(c) CAT2(c,LL)
162# define UINT64_C(c) CAT2(c,ULL)
163# else
164# if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
165# define INT64_C(c) CAT2(c,L)
166# define UINT64_C(c) CAT2(c,UL)
167# else
168# define INT64_C(c) ((I64TYPE)(c))
169# define UINT64_C(c) ((U64TYPE)(c))
170# endif
171# endif
e8c95190
JH
172# endif
173#endif
dc750f2e 174
cae3d67c 175/* HMB H.Merijn Brand - a placeholder for preparing Configure patches */
0a0abfba 176#if defined(LIBM_LIB_VERSION)
cae3d67c 177/* Not (yet) used at top level, but mention them for metaconfig */
dc750f2e 178#endif
e8c95190 179
a22e52b9
JH
180/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
181 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
182
d8668976 183#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 184
5ff3f7a4
GS
185/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
186 Please search CHAR_MAX in perl.h for further details. */
187#define U8_MAX UINT8_MAX
188#define U8_MIN UINT8_MIN
189
5ff3f7a4
GS
190#define I16_MAX INT16_MAX
191#define I16_MIN INT16_MIN
192#define U16_MAX UINT16_MAX
193#define U16_MIN UINT16_MIN
194
5ff3f7a4
GS
195#define I32_MAX INT32_MAX
196#define I32_MIN INT32_MIN
0e983133
GS
197#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
198# define U32_MAX UINT32_MAX
199#else
200# define U32_MAX 4294967295U
201#endif
5ff3f7a4
GS
202#define U32_MIN UINT32_MIN
203
204#else
205
5c9fa16e
KA
206/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
207 Please search CHAR_MAX in perl.h for further details. */
27d4fb96
PP
208#define U8_MAX PERL_UCHAR_MAX
209#define U8_MIN PERL_UCHAR_MIN
79072805 210
27d4fb96
PP
211#define I16_MAX PERL_SHORT_MAX
212#define I16_MIN PERL_SHORT_MIN
213#define U16_MAX PERL_USHORT_MAX
214#define U16_MIN PERL_USHORT_MIN
79072805 215
c4f23d77 216#if LONGSIZE > 4
27d4fb96
PP
217# define I32_MAX PERL_INT_MAX
218# define I32_MIN PERL_INT_MIN
219# define U32_MAX PERL_UINT_MAX
220# define U32_MIN PERL_UINT_MIN
79072805 221#else
27d4fb96
PP
222# define I32_MAX PERL_LONG_MAX
223# define I32_MIN PERL_LONG_MIN
224# define U32_MAX PERL_ULONG_MAX
225# define U32_MIN PERL_ULONG_MIN
79072805
LW
226#endif
227
5ff3f7a4
GS
228#endif
229
58a9a5d5 230/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
fc36a67e
PP
231#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
232#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
233#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
234
ff68c719 235#define Ctl(ch) ((ch) & 037)
8d063cd8 236
954c1994 237/*
ccfc67b7
JH
238=head1 Miscellaneous Functions
239
954c1994
GS
240=for apidoc Am|bool|strNE|char* s1|char* s2
241Test two strings to see if they are different. Returns true or
242false.
243
244=for apidoc Am|bool|strEQ|char* s1|char* s2
245Test two strings to see if they are equal. Returns true or false.
246
247=for apidoc Am|bool|strLT|char* s1|char* s2
248Test two strings to see if the first, C<s1>, is less than the second,
249C<s2>. Returns true or false.
250
251=for apidoc Am|bool|strLE|char* s1|char* s2
252Test two strings to see if the first, C<s1>, is less than or equal to the
253second, C<s2>. Returns true or false.
254
255=for apidoc Am|bool|strGT|char* s1|char* s2
256Test two strings to see if the first, C<s1>, is greater than the second,
257C<s2>. Returns true or false.
258
259=for apidoc Am|bool|strGE|char* s1|char* s2
260Test two strings to see if the first, C<s1>, is greater than or equal to
261the second, C<s2>. Returns true or false.
262
263=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
264Test two strings to see if they are different. The C<len> parameter
265indicates the number of bytes to compare. Returns true or false. (A
266wrapper for C<strncmp>).
267
268=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
269Test two strings to see if they are equal. The C<len> parameter indicates
270the number of bytes to compare. Returns true or false. (A wrapper for
271C<strncmp>).
272
273=cut
274*/
275
8d063cd8
LW
276#define strNE(s1,s2) (strcmp(s1,s2))
277#define strEQ(s1,s2) (!strcmp(s1,s2))
278#define strLT(s1,s2) (strcmp(s1,s2) < 0)
279#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
280#define strGT(s1,s2) (strcmp(s1,s2) > 0)
281#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
282#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
283#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 284
36477c24
PP
285#ifdef HAS_MEMCMP
286# define memNE(s1,s2,l) (memcmp(s1,s2,l))
287# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
288#else
289# define memNE(s1,s2,l) (bcmp(s1,s2,l))
290# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
291#endif
292
bbce6d69
PP
293/*
294 * Character classes.
295 *
296 * Unfortunately, the introduction of locales means that we
297 * can't trust isupper(), etc. to tell the truth. And when
298 * it comes to /\w+/ with tainting enabled, we *must* be able
299 * to trust our character classes.
300 *
301 * Therefore, the default tests in the text of Perl will be
302 * independent of locale. Any code that wants to depend on
303 * the current locale will use the tests that begin with "lc".
304 */
305
2304df62
AD
306#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
307# ifndef CTYPE256
308# define CTYPE256
309# endif
310#endif
311
954c1994 312/*
ccfc67b7
JH
313
314=head1 Character classes
315
954c1994 316=for apidoc Am|bool|isALNUM|char ch
4375e838 317Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 318character (including underscore) or digit.
954c1994
GS
319
320=for apidoc Am|bool|isALPHA|char ch
4375e838 321Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994
GS
322character.
323
324=for apidoc Am|bool|isSPACE|char ch
325Returns a boolean indicating whether the C C<char> is whitespace.
326
327=for apidoc Am|bool|isDIGIT|char ch
4375e838 328Returns a boolean indicating whether the C C<char> is an ASCII
954c1994
GS
329digit.
330
331=for apidoc Am|bool|isUPPER|char ch
332Returns a boolean indicating whether the C C<char> is an uppercase
333character.
334
335=for apidoc Am|bool|isLOWER|char ch
336Returns a boolean indicating whether the C C<char> is a lowercase
337character.
338
339=for apidoc Am|char|toUPPER|char ch
340Converts the specified character to uppercase.
341
342=for apidoc Am|char|toLOWER|char ch
343Converts the specified character to lowercase.
344
345=cut
346*/
347
bbce6d69
PP
348#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
349#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
350#define isALPHA(c) (isUPPER(c) || isLOWER(c))
351#define isSPACE(c) \
352 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e
JF
353#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
354#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 355#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7
JH
356#ifdef EBCDIC
357 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
358# define isUPPER(c) isupper(c)
359# define isLOWER(c) islower(c)
b8c5462f
JH
360# define isALNUMC(c) isalnum(c)
361# define isASCII(c) isascii(c)
362# define isCNTRL(c) iscntrl(c)
363# define isGRAPH(c) isgraph(c)
9d116dd7 364# define isPRINT(c) isprint(c)
b8c5462f
JH
365# define isPUNCT(c) ispunct(c)
366# define isXDIGIT(c) isxdigit(c)
9d116dd7
JH
367# define toUPPER(c) toupper(c)
368# define toLOWER(c) tolower(c)
369#else
370# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
371# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f
JH
372# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
373# define isASCII(c) ((c) <= 127)
7be5a6cf 374# define isCNTRL(c) ((c) < ' ' || (c) == 127)
b8c5462f 375# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
f79b3095 376# define isPRINT(c) (((c) > 32 && (c) < 127) || (c) == ' ')
b8c5462f 377# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
c302d089 378# define isXDIGIT(c) (isDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7
JH
379# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
380# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
381#endif
bbce6d69
PP
382
383#ifdef USE_NEXT_CTYPE
384
385# define isALNUM_LC(c) \
37bd1396 386 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719
PP
387# define isIDFIRST_LC(c) \
388 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
389# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
390# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
391# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
392# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
393# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 394# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f
JH
395# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
396# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 397# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 398# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719
PP
399# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
400# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69
PP
401
402#else /* !USE_NEXT_CTYPE */
b8c5462f 403
bbce6d69
PP
404# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
405
b8c5462f 406# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 407# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69
PP
408# define isALPHA_LC(c) isalpha((unsigned char)(c))
409# define isSPACE_LC(c) isspace((unsigned char)(c))
410# define isDIGIT_LC(c) isdigit((unsigned char)(c))
411# define isUPPER_LC(c) isupper((unsigned char)(c))
412# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f
JH
413# define isALNUMC_LC(c) isalnum((unsigned char)(c))
414# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
415# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 416# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 417# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69
PP
418# define toUPPER_LC(c) toupper((unsigned char)(c))
419# define toLOWER_LC(c) tolower((unsigned char)(c))
420
421# else
422
b8c5462f 423# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69
PP
424# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
425# define isALPHA_LC(c) (isascii(c) && isalpha(c))
426# define isSPACE_LC(c) (isascii(c) && isspace(c))
427# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
428# define isUPPER_LC(c) (isascii(c) && isupper(c))
429# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f
JH
430# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
431# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
432# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 433# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 434# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69
PP
435# define toUPPER_LC(c) toupper(c)
436# define toLOWER_LC(c) tolower(c)
437
438# endif
a0d0e21e 439#endif /* USE_NEXT_CTYPE */
55204971 440
aaa51d5e
JF
441#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
442#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
443
a0ed51b3
LW
444#define isALNUM_uni(c) is_uni_alnum(c)
445#define isIDFIRST_uni(c) is_uni_idfirst(c)
446#define isALPHA_uni(c) is_uni_alpha(c)
447#define isSPACE_uni(c) is_uni_space(c)
448#define isDIGIT_uni(c) is_uni_digit(c)
449#define isUPPER_uni(c) is_uni_upper(c)
450#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f
JH
451#define isALNUMC_uni(c) is_uni_alnumc(c)
452#define isASCII_uni(c) is_uni_ascii(c)
453#define isCNTRL_uni(c) is_uni_cntrl(c)
454#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 455#define isPRINT_uni(c) is_uni_print(c)
b8c5462f
JH
456#define isPUNCT_uni(c) is_uni_punct(c)
457#define isXDIGIT_uni(c) is_uni_xdigit(c)
a2a2844f
JH
458#define toUPPER_uni(c,s,l) to_uni_upper(c,s,l)
459#define toTITLE_uni(c,s,l) to_uni_title(c,s,l)
460#define toLOWER_uni(c,s,l) to_uni_lower(c,s,l)
b8d68ded 461#define toFOLD_uni(c,s,l) to_uni_fold(c,s,l)
a0ed51b3 462
aaa51d5e
JF
463#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
464#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
465
9041c2e3
NIS
466#define isALNUM_LC_uvchr(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
467#define isIDFIRST_LC_uvchr(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
468#define isALPHA_LC_uvchr(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
469#define isSPACE_LC_uvchr(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
470#define isDIGIT_LC_uvchr(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
471#define isUPPER_LC_uvchr(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
472#define isLOWER_LC_uvchr(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
473#define isALNUMC_LC_uvchr(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
474#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
475#define isGRAPH_LC_uvchr(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
476#define isPRINT_LC_uvchr(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
477#define isPUNCT_LC_uvchr(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
a0ed51b3 478
aaa51d5e
JF
479#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
480#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
481
a0ed51b3 482#define isALNUM_utf8(p) is_utf8_alnum(p)
82686b01
JH
483/* The ID_Start of Unicode is quite limiting: it assumes a L-class
484 * character (meaning that you cannot have, say, a CJK character).
485 * Instead, let's allow ID_Continue but not digits. */
486#define isIDFIRST_utf8(p) (is_utf8_idcont(p) && !is_utf8_digit(p))
a0ed51b3
LW
487#define isALPHA_utf8(p) is_utf8_alpha(p)
488#define isSPACE_utf8(p) is_utf8_space(p)
489#define isDIGIT_utf8(p) is_utf8_digit(p)
490#define isUPPER_utf8(p) is_utf8_upper(p)
491#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f
JH
492#define isALNUMC_utf8(p) is_utf8_alnumc(p)
493#define isASCII_utf8(p) is_utf8_ascii(p)
494#define isCNTRL_utf8(p) is_utf8_cntrl(p)
495#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 496#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f
JH
497#define isPUNCT_utf8(p) is_utf8_punct(p)
498#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a2a2844f
JH
499#define toUPPER_utf8(p,s,l) to_utf8_upper(p,s,l)
500#define toTITLE_utf8(p,s,l) to_utf8_title(p,s,l)
501#define toLOWER_utf8(p,s,l) to_utf8_lower(p,s,l)
a0ed51b3 502
aaa51d5e
JF
503#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
504#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
505
9041c2e3
NIS
506#define isALNUM_LC_utf8(p) isALNUM_LC_uvchr(utf8_to_uvchr(p, 0))
507#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uvchr(utf8_to_uvchr(p, 0))
508#define isALPHA_LC_utf8(p) isALPHA_LC_uvchr(utf8_to_uvchr(p, 0))
509#define isSPACE_LC_utf8(p) isSPACE_LC_uvchr(utf8_to_uvchr(p, 0))
510#define isDIGIT_LC_utf8(p) isDIGIT_LC_uvchr(utf8_to_uvchr(p, 0))
511#define isUPPER_LC_utf8(p) isUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
512#define isLOWER_LC_utf8(p) isLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
513#define isALNUMC_LC_utf8(p) isALNUMC_LC_uvchr(utf8_to_uvchr(p, 0))
514#define isCNTRL_LC_utf8(p) isCNTRL_LC_uvchr(utf8_to_uvchr(p, 0))
515#define isGRAPH_LC_utf8(p) isGRAPH_LC_uvchr(utf8_to_uvchr(p, 0))
516#define isPRINT_LC_utf8(p) isPRINT_LC_uvchr(utf8_to_uvchr(p, 0))
517#define isPUNCT_LC_utf8(p) isPUNCT_LC_uvchr(utf8_to_uvchr(p, 0))
a0ed51b3 518
aaa51d5e
JF
519#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
520#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
521
9d116dd7 522#ifdef EBCDIC
20b634c2
JH
523# ifdef PERL_IMPLICIT_CONTEXT
524# define toCTRL(c) Perl_ebcdic_control(aTHX_ c)
525# else
526# define toCTRL Perl_ebcdic_control
527# endif
9d116dd7
JH
528#else
529 /* This conversion works both ways, strangely enough. */
530# define toCTRL(c) (toUPPER(c) ^ 64)
531#endif
bbce6d69 532
dea28490
JJ
533/* Line numbers are unsigned, 32 bits. */
534typedef U32 line_t;
dea28490 535#define NOLINE ((line_t) 4294967295UL)
378cc40b 536
8c52afec 537
8e84507e 538/*
ccfc67b7
JH
539=head1 SV Manipulation Functions
540
954c1994
GS
541=for apidoc Am|SV*|NEWSV|int id|STRLEN len
542Creates a new SV. A non-zero C<len> parameter indicates the number of
543bytes of preallocated string space the SV should have. An extra byte for a
544tailing NUL is also reserved. (SvPOK is not set for the SV even if string
8e84507e 545space is allocated.) The reference count for the new SV is set to 1.
954c1994
GS
546C<id> is an integer id between 0 and 1299 (used to identify leaks).
547
ccfc67b7
JH
548=head1 Memory Management
549
a02a5408 550=for apidoc Am|void|Newx|void* ptr|int nitems|type
954c1994
GS
551The XSUB-writer's interface to the C C<malloc> function.
552
c5008215
JC
553In 5.9.3, Newx() and friends replace the older New() API, and drops
554the first parameter, I<x>, a debug aid which allowed callers to identify
555themselves. This aid has been superceded by a new build option,
556PERL_MEM_LOG (see L<perlhack/PERL_MEM_LOG>). The older API is still
557there for use in XS modules supporting older perls.
558
a02a5408 559=for apidoc Am|void|Newxc|void* ptr|int nitems|type|cast
954c1994 560The XSUB-writer's interface to the C C<malloc> function, with
c5008215 561cast. See also C<Newx>.
954c1994 562
a02a5408 563=for apidoc Am|void|Newxz|void* ptr|int nitems|type
954c1994 564The XSUB-writer's interface to the C C<malloc> function. The allocated
c5008215 565memory is zeroed with C<memzero>. See also C<Newx>.
a02a5408 566
954c1994
GS
567=for apidoc Am|void|Renew|void* ptr|int nitems|type
568The XSUB-writer's interface to the C C<realloc> function.
569
570=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
571The XSUB-writer's interface to the C C<realloc> function, with
572cast.
573
49b8b560 574=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
575The XSUB-writer's interface to the C C<free> function.
576
577=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
578The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
579source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
580the type. Can do overlapping moves. See also C<Copy>.
581
e90e2364
NC
582=for apidoc Am|void *|MoveD|void* src|void* dest|int nitems|type
583Like C<Move> but returns dest. Useful for encouraging compilers to tail-call
584optimise.
585
954c1994
GS
586=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
587The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
588source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
589the type. May fail on overlapping copies. See also C<Move>.
590
e90e2364
NC
591=for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
592
593Like C<Copy> but returns dest. Useful for encouraging compilers to tail-call
594optimise.
595
954c1994
GS
596=for apidoc Am|void|Zero|void* dest|int nitems|type
597
598The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
599destination, C<nitems> is the number of items, and C<type> is the type.
600
e90e2364
NC
601=for apidoc Am|void *|ZeroD|void* dest|int nitems|type
602
603Like C<Zero> but returns dest. Useful for encouraging compilers to tail-call
604optimise.
605
954c1994 606=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 607This is an architecture-independent macro to copy one structure to another.
954c1994 608
9965345d
JH
609=for apidoc Am|void|Poison|void* dest|int nitems|type
610
611Fill up memory with a pattern (byte 0xAB over and over again) that
612hopefully catches attempts to access uninitialized memory.
613
614=cut */
954c1994 615
ff06c60c
IZ
616#define NEWSV(x,len) newSV(len)
617
27d5b266
JH
618#ifdef PERL_MALLOC_WRAP
619#define MEM_WRAP_CHECK(n,t) \
ed87b6ed 620 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(PL_memory_wrap),0):0)
27d5b266 621#define MEM_WRAP_CHECK_1(n,t,a) \
ed87b6ed 622 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a),0):0)
27d5b266 623#define MEM_WRAP_CHECK_2(n,t,a,b) \
ed87b6ed 624 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a,b),0):0)
8b44ba4c 625#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
27d5b266 626
6fff79ce 627#define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > (MEM_SIZE)~0 - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (Perl_croak_nocontext(PL_memory_wrap),0):0),((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
1936d2a7 628
27d5b266
JH
629#else
630
410319be
NC
631#define MEM_WRAP_CHECK(n,t)
632#define MEM_WRAP_CHECK_1(n,t,a)
633#define MEM_WRAP_CHECK_2(n,t,a,b)
8b44ba4c
NC
634#define MEM_WRAP_CHECK_(n,t)
635
6fff79ce 636#define PERL_STRLEN_ROUNDUP(n) (((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
27d5b266 637
1936d2a7 638#endif
8b44ba4c 639
fe4f188c 640#ifdef PERL_MEM_LOG
46c6c7e2 641/*
9f653bb5 642 * If PERL_MEM_LOG is defined, all Newx()s, Renew()s, and Safefree()s
46c6c7e2
JH
643 * go through functions, which are handy for debugging breakpoints, but
644 * which more importantly get the immediate calling environment (file and
645 * line number) passed in. This can then be used for logging the calls,
646 * for which one can get a sample implementation if PERL_MEM_LOG_STDERR
647 * is defined.
648 *
649 * Known problems:
650 * - all memory allocs do not get logged, only those
651 * that go through Newx() and derivatives (while all
652 * Safefrees do get logged)
653 * - __FILE__ and __LINE__ do not work everywhere
654 * - __func__ or __FUNCTION__ even less so
655 * - I think more goes on after the perlio frees but
656 * the thing is that STDERR gets closed (as do all
657 * the file descriptors)
658 * - no deeper calling stack than the caller of the Newx()
659 * or the kind, but do I look like a C reflection/introspection
660 * utility to you?
661 * - the function prototypes for the logging functions
662 * probably should maybe be somewhere else than handy.h
663 * - one could consider inlining (macrofying) the logging
664 * for speed, but I am too lazy
665 * - one could imagine recording the allocations in a hash,
666 * (keyed by the allocation address?), and maintain that
667 * through reallocs and frees, but how to do that without
668 * any News() happening...?
669 */
670
671Malloc_t Perl_mem_log_alloc(const UV n, const UV typesize, const char *typename, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
672
673Malloc_t Perl_mem_log_realloc(const UV n, const UV typesize, const char *typename, Malloc_t oldalloc, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
674
675Malloc_t Perl_mem_log_free(Malloc_t oldalloc, const char *filename, const int linenumber, const char *funcname);
676
fe4f188c
JH
677#endif
678
679#ifdef PERL_MEM_LOG
46c6c7e2
JH
680#define MEM_LOG_ALLOC(n,t,a) Perl_mem_log_alloc(n,sizeof(t),STRINGIFY(t),a,__FILE__,__LINE__,FUNCTION__)
681#define MEM_LOG_REALLOC(n,t,v,a) Perl_mem_log_realloc(n,sizeof(t),STRINGIFY(t),v,a,__FILE__,__LINE__,FUNCTION__)
682#define MEM_LOG_FREE(a) Perl_mem_log_free(a,__FILE__,__LINE__,FUNCTION__)
fe4f188c
JH
683#endif
684
685#ifndef MEM_LOG_ALLOC
686#define MEM_LOG_ALLOC(n,t,a) (a)
687#endif
688#ifndef MEM_LOG_REALLOC
689#define MEM_LOG_REALLOC(n,t,v,a) (a)
690#endif
691#ifndef MEM_LOG_FREE
692#define MEM_LOG_FREE(a) (a)
693#endif
694
695#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
696#define Newxc(v,n,t,c) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
697#define Newxz(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))))), \
ff68c719 698 memzero((char*)(v), (n)*sizeof(t))
a02a5408
JC
699/* pre 5.9.x compatibility */
700#define New(x,v,n,t) Newx(v,n,t)
701#define Newc(x,v,n,t,c) Newxc(v,n,t,c)
aab6a430 702#define Newz(x,v,n,t,c) Newxz(v,n,t,c)
a02a5408 703
ff68c719 704#define Renew(v,n,t) \
fe4f188c 705 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
ff68c719 706#define Renewc(v,n,t,c) \
fe4f188c 707 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
94010e71
NC
708
709#ifdef PERL_POISON
710#define Safefree(d) \
fe4f188c 711 ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d)))), Poison(&(d), 1, Malloc_t)) : (void) 0)
94010e71 712#else
fe4f188c 713#define Safefree(d) safefree(MEM_LOG_FREE((Malloc_t)(d)))
94010e71 714#endif
55497cff 715
8b44ba4c
NC
716#define Move(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
717#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
718#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
55497cff 719
8b44ba4c
NC
720#define MoveD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
721#define CopyD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
e90e2364 722#ifdef HAS_MEMSET
8b44ba4c 723#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)))
e90e2364 724#else
8b44ba4c
NC
725/* Using bzero(), which returns void. */
726#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)),d)
e90e2364
NC
727#endif
728
8b44ba4c 729#define Poison(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), 0xAB, (n) * sizeof(t)))
27d5b266 730
2304df62 731#ifdef USE_STRUCT_COPY
ff68c719 732#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
733#else
734#define StructCopy(s,d,t) Copy(s,d,1,t)
735#endif
2cc61e15 736
622913ab
IZ
737#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
738
2cc61e15
DD
739#ifdef NEED_VA_COPY
740# ifdef va_copy
741# define Perl_va_copy(s, d) va_copy(d, s)
2cc61e15 742# else
a1866d1b
JH
743# if defined(__va_copy)
744# define Perl_va_copy(s, d) __va_copy(d, s)
745# else
746# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
747# endif
2cc61e15
DD
748# endif
749#endif
750
472d47bc
SB
751/* convenience debug macros */
752#ifdef USE_ITHREADS
753#define pTHX_FORMAT "Perl interpreter: 0x%p"
754#define pTHX__FORMAT ", Perl interpreter: 0x%p"
f54cb97a
AL
755#define pTHX_VALUE_ (void *)my_perl,
756#define pTHX_VALUE (void *)my_perl
757#define pTHX__VALUE_ ,(void *)my_perl,
758#define pTHX__VALUE ,(void *)my_perl
472d47bc
SB
759#else
760#define pTHX_FORMAT
761#define pTHX__FORMAT
762#define pTHX_VALUE_
763#define pTHX_VALUE
764#define pTHX__VALUE_
765#define pTHX__VALUE
766#endif /* USE_ITHREADS */