This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Oops - forgot to update MANIFEST for change 24056.
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
b5f8cc5c 4 * 2000, 2001, 2002, 2004, 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 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 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 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 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
27d4fb96 90/* XXX A note on the perl source internal type system. The
91 original intent was that I32 be *exactly* 32 bits.
92
93 Currently, we only guarantee that I32 is *at least* 32 bits.
94 Specifically, if int is 64 bits, then so is I32. (This is the case
95 for the Cray.) This has the advantage of meshing nicely with
96 standard library calls (where we pass an I32 and the library is
97 expecting an int), but the disadvantage that an I32 is not 32 bits.
98 Andy Dougherty August 1996
24fef2a7 99
dc45a647
MB
100 There is no guarantee that there is *any* integral type with
101 exactly 32 bits. It is perfectly legal for a system to have
102 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 103
dc45a647
MB
104 Similarly, there is no guarantee that I16 and U16 have exactly 16
105 bits.
693762b4 106
8e84507e
NIS
107 For dealing with issues that may arise from various 32/64-bit
108 systems, we will ask Configure to check out
8175356b 109
dc45a647
MB
110 SHORTSIZE == sizeof(short)
111 INTSIZE == sizeof(int)
112 LONGSIZE == sizeof(long)
113 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
114 PTRSIZE == sizeof(void *)
115 DOUBLESIZE == sizeof(double)
116 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 117
27d4fb96 118*/
119
69512466
JH
120#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
121# include <inttypes.h>
dd0eed91
JH
122# ifdef INT32_MIN_BROKEN
123# undef INT32_MIN
124# define INT32_MIN (-2147483647-1)
125# endif
126# ifdef INT64_MIN_BROKEN
127# undef INT64_MIN
128# define INT64_MIN (-9223372036854775807LL-1)
129# endif
69512466
JH
130#endif
131
8175356b
JH
132typedef I8TYPE I8;
133typedef U8TYPE U8;
134typedef I16TYPE I16;
135typedef U16TYPE U16;
136typedef I32TYPE I32;
137typedef U32TYPE U32;
6b8eaf93
JH
138#ifdef PERL_CORE
139# ifdef HAS_QUAD
8175356b
JH
140typedef I64TYPE I64;
141typedef U64TYPE U64;
6b8eaf93
JH
142# endif
143#endif /* PERL_CORE */
8175356b 144
69512466
JH
145#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
146# ifndef UINT64_C /* usually from <inttypes.h> */
147# if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
148# define INT64_C(c) CAT2(c,LL)
149# define UINT64_C(c) CAT2(c,ULL)
150# else
151# if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
152# define INT64_C(c) CAT2(c,L)
153# define UINT64_C(c) CAT2(c,UL)
154# else
155# define INT64_C(c) ((I64TYPE)(c))
156# define UINT64_C(c) ((U64TYPE)(c))
157# endif
158# endif
e8c95190
JH
159# endif
160#endif
dc750f2e 161
b4d4ebb4
MB
162/* H.Merijn Brand [ 01 Nov 2004 ] */
163#if defined(HAS_STRLCAT) || defined(HAS_STRLCPY)
164/* Not (yet) used at top level, but mention them for metaconfig
165 * Read http://www.courtesan.com/todd/papers/strlcpy.html
166 * for the discussion of why replacing strncat/strncpy with
167 * strlcat/strlcpy would be wise */
dc750f2e 168#endif
e8c95190 169
a22e52b9
JH
170/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
171 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
172
d8668976 173#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 174
5ff3f7a4
GS
175/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
176 Please search CHAR_MAX in perl.h for further details. */
177#define U8_MAX UINT8_MAX
178#define U8_MIN UINT8_MIN
179
5ff3f7a4
GS
180#define I16_MAX INT16_MAX
181#define I16_MIN INT16_MIN
182#define U16_MAX UINT16_MAX
183#define U16_MIN UINT16_MIN
184
5ff3f7a4
GS
185#define I32_MAX INT32_MAX
186#define I32_MIN INT32_MIN
0e983133
GS
187#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
188# define U32_MAX UINT32_MAX
189#else
190# define U32_MAX 4294967295U
191#endif
5ff3f7a4
GS
192#define U32_MIN UINT32_MIN
193
194#else
195
5c9fa16e
KA
196/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
197 Please search CHAR_MAX in perl.h for further details. */
27d4fb96 198#define U8_MAX PERL_UCHAR_MAX
199#define U8_MIN PERL_UCHAR_MIN
79072805 200
27d4fb96 201#define I16_MAX PERL_SHORT_MAX
202#define I16_MIN PERL_SHORT_MIN
203#define U16_MAX PERL_USHORT_MAX
204#define U16_MIN PERL_USHORT_MIN
79072805 205
c4f23d77 206#if LONGSIZE > 4
27d4fb96 207# define I32_MAX PERL_INT_MAX
208# define I32_MIN PERL_INT_MIN
209# define U32_MAX PERL_UINT_MAX
210# define U32_MIN PERL_UINT_MIN
79072805 211#else
27d4fb96 212# define I32_MAX PERL_LONG_MAX
213# define I32_MIN PERL_LONG_MIN
214# define U32_MAX PERL_ULONG_MAX
215# define U32_MIN PERL_ULONG_MIN
79072805
LW
216#endif
217
5ff3f7a4
GS
218#endif
219
58a9a5d5 220/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
fc36a67e 221#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
222#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
223#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
224
ff68c719 225#define Ctl(ch) ((ch) & 037)
8d063cd8 226
954c1994 227/*
ccfc67b7
JH
228=head1 Miscellaneous Functions
229
954c1994
GS
230=for apidoc Am|bool|strNE|char* s1|char* s2
231Test two strings to see if they are different. Returns true or
232false.
233
234=for apidoc Am|bool|strEQ|char* s1|char* s2
235Test two strings to see if they are equal. Returns true or false.
236
237=for apidoc Am|bool|strLT|char* s1|char* s2
238Test two strings to see if the first, C<s1>, is less than the second,
239C<s2>. Returns true or false.
240
241=for apidoc Am|bool|strLE|char* s1|char* s2
242Test two strings to see if the first, C<s1>, is less than or equal to the
243second, C<s2>. Returns true or false.
244
245=for apidoc Am|bool|strGT|char* s1|char* s2
246Test two strings to see if the first, C<s1>, is greater than the second,
247C<s2>. Returns true or false.
248
249=for apidoc Am|bool|strGE|char* s1|char* s2
250Test two strings to see if the first, C<s1>, is greater than or equal to
251the second, C<s2>. Returns true or false.
252
253=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
254Test two strings to see if they are different. The C<len> parameter
255indicates the number of bytes to compare. Returns true or false. (A
256wrapper for C<strncmp>).
257
258=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
259Test two strings to see if they are equal. The C<len> parameter indicates
260the number of bytes to compare. Returns true or false. (A wrapper for
261C<strncmp>).
262
263=cut
264*/
265
8d063cd8
LW
266#define strNE(s1,s2) (strcmp(s1,s2))
267#define strEQ(s1,s2) (!strcmp(s1,s2))
268#define strLT(s1,s2) (strcmp(s1,s2) < 0)
269#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
270#define strGT(s1,s2) (strcmp(s1,s2) > 0)
271#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
272#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
273#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 274
36477c24 275#ifdef HAS_MEMCMP
276# define memNE(s1,s2,l) (memcmp(s1,s2,l))
277# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
278#else
279# define memNE(s1,s2,l) (bcmp(s1,s2,l))
280# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
281#endif
282
bbce6d69 283/*
284 * Character classes.
285 *
286 * Unfortunately, the introduction of locales means that we
287 * can't trust isupper(), etc. to tell the truth. And when
288 * it comes to /\w+/ with tainting enabled, we *must* be able
289 * to trust our character classes.
290 *
291 * Therefore, the default tests in the text of Perl will be
292 * independent of locale. Any code that wants to depend on
293 * the current locale will use the tests that begin with "lc".
294 */
295
2304df62
AD
296#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
297# ifndef CTYPE256
298# define CTYPE256
299# endif
300#endif
301
954c1994 302/*
ccfc67b7
JH
303
304=head1 Character classes
305
954c1994 306=for apidoc Am|bool|isALNUM|char ch
4375e838 307Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 308character (including underscore) or digit.
954c1994
GS
309
310=for apidoc Am|bool|isALPHA|char ch
4375e838 311Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994
GS
312character.
313
314=for apidoc Am|bool|isSPACE|char ch
315Returns a boolean indicating whether the C C<char> is whitespace.
316
317=for apidoc Am|bool|isDIGIT|char ch
4375e838 318Returns a boolean indicating whether the C C<char> is an ASCII
954c1994
GS
319digit.
320
321=for apidoc Am|bool|isUPPER|char ch
322Returns a boolean indicating whether the C C<char> is an uppercase
323character.
324
325=for apidoc Am|bool|isLOWER|char ch
326Returns a boolean indicating whether the C C<char> is a lowercase
327character.
328
329=for apidoc Am|char|toUPPER|char ch
330Converts the specified character to uppercase.
331
332=for apidoc Am|char|toLOWER|char ch
333Converts the specified character to lowercase.
334
335=cut
336*/
337
bbce6d69 338#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
339#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
340#define isALPHA(c) (isUPPER(c) || isLOWER(c))
341#define isSPACE(c) \
342 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e
JF
343#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
344#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 345#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7
JH
346#ifdef EBCDIC
347 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
348# define isUPPER(c) isupper(c)
349# define isLOWER(c) islower(c)
b8c5462f
JH
350# define isALNUMC(c) isalnum(c)
351# define isASCII(c) isascii(c)
352# define isCNTRL(c) iscntrl(c)
353# define isGRAPH(c) isgraph(c)
9d116dd7 354# define isPRINT(c) isprint(c)
b8c5462f
JH
355# define isPUNCT(c) ispunct(c)
356# define isXDIGIT(c) isxdigit(c)
9d116dd7
JH
357# define toUPPER(c) toupper(c)
358# define toLOWER(c) tolower(c)
359#else
360# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
361# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f
JH
362# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
363# define isASCII(c) ((c) <= 127)
7be5a6cf 364# define isCNTRL(c) ((c) < ' ' || (c) == 127)
b8c5462f 365# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
f79b3095 366# define isPRINT(c) (((c) > 32 && (c) < 127) || (c) == ' ')
b8c5462f 367# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
c302d089 368# define isXDIGIT(c) (isDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7
JH
369# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
370# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
371#endif
bbce6d69 372
373#ifdef USE_NEXT_CTYPE
374
375# define isALNUM_LC(c) \
37bd1396 376 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719 377# define isIDFIRST_LC(c) \
378 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
379# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
380# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
381# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
382# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
383# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 384# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f
JH
385# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
386# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 387# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 388# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719 389# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
390# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69 391
392#else /* !USE_NEXT_CTYPE */
b8c5462f 393
bbce6d69 394# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
395
b8c5462f 396# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 397# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69 398# define isALPHA_LC(c) isalpha((unsigned char)(c))
399# define isSPACE_LC(c) isspace((unsigned char)(c))
400# define isDIGIT_LC(c) isdigit((unsigned char)(c))
401# define isUPPER_LC(c) isupper((unsigned char)(c))
402# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f
JH
403# define isALNUMC_LC(c) isalnum((unsigned char)(c))
404# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
405# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 406# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 407# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69 408# define toUPPER_LC(c) toupper((unsigned char)(c))
409# define toLOWER_LC(c) tolower((unsigned char)(c))
410
411# else
412
b8c5462f 413# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69 414# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
415# define isALPHA_LC(c) (isascii(c) && isalpha(c))
416# define isSPACE_LC(c) (isascii(c) && isspace(c))
417# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
418# define isUPPER_LC(c) (isascii(c) && isupper(c))
419# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f
JH
420# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
421# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
422# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 423# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 424# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69 425# define toUPPER_LC(c) toupper(c)
426# define toLOWER_LC(c) tolower(c)
427
428# endif
a0d0e21e 429#endif /* USE_NEXT_CTYPE */
55204971 430
aaa51d5e
JF
431#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
432#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
433
a0ed51b3
LW
434#define isALNUM_uni(c) is_uni_alnum(c)
435#define isIDFIRST_uni(c) is_uni_idfirst(c)
436#define isALPHA_uni(c) is_uni_alpha(c)
437#define isSPACE_uni(c) is_uni_space(c)
438#define isDIGIT_uni(c) is_uni_digit(c)
439#define isUPPER_uni(c) is_uni_upper(c)
440#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f
JH
441#define isALNUMC_uni(c) is_uni_alnumc(c)
442#define isASCII_uni(c) is_uni_ascii(c)
443#define isCNTRL_uni(c) is_uni_cntrl(c)
444#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 445#define isPRINT_uni(c) is_uni_print(c)
b8c5462f
JH
446#define isPUNCT_uni(c) is_uni_punct(c)
447#define isXDIGIT_uni(c) is_uni_xdigit(c)
a2a2844f
JH
448#define toUPPER_uni(c,s,l) to_uni_upper(c,s,l)
449#define toTITLE_uni(c,s,l) to_uni_title(c,s,l)
450#define toLOWER_uni(c,s,l) to_uni_lower(c,s,l)
b8d68ded 451#define toFOLD_uni(c,s,l) to_uni_fold(c,s,l)
a0ed51b3 452
aaa51d5e
JF
453#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
454#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
455
9041c2e3
NIS
456#define isALNUM_LC_uvchr(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
457#define isIDFIRST_LC_uvchr(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
458#define isALPHA_LC_uvchr(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
459#define isSPACE_LC_uvchr(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
460#define isDIGIT_LC_uvchr(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
461#define isUPPER_LC_uvchr(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
462#define isLOWER_LC_uvchr(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
463#define isALNUMC_LC_uvchr(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
464#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
465#define isGRAPH_LC_uvchr(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
466#define isPRINT_LC_uvchr(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
467#define isPUNCT_LC_uvchr(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
a0ed51b3 468
aaa51d5e
JF
469#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
470#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
471
a0ed51b3 472#define isALNUM_utf8(p) is_utf8_alnum(p)
82686b01
JH
473/* The ID_Start of Unicode is quite limiting: it assumes a L-class
474 * character (meaning that you cannot have, say, a CJK character).
475 * Instead, let's allow ID_Continue but not digits. */
476#define isIDFIRST_utf8(p) (is_utf8_idcont(p) && !is_utf8_digit(p))
a0ed51b3
LW
477#define isALPHA_utf8(p) is_utf8_alpha(p)
478#define isSPACE_utf8(p) is_utf8_space(p)
479#define isDIGIT_utf8(p) is_utf8_digit(p)
480#define isUPPER_utf8(p) is_utf8_upper(p)
481#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f
JH
482#define isALNUMC_utf8(p) is_utf8_alnumc(p)
483#define isASCII_utf8(p) is_utf8_ascii(p)
484#define isCNTRL_utf8(p) is_utf8_cntrl(p)
485#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 486#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f
JH
487#define isPUNCT_utf8(p) is_utf8_punct(p)
488#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a2a2844f
JH
489#define toUPPER_utf8(p,s,l) to_utf8_upper(p,s,l)
490#define toTITLE_utf8(p,s,l) to_utf8_title(p,s,l)
491#define toLOWER_utf8(p,s,l) to_utf8_lower(p,s,l)
a0ed51b3 492
aaa51d5e
JF
493#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
494#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
495
9041c2e3
NIS
496#define isALNUM_LC_utf8(p) isALNUM_LC_uvchr(utf8_to_uvchr(p, 0))
497#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uvchr(utf8_to_uvchr(p, 0))
498#define isALPHA_LC_utf8(p) isALPHA_LC_uvchr(utf8_to_uvchr(p, 0))
499#define isSPACE_LC_utf8(p) isSPACE_LC_uvchr(utf8_to_uvchr(p, 0))
500#define isDIGIT_LC_utf8(p) isDIGIT_LC_uvchr(utf8_to_uvchr(p, 0))
501#define isUPPER_LC_utf8(p) isUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
502#define isLOWER_LC_utf8(p) isLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
503#define isALNUMC_LC_utf8(p) isALNUMC_LC_uvchr(utf8_to_uvchr(p, 0))
504#define isCNTRL_LC_utf8(p) isCNTRL_LC_uvchr(utf8_to_uvchr(p, 0))
505#define isGRAPH_LC_utf8(p) isGRAPH_LC_uvchr(utf8_to_uvchr(p, 0))
506#define isPRINT_LC_utf8(p) isPRINT_LC_uvchr(utf8_to_uvchr(p, 0))
507#define isPUNCT_LC_utf8(p) isPUNCT_LC_uvchr(utf8_to_uvchr(p, 0))
a0ed51b3 508
aaa51d5e
JF
509#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
510#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
511
9d116dd7 512#ifdef EBCDIC
20b634c2
JH
513# ifdef PERL_IMPLICIT_CONTEXT
514# define toCTRL(c) Perl_ebcdic_control(aTHX_ c)
515# else
516# define toCTRL Perl_ebcdic_control
517# endif
9d116dd7
JH
518#else
519 /* This conversion works both ways, strangely enough. */
520# define toCTRL(c) (toUPPER(c) ^ 64)
521#endif
bbce6d69 522
dea28490
JJ
523/* Line numbers are unsigned, 32 bits. */
524typedef U32 line_t;
378cc40b
LW
525#ifdef lint
526#define NOLINE ((line_t)0)
527#else
dea28490 528#define NOLINE ((line_t) 4294967295UL)
378cc40b
LW
529#endif
530
8c52afec 531
8e84507e 532/*
ccfc67b7
JH
533=head1 SV Manipulation Functions
534
954c1994
GS
535=for apidoc Am|SV*|NEWSV|int id|STRLEN len
536Creates a new SV. A non-zero C<len> parameter indicates the number of
537bytes of preallocated string space the SV should have. An extra byte for a
538tailing NUL is also reserved. (SvPOK is not set for the SV even if string
8e84507e 539space is allocated.) The reference count for the new SV is set to 1.
954c1994
GS
540C<id> is an integer id between 0 and 1299 (used to identify leaks).
541
ccfc67b7
JH
542=head1 Memory Management
543
954c1994
GS
544=for apidoc Am|void|New|int id|void* ptr|int nitems|type
545The XSUB-writer's interface to the C C<malloc> function.
546
547=for apidoc Am|void|Newc|int id|void* ptr|int nitems|type|cast
548The XSUB-writer's interface to the C C<malloc> function, with
549cast.
550
551=for apidoc Am|void|Newz|int id|void* ptr|int nitems|type
552The XSUB-writer's interface to the C C<malloc> function. The allocated
553memory is zeroed with C<memzero>.
554
555=for apidoc Am|void|Renew|void* ptr|int nitems|type
556The XSUB-writer's interface to the C C<realloc> function.
557
558=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
559The XSUB-writer's interface to the C C<realloc> function, with
560cast.
561
49b8b560 562=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
563The XSUB-writer's interface to the C C<free> function.
564
565=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
566The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
567source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
568the type. Can do overlapping moves. See also C<Copy>.
569
e90e2364
NC
570=for apidoc Am|void *|MoveD|void* src|void* dest|int nitems|type
571Like C<Move> but returns dest. Useful for encouraging compilers to tail-call
572optimise.
573
954c1994
GS
574=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
575The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
576source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
577the type. May fail on overlapping copies. See also C<Move>.
578
e90e2364
NC
579=for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
580
581Like C<Copy> but returns dest. Useful for encouraging compilers to tail-call
582optimise.
583
954c1994
GS
584=for apidoc Am|void|Zero|void* dest|int nitems|type
585
586The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
587destination, C<nitems> is the number of items, and C<type> is the type.
588
e90e2364
NC
589=for apidoc Am|void *|ZeroD|void* dest|int nitems|type
590
591Like C<Zero> but returns dest. Useful for encouraging compilers to tail-call
592optimise.
593
954c1994 594=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 595This is an architecture-independent macro to copy one structure to another.
954c1994 596
9965345d
JH
597=for apidoc Am|void|Poison|void* dest|int nitems|type
598
599Fill up memory with a pattern (byte 0xAB over and over again) that
600hopefully catches attempts to access uninitialized memory.
601
602=cut */
954c1994 603
a687059c 604#ifndef lint
ff06c60c
IZ
605
606#define NEWSV(x,len) newSV(len)
607
27d5b266
JH
608#ifdef PERL_MALLOC_WRAP
609#define MEM_WRAP_CHECK(n,t) \
610 (void)((n)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(PL_memory_wrap),0):0)
611#define MEM_WRAP_CHECK_1(n,t,a) \
612 (void)((n)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a),0):0)
613#define MEM_WRAP_CHECK_2(n,t,a,b) \
614 (void)((n)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a,b),0):0)
615
616#define New(x,v,n,t) (v = (MEM_WRAP_CHECK(n,t), (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))))
617#define Newc(x,v,n,t,c) (v = (MEM_WRAP_CHECK(n,t), (c*)safemalloc((MEM_SIZE)((n)*sizeof(t)))))
618#define Newz(x,v,n,t) (v = (MEM_WRAP_CHECK(n,t), (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))), \
619 memzero((char*)(v), (n)*sizeof(t))
620#define Renew(v,n,t) \
621 (v = (MEM_WRAP_CHECK(n,t), (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))))
622#define Renewc(v,n,t,c) \
623 (v = (MEM_WRAP_CHECK(n,t), (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))))
624#define Safefree(d) safefree((Malloc_t)(d))
625
bfed75c6
AL
626#define Move(s,d,n,t) (MEM_WRAP_CHECK(n,t), (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
627#define Copy(s,d,n,t) (MEM_WRAP_CHECK(n,t), (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
27d5b266
JH
628#define Zero(d,n,t) (MEM_WRAP_CHECK(n,t), (void)memzero((char*)(d), (n) * sizeof(t)))
629
bfed75c6
AL
630#define MoveD(s,d,n,t) (MEM_WRAP_CHECK(n,t), memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
631#define CopyD(s,d,n,t) (MEM_WRAP_CHECK(n,t), memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
e90e2364
NC
632#ifdef HAS_MEMSET
633#define ZeroD(d,n,t) (MEM_WRAP_CHECK(n,t), memzero((char*)(d), (n) * sizeof(t)))
634#else
635/* Using bzero(), which returns void. */
636#define ZeroD(d,n,t) (MEM_WRAP_CHECK(n,t), memzero((char*)(d), (n) * sizeof(t)),d)
637#endif
638
27d5b266
JH
639#define Poison(d,n,t) (MEM_WRAP_CHECK(n,t), (void)memset((char*)(d), 0xAB, (n) * sizeof(t)))
640
641#else
642
410319be
NC
643#define MEM_WRAP_CHECK(n,t)
644#define MEM_WRAP_CHECK_1(n,t,a)
645#define MEM_WRAP_CHECK_2(n,t,a,b)
27d5b266 646
ff68c719 647#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
648#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
649#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
650 memzero((char*)(v), (n)*sizeof(t))
651#define Renew(v,n,t) \
652 (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
653#define Renewc(v,n,t,c) \
654 (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
655#define Safefree(d) safefree((Malloc_t)(d))
55497cff 656
bfed75c6
AL
657#define Move(s,d,n,t) (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t))
658#define Copy(s,d,n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t))
ff68c719 659#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
55497cff 660
bfed75c6
AL
661#define MoveD(s,d,n,t) memmove((char*)(d),(const char*)(s), (n) * sizeof(t))
662#define CopyD(s,d,n,t) memcpy((char*)(d),(const char*)(s), (n) * sizeof(t))
e90e2364
NC
663#ifdef HAS_MEMSET
664#define ZeroD(d,n,t) memzero((char*)(d), (n) * sizeof(t))
665#else
666#define ZeroD(d,n,t) ((void)memzero((char*)(d), (n) * sizeof(t)),d)
667#endif
668
9965345d
JH
669#define Poison(d,n,t) (void)memset((char*)(d), 0xAB, (n) * sizeof(t))
670
27d5b266
JH
671#endif
672
a687059c 673#else /* lint */
55497cff 674
ff68c719 675#define New(x,v,n,s) (v = Null(s *))
676#define Newc(x,v,n,s,c) (v = Null(s *))
677#define Newz(x,v,n,s) (v = Null(s *))
678#define Renew(v,n,s) (v = Null(s *))
bee1dbe2 679#define Move(s,d,n,t)
a687059c
LW
680#define Copy(s,d,n,t)
681#define Zero(d,n,t)
e90e2364
NC
682#define MoveD(s,d,n,t) d
683#define CopyD(s,d,n,t) d
684#define ZeroD(d,n,t) d
9965345d 685#define Poison(d,n,t)
ff68c719 686#define Safefree(d) (d) = (d)
55497cff 687
a687059c 688#endif /* lint */
bee1dbe2 689
2304df62 690#ifdef USE_STRUCT_COPY
ff68c719 691#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
692#else
693#define StructCopy(s,d,t) Copy(s,d,1,t)
694#endif
2cc61e15 695
622913ab
IZ
696#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
697
2cc61e15
DD
698#ifdef NEED_VA_COPY
699# ifdef va_copy
700# define Perl_va_copy(s, d) va_copy(d, s)
2cc61e15 701# else
a1866d1b
JH
702# if defined(__va_copy)
703# define Perl_va_copy(s, d) __va_copy(d, s)
704# else
705# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
706# endif
2cc61e15
DD
707# endif
708#endif
709
472d47bc
SB
710/* convenience debug macros */
711#ifdef USE_ITHREADS
712#define pTHX_FORMAT "Perl interpreter: 0x%p"
713#define pTHX__FORMAT ", Perl interpreter: 0x%p"
714#define pTHX_VALUE_ (unsigned long)my_perl,
715#define pTHX_VALUE (unsigned long)my_perl
716#define pTHX__VALUE_ ,(unsigned long)my_perl,
717#define pTHX__VALUE ,(unsigned long)my_perl
718#else
719#define pTHX_FORMAT
720#define pTHX__FORMAT
721#define pTHX_VALUE_
722#define pTHX_VALUE
723#define pTHX__VALUE_
724#define pTHX__VALUE
725#endif /* USE_ITHREADS */