This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Multiplicity & ithreads fix for sv.c/Encode.xs
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
3818b22b 3 * Copyright (c) 1991-2000, 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*/
c1d22f6b
GS
51/* bool is built-in for g++-2.6.3 and later, which might be used
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
54 also defines _G_HAVE_BOOL for both gcc and g++, but only g++
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
dc45a647
MB
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
8175356b
JH
117typedef I8TYPE I8;
118typedef U8TYPE U8;
119typedef I16TYPE I16;
120typedef U16TYPE U16;
121typedef I32TYPE I32;
122typedef U32TYPE U32;
6b8eaf93
JH
123#ifdef PERL_CORE
124# ifdef HAS_QUAD
125# if QUADKIND == QUAD_IS_INT64_T
126# include <sys/types.h>
127# ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
128# include <inttypes.h>
129# endif
130# endif
8175356b
JH
131typedef I64TYPE I64;
132typedef U64TYPE U64;
6b8eaf93
JH
133# endif
134#endif /* PERL_CORE */
8175356b 135
a22e52b9
JH
136/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
137 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
138
d8668976 139#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 140
5ff3f7a4
GS
141/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
142 Please search CHAR_MAX in perl.h for further details. */
143#define U8_MAX UINT8_MAX
144#define U8_MIN UINT8_MIN
145
5ff3f7a4
GS
146#define I16_MAX INT16_MAX
147#define I16_MIN INT16_MIN
148#define U16_MAX UINT16_MAX
149#define U16_MIN UINT16_MIN
150
5ff3f7a4
GS
151#define I32_MAX INT32_MAX
152#define I32_MIN INT32_MIN
153#define U32_MAX UINT32_MAX
154#define U32_MIN UINT32_MIN
155
156#else
157
5c9fa16e
KA
158/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
159 Please search CHAR_MAX in perl.h for further details. */
27d4fb96
PP
160#define U8_MAX PERL_UCHAR_MAX
161#define U8_MIN PERL_UCHAR_MIN
79072805 162
27d4fb96
PP
163#define I16_MAX PERL_SHORT_MAX
164#define I16_MIN PERL_SHORT_MIN
165#define U16_MAX PERL_USHORT_MAX
166#define U16_MIN PERL_USHORT_MIN
79072805 167
c4f23d77 168#if LONGSIZE > 4
27d4fb96
PP
169# define I32_MAX PERL_INT_MAX
170# define I32_MIN PERL_INT_MIN
171# define U32_MAX PERL_UINT_MAX
172# define U32_MIN PERL_UINT_MIN
79072805 173#else
27d4fb96
PP
174# define I32_MAX PERL_LONG_MAX
175# define I32_MIN PERL_LONG_MIN
176# define U32_MAX PERL_ULONG_MAX
177# define U32_MIN PERL_ULONG_MIN
79072805
LW
178#endif
179
5ff3f7a4
GS
180#endif
181
fc36a67e
PP
182#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
183#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
184#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
185
ff68c719 186#define Ctl(ch) ((ch) & 037)
8d063cd8 187
954c1994
GS
188/*
189=for apidoc Am|bool|strNE|char* s1|char* s2
190Test two strings to see if they are different. Returns true or
191false.
192
193=for apidoc Am|bool|strEQ|char* s1|char* s2
194Test two strings to see if they are equal. Returns true or false.
195
196=for apidoc Am|bool|strLT|char* s1|char* s2
197Test two strings to see if the first, C<s1>, is less than the second,
198C<s2>. Returns true or false.
199
200=for apidoc Am|bool|strLE|char* s1|char* s2
201Test two strings to see if the first, C<s1>, is less than or equal to the
202second, C<s2>. Returns true or false.
203
204=for apidoc Am|bool|strGT|char* s1|char* s2
205Test two strings to see if the first, C<s1>, is greater than the second,
206C<s2>. Returns true or false.
207
208=for apidoc Am|bool|strGE|char* s1|char* s2
209Test two strings to see if the first, C<s1>, is greater than or equal to
210the second, C<s2>. Returns true or false.
211
212=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
213Test two strings to see if they are different. The C<len> parameter
214indicates the number of bytes to compare. Returns true or false. (A
215wrapper for C<strncmp>).
216
217=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
218Test two strings to see if they are equal. The C<len> parameter indicates
219the number of bytes to compare. Returns true or false. (A wrapper for
220C<strncmp>).
221
222=cut
223*/
224
8d063cd8
LW
225#define strNE(s1,s2) (strcmp(s1,s2))
226#define strEQ(s1,s2) (!strcmp(s1,s2))
227#define strLT(s1,s2) (strcmp(s1,s2) < 0)
228#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
229#define strGT(s1,s2) (strcmp(s1,s2) > 0)
230#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
231#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
232#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 233
36477c24
PP
234#ifdef HAS_MEMCMP
235# define memNE(s1,s2,l) (memcmp(s1,s2,l))
236# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
237#else
238# define memNE(s1,s2,l) (bcmp(s1,s2,l))
239# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
240#endif
241
bbce6d69
PP
242/*
243 * Character classes.
244 *
245 * Unfortunately, the introduction of locales means that we
246 * can't trust isupper(), etc. to tell the truth. And when
247 * it comes to /\w+/ with tainting enabled, we *must* be able
248 * to trust our character classes.
249 *
250 * Therefore, the default tests in the text of Perl will be
251 * independent of locale. Any code that wants to depend on
252 * the current locale will use the tests that begin with "lc".
253 */
254
2304df62
AD
255#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
256# ifndef CTYPE256
257# define CTYPE256
258# endif
259#endif
260
954c1994
GS
261/*
262=for apidoc Am|bool|isALNUM|char ch
4375e838 263Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 264character (including underscore) or digit.
954c1994
GS
265
266=for apidoc Am|bool|isALPHA|char ch
4375e838 267Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994
GS
268character.
269
270=for apidoc Am|bool|isSPACE|char ch
271Returns a boolean indicating whether the C C<char> is whitespace.
272
273=for apidoc Am|bool|isDIGIT|char ch
4375e838 274Returns a boolean indicating whether the C C<char> is an ASCII
954c1994
GS
275digit.
276
277=for apidoc Am|bool|isUPPER|char ch
278Returns a boolean indicating whether the C C<char> is an uppercase
279character.
280
281=for apidoc Am|bool|isLOWER|char ch
282Returns a boolean indicating whether the C C<char> is a lowercase
283character.
284
285=for apidoc Am|char|toUPPER|char ch
286Converts the specified character to uppercase.
287
288=for apidoc Am|char|toLOWER|char ch
289Converts the specified character to lowercase.
290
291=cut
292*/
293
bbce6d69
PP
294#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
295#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
296#define isALPHA(c) (isUPPER(c) || isLOWER(c))
297#define isSPACE(c) \
298 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e
JF
299#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
300#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 301#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7
JH
302#ifdef EBCDIC
303 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
304# define isUPPER(c) isupper(c)
305# define isLOWER(c) islower(c)
b8c5462f
JH
306# define isALNUMC(c) isalnum(c)
307# define isASCII(c) isascii(c)
308# define isCNTRL(c) iscntrl(c)
309# define isGRAPH(c) isgraph(c)
9d116dd7 310# define isPRINT(c) isprint(c)
b8c5462f
JH
311# define isPUNCT(c) ispunct(c)
312# define isXDIGIT(c) isxdigit(c)
9d116dd7
JH
313# define toUPPER(c) toupper(c)
314# define toLOWER(c) tolower(c)
315#else
316# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
317# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f
JH
318# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
319# define isASCII(c) ((c) <= 127)
320# define isCNTRL(c) ((c) < ' ')
321# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
9d116dd7 322# define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c))
b8c5462f
JH
323# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
324# define isXDIGIT(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7
JH
325# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
326# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
327#endif
bbce6d69
PP
328
329#ifdef USE_NEXT_CTYPE
330
331# define isALNUM_LC(c) \
37bd1396 332 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719
PP
333# define isIDFIRST_LC(c) \
334 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
335# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
336# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
337# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
338# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
339# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 340# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f
JH
341# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
342# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 343# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 344# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719
PP
345# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
346# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69
PP
347
348#else /* !USE_NEXT_CTYPE */
b8c5462f 349
bbce6d69
PP
350# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
351
b8c5462f 352# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 353# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69
PP
354# define isALPHA_LC(c) isalpha((unsigned char)(c))
355# define isSPACE_LC(c) isspace((unsigned char)(c))
356# define isDIGIT_LC(c) isdigit((unsigned char)(c))
357# define isUPPER_LC(c) isupper((unsigned char)(c))
358# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f
JH
359# define isALNUMC_LC(c) isalnum((unsigned char)(c))
360# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
361# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 362# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 363# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69
PP
364# define toUPPER_LC(c) toupper((unsigned char)(c))
365# define toLOWER_LC(c) tolower((unsigned char)(c))
366
367# else
368
b8c5462f 369# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69
PP
370# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
371# define isALPHA_LC(c) (isascii(c) && isalpha(c))
372# define isSPACE_LC(c) (isascii(c) && isspace(c))
373# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
374# define isUPPER_LC(c) (isascii(c) && isupper(c))
375# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f
JH
376# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
377# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
378# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 379# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 380# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69
PP
381# define toUPPER_LC(c) toupper(c)
382# define toLOWER_LC(c) tolower(c)
383
384# endif
a0d0e21e 385#endif /* USE_NEXT_CTYPE */
55204971 386
aaa51d5e
JF
387#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
388#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
389
a0ed51b3
LW
390#define isALNUM_uni(c) is_uni_alnum(c)
391#define isIDFIRST_uni(c) is_uni_idfirst(c)
392#define isALPHA_uni(c) is_uni_alpha(c)
393#define isSPACE_uni(c) is_uni_space(c)
394#define isDIGIT_uni(c) is_uni_digit(c)
395#define isUPPER_uni(c) is_uni_upper(c)
396#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f
JH
397#define isALNUMC_uni(c) is_uni_alnumc(c)
398#define isASCII_uni(c) is_uni_ascii(c)
399#define isCNTRL_uni(c) is_uni_cntrl(c)
400#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 401#define isPRINT_uni(c) is_uni_print(c)
b8c5462f
JH
402#define isPUNCT_uni(c) is_uni_punct(c)
403#define isXDIGIT_uni(c) is_uni_xdigit(c)
a0ed51b3
LW
404#define toUPPER_uni(c) to_uni_upper(c)
405#define toTITLE_uni(c) to_uni_title(c)
406#define toLOWER_uni(c) to_uni_lower(c)
407
aaa51d5e
JF
408#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
409#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
410
a0ed51b3
LW
411#define isALNUM_LC_uni(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
412#define isIDFIRST_LC_uni(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
413#define isALPHA_LC_uni(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
414#define isSPACE_LC_uni(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
415#define isDIGIT_LC_uni(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
416#define isUPPER_LC_uni(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
417#define isLOWER_LC_uni(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
b8c5462f
JH
418#define isALNUMC_LC_uni(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
419#define isCNTRL_LC_uni(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
420#define isGRAPH_LC_uni(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
a0ed51b3 421#define isPRINT_LC_uni(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
b8c5462f 422#define isPUNCT_LC_uni(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
a0ed51b3
LW
423#define toUPPER_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_upper_lc(c))
424#define toTITLE_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_title_lc(c))
425#define toLOWER_LC_uni(c) (c < 256 ? toLOWER_LC(c) : to_uni_lower_lc(c))
426
aaa51d5e
JF
427#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
428#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
429
a0ed51b3
LW
430#define isALNUM_utf8(p) is_utf8_alnum(p)
431#define isIDFIRST_utf8(p) is_utf8_idfirst(p)
432#define isALPHA_utf8(p) is_utf8_alpha(p)
433#define isSPACE_utf8(p) is_utf8_space(p)
434#define isDIGIT_utf8(p) is_utf8_digit(p)
435#define isUPPER_utf8(p) is_utf8_upper(p)
436#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f
JH
437#define isALNUMC_utf8(p) is_utf8_alnumc(p)
438#define isASCII_utf8(p) is_utf8_ascii(p)
439#define isCNTRL_utf8(p) is_utf8_cntrl(p)
440#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 441#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f
JH
442#define isPUNCT_utf8(p) is_utf8_punct(p)
443#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a0ed51b3
LW
444#define toUPPER_utf8(p) to_utf8_upper(p)
445#define toTITLE_utf8(p) to_utf8_title(p)
446#define toLOWER_utf8(p) to_utf8_lower(p)
447
aaa51d5e
JF
448#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
449#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
450
67e989fb
JH
451#define isALNUM_LC_utf8(p) isALNUM_LC_uni(utf8_to_uv(p, 0, 0))
452#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uni(utf8_to_uv(p, 0, 0))
453#define isALPHA_LC_utf8(p) isALPHA_LC_uni(utf8_to_uv(p, 0, 0))
454#define isSPACE_LC_utf8(p) isSPACE_LC_uni(utf8_to_uv(p, 0, 0))
455#define isDIGIT_LC_utf8(p) isDIGIT_LC_uni(utf8_to_uv(p, 0, 0))
456#define isUPPER_LC_utf8(p) isUPPER_LC_uni(utf8_to_uv(p, 0, 0))
457#define isLOWER_LC_utf8(p) isLOWER_LC_uni(utf8_to_uv(p, 0, 0))
458#define isALNUMC_LC_utf8(p) isALNUMC_LC_uni(utf8_to_uv(p, 0, 0))
459#define isCNTRL_LC_utf8(p) isCNTRL_LC_uni(utf8_to_uv(p, 0, 0))
460#define isGRAPH_LC_utf8(p) isGRAPH_LC_uni(utf8_to_uv(p, 0, 0))
461#define isPRINT_LC_utf8(p) isPRINT_LC_uni(utf8_to_uv(p, 0, 0))
462#define isPUNCT_LC_utf8(p) isPUNCT_LC_uni(utf8_to_uv(p, 0, 0))
463#define toUPPER_LC_utf8(p) toUPPER_LC_uni(utf8_to_uv(p, 0, 0))
464#define toTITLE_LC_utf8(p) toTITLE_LC_uni(utf8_to_uv(p, 0, 0))
465#define toLOWER_LC_utf8(p) toLOWER_LC_uni(utf8_to_uv(p, 0, 0))
a0ed51b3 466
aaa51d5e
JF
467#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
468#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
469
9d116dd7 470#ifdef EBCDIC
20ce7b12 471EXT int ebcdic_control (int);
9d116dd7
JH
472# define toCTRL(c) ebcdic_control(c)
473#else
474 /* This conversion works both ways, strangely enough. */
475# define toCTRL(c) (toUPPER(c) ^ 64)
476#endif
bbce6d69 477
378cc40b 478/* Line numbers are unsigned, 16 bits. */
79072805 479typedef U16 line_t;
378cc40b
LW
480#ifdef lint
481#define NOLINE ((line_t)0)
482#else
483#define NOLINE ((line_t) 65535)
484#endif
485
8c52afec 486
c1d22f6b 487/*
8c52afec 488 XXX LEAKTEST doesn't really work in perl5. There are direct calls to
27d4fb96 489 safemalloc() in the source, so LEAKTEST won't pick them up.
c1d22f6b 490 (The main "offenders" are extensions.)
27d4fb96
PP
491 Further, if you try LEAKTEST, you'll also end up calling
492 Safefree, which might call safexfree() on some things that weren't
493 malloced with safexmalloc. The correct "fix" to this, if anyone
494 is interested, is to ensure that all calls go through the New and
495 Renew macros.
496 --Andy Dougherty August 1996
497*/
55497cff 498
954c1994
GS
499/*
500=for apidoc Am|SV*|NEWSV|int id|STRLEN len
501Creates a new SV. A non-zero C<len> parameter indicates the number of
502bytes of preallocated string space the SV should have. An extra byte for a
503tailing NUL is also reserved. (SvPOK is not set for the SV even if string
504space is allocated.) The reference count for the new SV is set to 1.
505C<id> is an integer id between 0 and 1299 (used to identify leaks).
506
507=for apidoc Am|void|New|int id|void* ptr|int nitems|type
508The XSUB-writer's interface to the C C<malloc> function.
509
510=for apidoc Am|void|Newc|int id|void* ptr|int nitems|type|cast
511The XSUB-writer's interface to the C C<malloc> function, with
512cast.
513
514=for apidoc Am|void|Newz|int id|void* ptr|int nitems|type
515The XSUB-writer's interface to the C C<malloc> function. The allocated
516memory is zeroed with C<memzero>.
517
518=for apidoc Am|void|Renew|void* ptr|int nitems|type
519The XSUB-writer's interface to the C C<realloc> function.
520
521=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
522The XSUB-writer's interface to the C C<realloc> function, with
523cast.
524
49b8b560 525=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
526The XSUB-writer's interface to the C C<free> function.
527
528=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
529The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
530source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
531the type. Can do overlapping moves. See also C<Copy>.
532
533=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
534The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
535source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
536the type. May fail on overlapping copies. See also C<Move>.
537
538=for apidoc Am|void|Zero|void* dest|int nitems|type
539
540The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
541destination, C<nitems> is the number of items, and C<type> is the type.
542
543=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 544This is an architecture-independent macro to copy one structure to another.
954c1994
GS
545
546=cut
547*/
548
a687059c 549#ifndef lint
ff06c60c
IZ
550
551#define NEWSV(x,len) newSV(len)
552
a687059c 553#ifndef LEAKTEST
598a3d64 554
ff68c719
PP
555#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
556#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
557#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
558 memzero((char*)(v), (n)*sizeof(t))
559#define Renew(v,n,t) \
560 (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
561#define Renewc(v,n,t,c) \
562 (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
563#define Safefree(d) safefree((Malloc_t)(d))
55497cff 564
a687059c 565#else /* LEAKTEST */
55497cff 566
ff68c719
PP
567#define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
568#define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
569#define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
570 memzero((char*)(v), (n)*sizeof(t))
571#define Renew(v,n,t) \
572 (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
573#define Renewc(v,n,t,c) \
574 (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
8c52afec 575#define Safefree(d) safexfree((Malloc_t)(d))
ff68c719 576
fc36a67e 577#define MAXXCOUNT 1400
8c52afec
IZ
578#define MAXY_SIZE 80
579#define MAXYCOUNT 16 /* (MAXY_SIZE/4 + 1) */
580extern long xcount[MAXXCOUNT];
581extern long lastxcount[MAXXCOUNT];
582extern long xycount[MAXXCOUNT][MAXYCOUNT];
583extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
55497cff 584
a687059c 585#endif /* LEAKTEST */
55497cff 586
ff68c719
PP
587#define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
588#define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
589#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
55497cff 590
a687059c 591#else /* lint */
55497cff 592
ff68c719
PP
593#define New(x,v,n,s) (v = Null(s *))
594#define Newc(x,v,n,s,c) (v = Null(s *))
595#define Newz(x,v,n,s) (v = Null(s *))
596#define Renew(v,n,s) (v = Null(s *))
bee1dbe2 597#define Move(s,d,n,t)
a687059c
LW
598#define Copy(s,d,n,t)
599#define Zero(d,n,t)
ff68c719 600#define Safefree(d) (d) = (d)
55497cff 601
a687059c 602#endif /* lint */
bee1dbe2 603
2304df62 604#ifdef USE_STRUCT_COPY
ff68c719 605#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
606#else
607#define StructCopy(s,d,t) Copy(s,d,1,t)
608#endif