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