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