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