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