This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[patch] preserve @DynaLoader::dl_{librefs,modules}
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
a687059c 4 *
6e21c824
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
8d063cd8
LW
8 */
9
85e6fe83 10#if !defined(__STDC__)
378cc40b
LW
11#ifdef NULL
12#undef NULL
13#endif
a687059c
LW
14#ifndef I286
15# define NULL 0
16#else
17# define NULL 0L
18#endif
85e6fe83
LW
19#endif
20
378cc40b 21#define Null(type) ((type)NULL)
954c1994
GS
22
23/*
24=for apidoc AmU||Nullch
25Null character pointer.
26
27=for apidoc AmU||Nullsv
28Null SV pointer.
29
30=cut
31*/
32
8d063cd8 33#define Nullch Null(char*)
760ac839 34#define Nullfp Null(PerlIO*)
79072805 35#define Nullsv Null(SV*)
8d063cd8 36
641d3f0b
PP
37#ifdef TRUE
38#undef TRUE
39#endif
40#ifdef FALSE
41#undef FALSE
42#endif
43#define TRUE (1)
44#define FALSE (0)
45
27d4fb96
PP
46
47/* XXX Configure ought to have a test for a boolean type, if I can
48 just figure out all the headers such a test needs.
49 Andy Dougherty August 1996
50*/
8e84507e 51/* bool is built-in for g++-2.6.3 and later, which might be used
c1d22f6b
GS
52 for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
53 be sure _G_config.h will be included before this file. _G_config.h
8e84507e 54 also defines _G_HAVE_BOOL for both gcc and g++, but only g++
c1d22f6b
GS
55 actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
56 g++ can be identified by __GNUG__.
57 Andy Dougherty February 2000
5d94fbed 58*/
c1d22f6b 59#ifdef __GNUG__ /* GNU g++ has bool built-in */
5d94fbed 60# ifndef HAS_BOOL
c1d22f6b 61# define HAS_BOOL 1
5d94fbed 62# endif
5d94fbed
AD
63#endif
64
641d3f0b
PP
65/* The NeXT dynamic loader headers will not build with the bool macro
66 So declare them now to clear confusion.
67*/
8f1f23e8 68#if defined(NeXT) || defined(__NeXT__)
641d3f0b
PP
69# undef FALSE
70# undef TRUE
71 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
72# define ENUM_BOOL 1
73# ifndef HAS_BOOL
74# define HAS_BOOL 1
75# endif /* !HAS_BOOL */
8f1f23e8 76#endif /* NeXT || __NeXT__ */
641d3f0b 77
5d94fbed 78#ifndef HAS_BOOL
61bb5906 79# if defined(UTS) || defined(VMS)
5d94fbed
AD
80# define bool int
81# else
82# define bool char
83# endif
c1d22f6b 84# define HAS_BOOL 1
a687059c 85#endif
0d3e774c 86
27d4fb96
PP
87/* XXX A note on the perl source internal type system. The
88 original intent was that I32 be *exactly* 32 bits.
89
90 Currently, we only guarantee that I32 is *at least* 32 bits.
91 Specifically, if int is 64 bits, then so is I32. (This is the case
92 for the Cray.) This has the advantage of meshing nicely with
93 standard library calls (where we pass an I32 and the library is
94 expecting an int), but the disadvantage that an I32 is not 32 bits.
95 Andy Dougherty August 1996
24fef2a7 96
dc45a647
MB
97 There is no guarantee that there is *any* integral type with
98 exactly 32 bits. It is perfectly legal for a system to have
99 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 100
dc45a647
MB
101 Similarly, there is no guarantee that I16 and U16 have exactly 16
102 bits.
693762b4 103
8e84507e
NIS
104 For dealing with issues that may arise from various 32/64-bit
105 systems, we will ask Configure to check out
8175356b 106
dc45a647
MB
107 SHORTSIZE == sizeof(short)
108 INTSIZE == sizeof(int)
109 LONGSIZE == sizeof(long)
110 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
111 PTRSIZE == sizeof(void *)
112 DOUBLESIZE == sizeof(double)
113 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 114
27d4fb96
PP
115*/
116
69512466
JH
117#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
118# include <inttypes.h>
119#endif
120
8175356b
JH
121typedef I8TYPE I8;
122typedef U8TYPE U8;
123typedef I16TYPE I16;
124typedef U16TYPE U16;
125typedef I32TYPE I32;
126typedef U32TYPE U32;
6b8eaf93
JH
127#ifdef PERL_CORE
128# ifdef HAS_QUAD
8175356b
JH
129typedef I64TYPE I64;
130typedef U64TYPE U64;
6b8eaf93
JH
131# endif
132#endif /* PERL_CORE */
8175356b 133
69512466
JH
134#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
135# ifndef UINT64_C /* usually from <inttypes.h> */
136# if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
137# define INT64_C(c) CAT2(c,LL)
138# define UINT64_C(c) CAT2(c,ULL)
139# else
140# if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
141# define INT64_C(c) CAT2(c,L)
142# define UINT64_C(c) CAT2(c,UL)
143# else
144# define INT64_C(c) ((I64TYPE)(c))
145# define UINT64_C(c) ((U64TYPE)(c))
146# endif
147# endif
e8c95190
JH
148# endif
149#endif
150
a22e52b9
JH
151/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
152 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
153
d8668976 154#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 155
5ff3f7a4
GS
156/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
157 Please search CHAR_MAX in perl.h for further details. */
158#define U8_MAX UINT8_MAX
159#define U8_MIN UINT8_MIN
160
5ff3f7a4
GS
161#define I16_MAX INT16_MAX
162#define I16_MIN INT16_MIN
163#define U16_MAX UINT16_MAX
164#define U16_MIN UINT16_MIN
165
5ff3f7a4
GS
166#define I32_MAX INT32_MAX
167#define I32_MIN INT32_MIN
168#define U32_MAX UINT32_MAX
169#define U32_MIN UINT32_MIN
170
171#else
172
5c9fa16e
KA
173/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
174 Please search CHAR_MAX in perl.h for further details. */
27d4fb96
PP
175#define U8_MAX PERL_UCHAR_MAX
176#define U8_MIN PERL_UCHAR_MIN
79072805 177
27d4fb96
PP
178#define I16_MAX PERL_SHORT_MAX
179#define I16_MIN PERL_SHORT_MIN
180#define U16_MAX PERL_USHORT_MAX
181#define U16_MIN PERL_USHORT_MIN
79072805 182
c4f23d77 183#if LONGSIZE > 4
27d4fb96
PP
184# define I32_MAX PERL_INT_MAX
185# define I32_MIN PERL_INT_MIN
186# define U32_MAX PERL_UINT_MAX
187# define U32_MIN PERL_UINT_MIN
79072805 188#else
27d4fb96
PP
189# define I32_MAX PERL_LONG_MAX
190# define I32_MIN PERL_LONG_MIN
191# define U32_MAX PERL_ULONG_MAX
192# define U32_MIN PERL_ULONG_MIN
79072805
LW
193#endif
194
5ff3f7a4
GS
195#endif
196
58a9a5d5 197/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
fc36a67e
PP
198#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
199#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
200#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
201
ff68c719 202#define Ctl(ch) ((ch) & 037)
8d063cd8 203
954c1994
GS
204/*
205=for apidoc Am|bool|strNE|char* s1|char* s2
206Test two strings to see if they are different. Returns true or
207false.
208
209=for apidoc Am|bool|strEQ|char* s1|char* s2
210Test two strings to see if they are equal. Returns true or false.
211
212=for apidoc Am|bool|strLT|char* s1|char* s2
213Test two strings to see if the first, C<s1>, is less than the second,
214C<s2>. Returns true or false.
215
216=for apidoc Am|bool|strLE|char* s1|char* s2
217Test two strings to see if the first, C<s1>, is less than or equal to the
218second, C<s2>. Returns true or false.
219
220=for apidoc Am|bool|strGT|char* s1|char* s2
221Test two strings to see if the first, C<s1>, is greater than the second,
222C<s2>. Returns true or false.
223
224=for apidoc Am|bool|strGE|char* s1|char* s2
225Test two strings to see if the first, C<s1>, is greater than or equal to
226the second, C<s2>. Returns true or false.
227
228=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
229Test two strings to see if they are different. The C<len> parameter
230indicates the number of bytes to compare. Returns true or false. (A
231wrapper for C<strncmp>).
232
233=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
234Test two strings to see if they are equal. The C<len> parameter indicates
235the number of bytes to compare. Returns true or false. (A wrapper for
236C<strncmp>).
237
238=cut
239*/
240
8d063cd8
LW
241#define strNE(s1,s2) (strcmp(s1,s2))
242#define strEQ(s1,s2) (!strcmp(s1,s2))
243#define strLT(s1,s2) (strcmp(s1,s2) < 0)
244#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
245#define strGT(s1,s2) (strcmp(s1,s2) > 0)
246#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
247#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
248#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 249
36477c24
PP
250#ifdef HAS_MEMCMP
251# define memNE(s1,s2,l) (memcmp(s1,s2,l))
252# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
253#else
254# define memNE(s1,s2,l) (bcmp(s1,s2,l))
255# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
256#endif
257
bbce6d69
PP
258/*
259 * Character classes.
260 *
261 * Unfortunately, the introduction of locales means that we
262 * can't trust isupper(), etc. to tell the truth. And when
263 * it comes to /\w+/ with tainting enabled, we *must* be able
264 * to trust our character classes.
265 *
266 * Therefore, the default tests in the text of Perl will be
267 * independent of locale. Any code that wants to depend on
268 * the current locale will use the tests that begin with "lc".
269 */
270
2304df62
AD
271#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
272# ifndef CTYPE256
273# define CTYPE256
274# endif
275#endif
276
954c1994
GS
277/*
278=for apidoc Am|bool|isALNUM|char ch
4375e838 279Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 280character (including underscore) or digit.
954c1994
GS
281
282=for apidoc Am|bool|isALPHA|char ch
4375e838 283Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994
GS
284character.
285
286=for apidoc Am|bool|isSPACE|char ch
287Returns a boolean indicating whether the C C<char> is whitespace.
288
289=for apidoc Am|bool|isDIGIT|char ch
4375e838 290Returns a boolean indicating whether the C C<char> is an ASCII
954c1994
GS
291digit.
292
293=for apidoc Am|bool|isUPPER|char ch
294Returns a boolean indicating whether the C C<char> is an uppercase
295character.
296
297=for apidoc Am|bool|isLOWER|char ch
298Returns a boolean indicating whether the C C<char> is a lowercase
299character.
300
301=for apidoc Am|char|toUPPER|char ch
302Converts the specified character to uppercase.
303
304=for apidoc Am|char|toLOWER|char ch
305Converts the specified character to lowercase.
306
307=cut
308*/
309
bbce6d69
PP
310#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
311#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
312#define isALPHA(c) (isUPPER(c) || isLOWER(c))
313#define isSPACE(c) \
314 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e
JF
315#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
316#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 317#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7
JH
318#ifdef EBCDIC
319 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
320# define isUPPER(c) isupper(c)
321# define isLOWER(c) islower(c)
b8c5462f
JH
322# define isALNUMC(c) isalnum(c)
323# define isASCII(c) isascii(c)
324# define isCNTRL(c) iscntrl(c)
325# define isGRAPH(c) isgraph(c)
9d116dd7 326# define isPRINT(c) isprint(c)
b8c5462f
JH
327# define isPUNCT(c) ispunct(c)
328# define isXDIGIT(c) isxdigit(c)
9d116dd7
JH
329# define toUPPER(c) toupper(c)
330# define toLOWER(c) tolower(c)
331#else
332# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
333# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f
JH
334# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
335# define isASCII(c) ((c) <= 127)
336# define isCNTRL(c) ((c) < ' ')
337# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
9d116dd7 338# define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c))
b8c5462f
JH
339# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
340# define isXDIGIT(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7
JH
341# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
342# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
343#endif
bbce6d69
PP
344
345#ifdef USE_NEXT_CTYPE
346
347# define isALNUM_LC(c) \
37bd1396 348 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719
PP
349# define isIDFIRST_LC(c) \
350 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
351# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
352# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
353# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
354# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
355# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 356# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f
JH
357# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
358# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 359# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 360# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719
PP
361# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
362# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69
PP
363
364#else /* !USE_NEXT_CTYPE */
b8c5462f 365
bbce6d69
PP
366# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
367
b8c5462f 368# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 369# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69
PP
370# define isALPHA_LC(c) isalpha((unsigned char)(c))
371# define isSPACE_LC(c) isspace((unsigned char)(c))
372# define isDIGIT_LC(c) isdigit((unsigned char)(c))
373# define isUPPER_LC(c) isupper((unsigned char)(c))
374# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f
JH
375# define isALNUMC_LC(c) isalnum((unsigned char)(c))
376# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
377# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 378# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 379# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69
PP
380# define toUPPER_LC(c) toupper((unsigned char)(c))
381# define toLOWER_LC(c) tolower((unsigned char)(c))
382
383# else
384
b8c5462f 385# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69
PP
386# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
387# define isALPHA_LC(c) (isascii(c) && isalpha(c))
388# define isSPACE_LC(c) (isascii(c) && isspace(c))
389# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
390# define isUPPER_LC(c) (isascii(c) && isupper(c))
391# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f
JH
392# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
393# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
394# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 395# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 396# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69
PP
397# define toUPPER_LC(c) toupper(c)
398# define toLOWER_LC(c) tolower(c)
399
400# endif
a0d0e21e 401#endif /* USE_NEXT_CTYPE */
55204971 402
aaa51d5e
JF
403#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
404#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
405
a0ed51b3
LW
406#define isALNUM_uni(c) is_uni_alnum(c)
407#define isIDFIRST_uni(c) is_uni_idfirst(c)
408#define isALPHA_uni(c) is_uni_alpha(c)
409#define isSPACE_uni(c) is_uni_space(c)
410#define isDIGIT_uni(c) is_uni_digit(c)
411#define isUPPER_uni(c) is_uni_upper(c)
412#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f
JH
413#define isALNUMC_uni(c) is_uni_alnumc(c)
414#define isASCII_uni(c) is_uni_ascii(c)
415#define isCNTRL_uni(c) is_uni_cntrl(c)
416#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 417#define isPRINT_uni(c) is_uni_print(c)
b8c5462f
JH
418#define isPUNCT_uni(c) is_uni_punct(c)
419#define isXDIGIT_uni(c) is_uni_xdigit(c)
a0ed51b3
LW
420#define toUPPER_uni(c) to_uni_upper(c)
421#define toTITLE_uni(c) to_uni_title(c)
422#define toLOWER_uni(c) to_uni_lower(c)
423
aaa51d5e
JF
424#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
425#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
426
9041c2e3
NIS
427#define isALNUM_LC_uvchr(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
428#define isIDFIRST_LC_uvchr(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
429#define isALPHA_LC_uvchr(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
430#define isSPACE_LC_uvchr(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
431#define isDIGIT_LC_uvchr(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
432#define isUPPER_LC_uvchr(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
433#define isLOWER_LC_uvchr(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
434#define isALNUMC_LC_uvchr(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
435#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
436#define isGRAPH_LC_uvchr(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
437#define isPRINT_LC_uvchr(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
438#define isPUNCT_LC_uvchr(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
439#define toUPPER_LC_uvchr(c) (c < 256 ? toUPPER_LC(c) : to_uni_upper_lc(c))
440#define toTITLE_LC_uvchr(c) (c < 256 ? toUPPER_LC(c) : to_uni_title_lc(c))
441#define toLOWER_LC_uvchr(c) (c < 256 ? toLOWER_LC(c) : to_uni_lower_lc(c))
a0ed51b3 442
aaa51d5e
JF
443#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
444#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
445
a0ed51b3
LW
446#define isALNUM_utf8(p) is_utf8_alnum(p)
447#define isIDFIRST_utf8(p) is_utf8_idfirst(p)
448#define isALPHA_utf8(p) is_utf8_alpha(p)
449#define isSPACE_utf8(p) is_utf8_space(p)
450#define isDIGIT_utf8(p) is_utf8_digit(p)
451#define isUPPER_utf8(p) is_utf8_upper(p)
452#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f
JH
453#define isALNUMC_utf8(p) is_utf8_alnumc(p)
454#define isASCII_utf8(p) is_utf8_ascii(p)
455#define isCNTRL_utf8(p) is_utf8_cntrl(p)
456#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 457#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f
JH
458#define isPUNCT_utf8(p) is_utf8_punct(p)
459#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a0ed51b3
LW
460#define toUPPER_utf8(p) to_utf8_upper(p)
461#define toTITLE_utf8(p) to_utf8_title(p)
462#define toLOWER_utf8(p) to_utf8_lower(p)
463
aaa51d5e
JF
464#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
465#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
466
9041c2e3
NIS
467#define isALNUM_LC_utf8(p) isALNUM_LC_uvchr(utf8_to_uvchr(p, 0))
468#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uvchr(utf8_to_uvchr(p, 0))
469#define isALPHA_LC_utf8(p) isALPHA_LC_uvchr(utf8_to_uvchr(p, 0))
470#define isSPACE_LC_utf8(p) isSPACE_LC_uvchr(utf8_to_uvchr(p, 0))
471#define isDIGIT_LC_utf8(p) isDIGIT_LC_uvchr(utf8_to_uvchr(p, 0))
472#define isUPPER_LC_utf8(p) isUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
473#define isLOWER_LC_utf8(p) isLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
474#define isALNUMC_LC_utf8(p) isALNUMC_LC_uvchr(utf8_to_uvchr(p, 0))
475#define isCNTRL_LC_utf8(p) isCNTRL_LC_uvchr(utf8_to_uvchr(p, 0))
476#define isGRAPH_LC_utf8(p) isGRAPH_LC_uvchr(utf8_to_uvchr(p, 0))
477#define isPRINT_LC_utf8(p) isPRINT_LC_uvchr(utf8_to_uvchr(p, 0))
478#define isPUNCT_LC_utf8(p) isPUNCT_LC_uvchr(utf8_to_uvchr(p, 0))
479#define toUPPER_LC_utf8(p) toUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
480#define toTITLE_LC_utf8(p) toTITLE_LC_uvchr(utf8_to_uvchr(p, 0))
481#define toLOWER_LC_utf8(p) toLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
a0ed51b3 482
aaa51d5e
JF
483#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
484#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
485
9d116dd7 486#ifdef EBCDIC
cbebf344 487# define toCTRL(c) Perl_ebcdic_control(c)
9d116dd7
JH
488#else
489 /* This conversion works both ways, strangely enough. */
490# define toCTRL(c) (toUPPER(c) ^ 64)
491#endif
bbce6d69 492
378cc40b 493/* Line numbers are unsigned, 16 bits. */
79072805 494typedef U16 line_t;
378cc40b
LW
495#ifdef lint
496#define NOLINE ((line_t)0)
497#else
498#define NOLINE ((line_t) 65535)
499#endif
500
8c52afec 501
8e84507e 502/*
8c52afec 503 XXX LEAKTEST doesn't really work in perl5. There are direct calls to
27d4fb96 504 safemalloc() in the source, so LEAKTEST won't pick them up.
c1d22f6b 505 (The main "offenders" are extensions.)
27d4fb96
PP
506 Further, if you try LEAKTEST, you'll also end up calling
507 Safefree, which might call safexfree() on some things that weren't
508 malloced with safexmalloc. The correct "fix" to this, if anyone
509 is interested, is to ensure that all calls go through the New and
510 Renew macros.
511 --Andy Dougherty August 1996
512*/
55497cff 513
954c1994
GS
514/*
515=for apidoc Am|SV*|NEWSV|int id|STRLEN len
516Creates a new SV. A non-zero C<len> parameter indicates the number of
517bytes of preallocated string space the SV should have. An extra byte for a
518tailing NUL is also reserved. (SvPOK is not set for the SV even if string
8e84507e 519space is allocated.) The reference count for the new SV is set to 1.
954c1994
GS
520C<id> is an integer id between 0 and 1299 (used to identify leaks).
521
522=for apidoc Am|void|New|int id|void* ptr|int nitems|type
523The XSUB-writer's interface to the C C<malloc> function.
524
525=for apidoc Am|void|Newc|int id|void* ptr|int nitems|type|cast
526The XSUB-writer's interface to the C C<malloc> function, with
527cast.
528
529=for apidoc Am|void|Newz|int id|void* ptr|int nitems|type
530The XSUB-writer's interface to the C C<malloc> function. The allocated
531memory is zeroed with C<memzero>.
532
533=for apidoc Am|void|Renew|void* ptr|int nitems|type
534The XSUB-writer's interface to the C C<realloc> function.
535
536=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
537The XSUB-writer's interface to the C C<realloc> function, with
538cast.
539
49b8b560 540=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
541The XSUB-writer's interface to the C C<free> function.
542
543=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
544The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
545source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
546the type. Can do overlapping moves. See also C<Copy>.
547
548=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
549The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
550source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
551the type. May fail on overlapping copies. See also C<Move>.
552
553=for apidoc Am|void|Zero|void* dest|int nitems|type
554
555The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
556destination, C<nitems> is the number of items, and C<type> is the type.
557
558=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 559This is an architecture-independent macro to copy one structure to another.
954c1994
GS
560
561=cut
562*/
563
a687059c 564#ifndef lint
ff06c60c
IZ
565
566#define NEWSV(x,len) newSV(len)
567
a687059c 568#ifndef LEAKTEST
598a3d64 569
ff68c719
PP
570#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
571#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
572#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
573 memzero((char*)(v), (n)*sizeof(t))
574#define Renew(v,n,t) \
575 (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
576#define Renewc(v,n,t,c) \
577 (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
578#define Safefree(d) safefree((Malloc_t)(d))
55497cff 579
a687059c 580#else /* LEAKTEST */
55497cff 581
ff68c719
PP
582#define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
583#define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
584#define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
585 memzero((char*)(v), (n)*sizeof(t))
586#define Renew(v,n,t) \
587 (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
588#define Renewc(v,n,t,c) \
589 (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
8c52afec 590#define Safefree(d) safexfree((Malloc_t)(d))
ff68c719 591
fc36a67e 592#define MAXXCOUNT 1400
8c52afec
IZ
593#define MAXY_SIZE 80
594#define MAXYCOUNT 16 /* (MAXY_SIZE/4 + 1) */
595extern long xcount[MAXXCOUNT];
596extern long lastxcount[MAXXCOUNT];
597extern long xycount[MAXXCOUNT][MAXYCOUNT];
598extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
55497cff 599
a687059c 600#endif /* LEAKTEST */
55497cff 601
ff68c719
PP
602#define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
603#define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
604#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
55497cff 605
a687059c 606#else /* lint */
55497cff 607
ff68c719
PP
608#define New(x,v,n,s) (v = Null(s *))
609#define Newc(x,v,n,s,c) (v = Null(s *))
610#define Newz(x,v,n,s) (v = Null(s *))
611#define Renew(v,n,s) (v = Null(s *))
bee1dbe2 612#define Move(s,d,n,t)
a687059c
LW
613#define Copy(s,d,n,t)
614#define Zero(d,n,t)
ff68c719 615#define Safefree(d) (d) = (d)
55497cff 616
a687059c 617#endif /* lint */
bee1dbe2 618
2304df62 619#ifdef USE_STRUCT_COPY
ff68c719 620#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
621#else
622#define StructCopy(s,d,t) Copy(s,d,1,t)
623#endif
2cc61e15
DD
624
625#ifdef NEED_VA_COPY
626# ifdef va_copy
627# define Perl_va_copy(s, d) va_copy(d, s)
628# elif defined(__va_copy)
629# define Perl_va_copy(s, d) __va_copy(d, s)
630# else
631# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
632# endif
633#endif
634