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