This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
small doc tweaks for _12
[perl5.git] / handy.h
1 /*    handy.h
2  *
3  *    Copyright (c) 1991-1994, 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 #define Nullch Null(char*)
23 #define Nullfp Null(PerlIO*)
24 #define Nullsv Null(SV*)
25
26 #ifdef TRUE
27 #undef TRUE
28 #endif
29 #ifdef FALSE
30 #undef FALSE
31 #endif
32 #define TRUE (1)
33 #define FALSE (0)
34
35
36 /* XXX Configure ought to have a test for a boolean type, if I can
37    just figure out all the headers such a test needs.
38    Andy Dougherty       August 1996
39 */
40 /* bool is built-in for g++-2.6.3, which might be used for an extension.
41    If the extension includes <_G_config.h> before this file then
42    _G_HAVE_BOOL will be properly set.  If, however, the extension includes
43    this file first, then you will have to manually set -DHAS_BOOL in 
44    your command line to avoid a conflict.
45 */
46 #ifdef _G_HAVE_BOOL
47 # if _G_HAVE_BOOL
48 #  ifndef HAS_BOOL
49 #   define HAS_BOOL 1
50 #  endif
51 # endif
52 #endif
53
54 /* The NeXT dynamic loader headers will not build with the bool macro
55    So declare them now to clear confusion.
56 */
57 #ifdef NeXT
58 # undef FALSE
59 # undef TRUE
60   typedef enum bool { FALSE = 0, TRUE = 1 } bool;
61 # define ENUM_BOOL 1
62 # ifndef HAS_BOOL
63 #  define HAS_BOOL 1
64 # endif /* !HAS_BOOL */
65 #endif /* NeXT */
66
67 #ifndef HAS_BOOL
68 # ifdef UTS
69 #  define bool int
70 # else
71 #  define bool char
72 # endif
73 #endif
74
75 /* XXX A note on the perl source internal type system.  The
76    original intent was that I32 be *exactly* 32 bits.
77
78    Currently, we only guarantee that I32 is *at least* 32 bits.
79    Specifically, if int is 64 bits, then so is I32.  (This is the case
80    for the Cray.)  This has the advantage of meshing nicely with
81    standard library calls (where we pass an I32 and the library is
82    expecting an int), but the disadvantage that an I32 is not 32 bits.
83    Andy Dougherty       August 1996
84 */
85
86 typedef char            I8;
87 typedef unsigned char   U8;
88 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
89    Please search CHAR_MAX in perl.h for further details. */
90 #define U8_MAX PERL_UCHAR_MAX
91 #define U8_MIN PERL_UCHAR_MIN
92
93 typedef short           I16;
94 typedef unsigned short  U16;
95 #define I16_MAX PERL_SHORT_MAX
96 #define I16_MIN PERL_SHORT_MIN
97 #define U16_MAX PERL_USHORT_MAX
98 #define U16_MIN PERL_USHORT_MIN
99
100 #if BYTEORDER > 0x4321
101   typedef int           I32;
102   typedef unsigned int  U32;
103 # define I32_MAX PERL_INT_MAX
104 # define I32_MIN PERL_INT_MIN
105 # define U32_MAX PERL_UINT_MAX
106 # define U32_MIN PERL_UINT_MIN
107 #else
108   typedef long          I32;
109   typedef unsigned long U32;
110 # define I32_MAX PERL_LONG_MAX
111 # define I32_MIN PERL_LONG_MIN
112 # define U32_MAX PERL_ULONG_MAX
113 # define U32_MIN PERL_ULONG_MIN
114 #endif
115
116 #define Ctl(ch) ((ch) & 037)
117
118 #define strNE(s1,s2) (strcmp(s1,s2))
119 #define strEQ(s1,s2) (!strcmp(s1,s2))
120 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
121 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
122 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
123 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
124 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
125 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
126
127 #ifdef HAS_MEMCMP
128 #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
129 #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
130 #else
131 #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
132 #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
133 #endif
134
135 /*
136  * Character classes.
137  *
138  * Unfortunately, the introduction of locales means that we
139  * can't trust isupper(), etc. to tell the truth.  And when
140  * it comes to /\w+/ with tainting enabled, we *must* be able
141  * to trust our character classes.
142  *
143  * Therefore, the default tests in the text of Perl will be
144  * independent of locale.  Any code that wants to depend on
145  * the current locale will use the tests that begin with "lc".
146  */
147
148 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
149 #  ifndef CTYPE256
150 #    define CTYPE256
151 #  endif
152 #endif
153
154 #define isALNUM(c)      (isALPHA(c) || isDIGIT(c) || (c) == '_')
155 #define isIDFIRST(c)    (isALPHA(c) || (c) == '_')
156 #define isALPHA(c)      (isUPPER(c) || isLOWER(c))
157 #define isSPACE(c) \
158         ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
159 #define isDIGIT(c)      ((c) >= '0' && (c) <= '9')
160 #define isUPPER(c)      ((c) >= 'A' && (c) <= 'Z')
161 #define isLOWER(c)      ((c) >= 'a' && (c) <= 'z')
162 #define isPRINT(c)      (((c) > 32 && (c) < 127) || isSPACE(c))
163 #define toUPPER(c)      (isLOWER(c) ? (c) - ('a' - 'A') : (c))
164 #define toLOWER(c)      (isUPPER(c) ? (c) + ('a' - 'A') : (c))
165
166 #ifdef USE_NEXT_CTYPE
167
168 #  define isALNUM_LC(c) \
169         (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \
170          (char)(c) == '_')
171 #  define isIDFIRST_LC(c) \
172         (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
173 #  define isALPHA_LC(c)         NXIsAlpha((unsigned int)(c))
174 #  define isSPACE_LC(c)         NXIsSpace((unsigned int)(c))
175 #  define isDIGIT_LC(c)         NXIsDigit((unsigned int)(c))
176 #  define isUPPER_LC(c)         NXIsUpper((unsigned int)(c))
177 #  define isLOWER_LC(c)         NXIsLower((unsigned int)(c))
178 #  define isPRINT_LC(c)         NXIsPrint((unsigned int)(c))
179 #  define toUPPER_LC(c)         NXToUpper((unsigned int)(c))
180 #  define toLOWER_LC(c)         NXToLower((unsigned int)(c))
181
182 #else /* !USE_NEXT_CTYPE */
183 #  if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
184
185 #    define isALNUM_LC(c) \
186         (isalpha((unsigned char)(c)) || \
187          isdigit((unsigned char)(c)) || (char)(c) == '_')
188 #    define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
189 #    define isALPHA_LC(c)       isalpha((unsigned char)(c))
190 #    define isSPACE_LC(c)       isspace((unsigned char)(c))
191 #    define isDIGIT_LC(c)       isdigit((unsigned char)(c))
192 #    define isUPPER_LC(c)       isupper((unsigned char)(c))
193 #    define isLOWER_LC(c)       islower((unsigned char)(c))
194 #    define isPRINT_LC(c)       isprint((unsigned char)(c))
195 #    define toUPPER_LC(c)       toupper((unsigned char)(c))
196 #    define toLOWER_LC(c)       tolower((unsigned char)(c))
197
198 #  else
199
200 #    define isALNUM_LC(c) \
201         (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_'))
202 #    define isIDFIRST_LC(c)     (isascii(c) && (isalpha(c) || (c) == '_'))
203 #    define isALPHA_LC(c)       (isascii(c) && isalpha(c))
204 #    define isSPACE_LC(c)       (isascii(c) && isspace(c))
205 #    define isDIGIT_LC(c)       (isascii(c) && isdigit(c))
206 #    define isUPPER_LC(c)       (isascii(c) && isupper(c))
207 #    define isLOWER_LC(c)       (isascii(c) && islower(c))
208 #    define isPRINT_LC(c)       (isascii(c) && isprint(c))
209 #    define toUPPER_LC(c)       toupper(c)
210 #    define toLOWER_LC(c)       tolower(c)
211
212 #  endif
213 #endif /* USE_NEXT_CTYPE */
214
215 /* This conversion works both ways, strangely enough. */
216 #define toCTRL(c)    (toUPPER(c) ^ 64)
217
218 /* Line numbers are unsigned, 16 bits. */
219 typedef U16 line_t;
220 #ifdef lint
221 #define NOLINE ((line_t)0)
222 #else
223 #define NOLINE ((line_t) 65535)
224 #endif
225
226 /* XXX LEAKTEST doesn't really work in perl5.  There are direct calls to
227    safemalloc() in the source, so LEAKTEST won't pick them up.
228    Further, if you try LEAKTEST, you'll also end up calling
229    Safefree, which might call safexfree() on some things that weren't
230    malloced with safexmalloc.  The correct "fix" to this, if anyone
231    is interested, is to ensure that all calls go through the New and
232    Renew macros.
233         --Andy Dougherty                August 1996
234 */
235
236 #ifndef lint
237 #ifndef LEAKTEST
238
239 #define New(x,v,n,t)    (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
240 #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
241 #define Newz(x,v,n,t)   (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
242                         memzero((char*)(v), (n)*sizeof(t))
243 #define Renew(v,n,t) \
244           (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
245 #define Renewc(v,n,t,c) \
246           (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
247 #define Safefree(d)     safefree((Malloc_t)(d))
248 #define NEWSV(x,len)    newSV(len)
249
250 #else /* LEAKTEST */
251
252 #define New(x,v,n,t)    (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
253 #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
254 #define Newz(x,v,n,t)   (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
255                          memzero((char*)(v), (n)*sizeof(t))
256 #define Renew(v,n,t) \
257           (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
258 #define Renewc(v,n,t,c) \
259           (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
260 #define Safefree(d)     safexfree((Malloc_t)d)
261 #define NEWSV(x,len)    newSV(x,len)
262
263 #define MAXXCOUNT 1200
264 long xcount[MAXXCOUNT];
265 long lastxcount[MAXXCOUNT];
266
267 #endif /* LEAKTEST */
268
269 #define Move(s,d,n,t)   (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
270 #define Copy(s,d,n,t)   (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
271 #define Zero(d,n,t)     (void)memzero((char*)(d), (n) * sizeof(t))
272
273 #else /* lint */
274
275 #define New(x,v,n,s)    (v = Null(s *))
276 #define Newc(x,v,n,s,c) (v = Null(s *))
277 #define Newz(x,v,n,s)   (v = Null(s *))
278 #define Renew(v,n,s)    (v = Null(s *))
279 #define Move(s,d,n,t)
280 #define Copy(s,d,n,t)
281 #define Zero(d,n,t)
282 #define Safefree(d)     (d) = (d)
283
284 #endif /* lint */
285
286 #ifdef USE_STRUCT_COPY
287 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
288 #else
289 #define StructCopy(s,d,t) Copy(s,d,1,t)
290 #endif