| 1 | /* handy.h
|
|---|
| 2 | *
|
|---|
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
|
|---|
| 4 | * 2000, 2001, 2002, 2004, 2005, 2006, by Larry Wall and others
|
|---|
| 5 | *
|
|---|
| 6 | * You may distribute under the terms of either the GNU General Public
|
|---|
| 7 | * License or the Artistic License, as specified in the README file.
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #if !defined(__STDC__)
|
|---|
| 12 | #ifdef NULL
|
|---|
| 13 | #undef NULL
|
|---|
| 14 | #endif
|
|---|
| 15 | #ifndef I286
|
|---|
| 16 | # define NULL 0
|
|---|
| 17 | #else
|
|---|
| 18 | # define NULL 0L
|
|---|
| 19 | #endif
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 | #define Null(type) ((type)NULL)
|
|---|
| 23 |
|
|---|
| 24 | /*
|
|---|
| 25 | =head1 Handy Values
|
|---|
| 26 |
|
|---|
| 27 | =for apidoc AmU||Nullch
|
|---|
| 28 | Null character pointer.
|
|---|
| 29 |
|
|---|
| 30 | =for apidoc AmU||Nullsv
|
|---|
| 31 | Null SV pointer.
|
|---|
| 32 |
|
|---|
| 33 | =cut
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #define Nullch Null(char*)
|
|---|
| 37 | #define Nullfp Null(PerlIO*)
|
|---|
| 38 | #define Nullsv Null(SV*)
|
|---|
| 39 |
|
|---|
| 40 | #ifdef TRUE
|
|---|
| 41 | #undef TRUE
|
|---|
| 42 | #endif
|
|---|
| 43 | #ifdef FALSE
|
|---|
| 44 | #undef FALSE
|
|---|
| 45 | #endif
|
|---|
| 46 | #define TRUE (1)
|
|---|
| 47 | #define FALSE (0)
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | /* XXX Configure ought to have a test for a boolean type, if I can
|
|---|
| 51 | just figure out all the headers such a test needs.
|
|---|
| 52 | Andy Dougherty August 1996
|
|---|
| 53 | */
|
|---|
| 54 | /* bool is built-in for g++-2.6.3 and later, which might be used
|
|---|
| 55 | for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
|
|---|
| 56 | be sure _G_config.h will be included before this file. _G_config.h
|
|---|
| 57 | also defines _G_HAVE_BOOL for both gcc and g++, but only g++
|
|---|
| 58 | actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
|
|---|
| 59 | g++ can be identified by __GNUG__.
|
|---|
| 60 | Andy Dougherty February 2000
|
|---|
| 61 | */
|
|---|
| 62 | #ifdef __GNUG__ /* GNU g++ has bool built-in */
|
|---|
| 63 | # ifndef HAS_BOOL
|
|---|
| 64 | # define HAS_BOOL 1
|
|---|
| 65 | # endif
|
|---|
| 66 | #endif
|
|---|
| 67 |
|
|---|
| 68 | /* The NeXT dynamic loader headers will not build with the bool macro
|
|---|
| 69 | So declare them now to clear confusion.
|
|---|
| 70 | */
|
|---|
| 71 | #if defined(NeXT) || defined(__NeXT__)
|
|---|
| 72 | # undef FALSE
|
|---|
| 73 | # undef TRUE
|
|---|
| 74 | typedef enum bool { FALSE = 0, TRUE = 1 } bool;
|
|---|
| 75 | # define ENUM_BOOL 1
|
|---|
| 76 | # ifndef HAS_BOOL
|
|---|
| 77 | # define HAS_BOOL 1
|
|---|
| 78 | # endif /* !HAS_BOOL */
|
|---|
| 79 | #endif /* NeXT || __NeXT__ */
|
|---|
| 80 |
|
|---|
| 81 | #ifndef HAS_BOOL
|
|---|
| 82 | # if defined(UTS) || defined(VMS)
|
|---|
| 83 | # define bool int
|
|---|
| 84 | # else
|
|---|
| 85 | # define bool char
|
|---|
| 86 | # endif
|
|---|
| 87 | # define HAS_BOOL 1
|
|---|
| 88 | #endif
|
|---|
| 89 |
|
|---|
| 90 | /* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
|
|---|
| 91 | * XXX Should really be a Configure probe, with HAS__FUNCTION__
|
|---|
| 92 | * and FUNCTION__ as results.
|
|---|
| 93 | * XXX Similarly, a Configure probe for __FILE__ and __LINE__ is needed. */
|
|---|
| 94 | #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
|
|---|
| 95 | # define FUNCTION__ __func__
|
|---|
| 96 | #else
|
|---|
| 97 | # if (defined(_MSC_VER) && _MSC_VER < 1300) || /* Pre-MSVC 7.0 has neither __func__ nor __FUNCTION and no good workarounds, either. */ \
|
|---|
| 98 | (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used, but not modern enough cc (in Tur64, -c99 not known, only -std1). */
|
|---|
| 99 | # define FUNCTION__ ""
|
|---|
| 100 | # else
|
|---|
| 101 | # define FUNCTION__ __FUNCTION__ /* Common extension. */
|
|---|
| 102 | # endif
|
|---|
| 103 | #endif
|
|---|
| 104 |
|
|---|
| 105 | /* XXX A note on the perl source internal type system. The
|
|---|
| 106 | original intent was that I32 be *exactly* 32 bits.
|
|---|
| 107 |
|
|---|
| 108 | Currently, we only guarantee that I32 is *at least* 32 bits.
|
|---|
| 109 | Specifically, if int is 64 bits, then so is I32. (This is the case
|
|---|
| 110 | for the Cray.) This has the advantage of meshing nicely with
|
|---|
| 111 | standard library calls (where we pass an I32 and the library is
|
|---|
| 112 | expecting an int), but the disadvantage that an I32 is not 32 bits.
|
|---|
| 113 | Andy Dougherty August 1996
|
|---|
| 114 |
|
|---|
| 115 | There is no guarantee that there is *any* integral type with
|
|---|
| 116 | exactly 32 bits. It is perfectly legal for a system to have
|
|---|
| 117 | sizeof(short) == sizeof(int) == sizeof(long) == 8.
|
|---|
| 118 |
|
|---|
| 119 | Similarly, there is no guarantee that I16 and U16 have exactly 16
|
|---|
| 120 | bits.
|
|---|
| 121 |
|
|---|
| 122 | For dealing with issues that may arise from various 32/64-bit
|
|---|
| 123 | systems, we will ask Configure to check out
|
|---|
| 124 |
|
|---|
| 125 | SHORTSIZE == sizeof(short)
|
|---|
| 126 | INTSIZE == sizeof(int)
|
|---|
| 127 | LONGSIZE == sizeof(long)
|
|---|
| 128 | LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
|
|---|
| 129 | PTRSIZE == sizeof(void *)
|
|---|
| 130 | DOUBLESIZE == sizeof(double)
|
|---|
| 131 | LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
|
|---|
| 132 |
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | #ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
|
|---|
| 136 | # include <inttypes.h>
|
|---|
| 137 | # ifdef INT32_MIN_BROKEN
|
|---|
| 138 | # undef INT32_MIN
|
|---|
| 139 | # define INT32_MIN (-2147483647-1)
|
|---|
| 140 | # endif
|
|---|
| 141 | # ifdef INT64_MIN_BROKEN
|
|---|
| 142 | # undef INT64_MIN
|
|---|
| 143 | # define INT64_MIN (-9223372036854775807LL-1)
|
|---|
| 144 | # endif
|
|---|
| 145 | #endif
|
|---|
| 146 |
|
|---|
| 147 | typedef I8TYPE I8;
|
|---|
| 148 | typedef U8TYPE U8;
|
|---|
| 149 | typedef I16TYPE I16;
|
|---|
| 150 | typedef U16TYPE U16;
|
|---|
| 151 | typedef I32TYPE I32;
|
|---|
| 152 | typedef U32TYPE U32;
|
|---|
| 153 | #ifdef PERL_CORE
|
|---|
| 154 | # ifdef HAS_QUAD
|
|---|
| 155 | typedef I64TYPE I64;
|
|---|
| 156 | typedef U64TYPE U64;
|
|---|
| 157 | # endif
|
|---|
| 158 | #endif /* PERL_CORE */
|
|---|
| 159 |
|
|---|
| 160 | #if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
|
|---|
| 161 | # ifndef UINT64_C /* usually from <inttypes.h> */
|
|---|
| 162 | # if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
|
|---|
| 163 | # define INT64_C(c) CAT2(c,LL)
|
|---|
| 164 | # define UINT64_C(c) CAT2(c,ULL)
|
|---|
| 165 | # else
|
|---|
| 166 | # if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
|
|---|
| 167 | # define INT64_C(c) CAT2(c,L)
|
|---|
| 168 | # define UINT64_C(c) CAT2(c,UL)
|
|---|
| 169 | # else
|
|---|
| 170 | # define INT64_C(c) ((I64TYPE)(c))
|
|---|
| 171 | # define UINT64_C(c) ((U64TYPE)(c))
|
|---|
| 172 | # endif
|
|---|
| 173 | # endif
|
|---|
| 174 | # endif
|
|---|
| 175 | #endif
|
|---|
| 176 |
|
|---|
| 177 | /* HMB H.Merijn Brand - a placeholder for preparing Configure patches */
|
|---|
| 178 | #if defined(HAS_MALLOC_SIZE) && defined(HAS_MALLOC_GOOD_SIZE)
|
|---|
| 179 | /* Not (yet) used at top level, but mention them for metaconfig */
|
|---|
| 180 | #endif
|
|---|
| 181 |
|
|---|
| 182 | /* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
|
|---|
| 183 | I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
|
|---|
| 184 |
|
|---|
| 185 | #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
|
|---|
| 186 |
|
|---|
| 187 | /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
|
|---|
| 188 | Please search CHAR_MAX in perl.h for further details. */
|
|---|
| 189 | #define U8_MAX UINT8_MAX
|
|---|
| 190 | #define U8_MIN UINT8_MIN
|
|---|
| 191 |
|
|---|
| 192 | #define I16_MAX INT16_MAX
|
|---|
| 193 | #define I16_MIN INT16_MIN
|
|---|
| 194 | #define U16_MAX UINT16_MAX
|
|---|
| 195 | #define U16_MIN UINT16_MIN
|
|---|
| 196 |
|
|---|
| 197 | #define I32_MAX INT32_MAX
|
|---|
| 198 | #define I32_MIN INT32_MIN
|
|---|
| 199 | #ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
|
|---|
| 200 | # define U32_MAX UINT32_MAX
|
|---|
| 201 | #else
|
|---|
| 202 | # define U32_MAX 4294967295U
|
|---|
| 203 | #endif
|
|---|
| 204 | #define U32_MIN UINT32_MIN
|
|---|
| 205 |
|
|---|
| 206 | #else
|
|---|
| 207 |
|
|---|
| 208 | /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
|
|---|
| 209 | Please search CHAR_MAX in perl.h for further details. */
|
|---|
| 210 | #define U8_MAX PERL_UCHAR_MAX
|
|---|
| 211 | #define U8_MIN PERL_UCHAR_MIN
|
|---|
| 212 |
|
|---|
| 213 | #define I16_MAX PERL_SHORT_MAX
|
|---|
| 214 | #define I16_MIN PERL_SHORT_MIN
|
|---|
| 215 | #define U16_MAX PERL_USHORT_MAX
|
|---|
| 216 | #define U16_MIN PERL_USHORT_MIN
|
|---|
| 217 |
|
|---|
| 218 | #if LONGSIZE > 4
|
|---|
| 219 | # define I32_MAX PERL_INT_MAX
|
|---|
| 220 | # define I32_MIN PERL_INT_MIN
|
|---|
| 221 | # define U32_MAX PERL_UINT_MAX
|
|---|
| 222 | # define U32_MIN PERL_UINT_MIN
|
|---|
| 223 | #else
|
|---|
| 224 | # define I32_MAX PERL_LONG_MAX
|
|---|
| 225 | # define I32_MIN PERL_LONG_MIN
|
|---|
| 226 | # define U32_MAX PERL_ULONG_MAX
|
|---|
| 227 | # define U32_MIN PERL_ULONG_MIN
|
|---|
| 228 | #endif
|
|---|
| 229 |
|
|---|
| 230 | #endif
|
|---|
| 231 |
|
|---|
| 232 | /* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
|
|---|
| 233 | #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
|
|---|
| 234 | #define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
|
|---|
| 235 | #define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
|
|---|
| 236 |
|
|---|
| 237 | #define Ctl(ch) ((ch) & 037)
|
|---|
| 238 |
|
|---|
| 239 | /*
|
|---|
| 240 | =head1 Miscellaneous Functions
|
|---|
| 241 |
|
|---|
| 242 | =for apidoc Am|bool|strNE|char* s1|char* s2
|
|---|
| 243 | Test two strings to see if they are different. Returns true or
|
|---|
| 244 | false.
|
|---|
| 245 |
|
|---|
| 246 | =for apidoc Am|bool|strEQ|char* s1|char* s2
|
|---|
| 247 | Test two strings to see if they are equal. Returns true or false.
|
|---|
| 248 |
|
|---|
| 249 | =for apidoc Am|bool|strLT|char* s1|char* s2
|
|---|
| 250 | Test two strings to see if the first, C<s1>, is less than the second,
|
|---|
| 251 | C<s2>. Returns true or false.
|
|---|
| 252 |
|
|---|
| 253 | =for apidoc Am|bool|strLE|char* s1|char* s2
|
|---|
| 254 | Test two strings to see if the first, C<s1>, is less than or equal to the
|
|---|
| 255 | second, C<s2>. Returns true or false.
|
|---|
| 256 |
|
|---|
| 257 | =for apidoc Am|bool|strGT|char* s1|char* s2
|
|---|
| 258 | Test two strings to see if the first, C<s1>, is greater than the second,
|
|---|
| 259 | C<s2>. Returns true or false.
|
|---|
| 260 |
|
|---|
| 261 | =for apidoc Am|bool|strGE|char* s1|char* s2
|
|---|
| 262 | Test two strings to see if the first, C<s1>, is greater than or equal to
|
|---|
| 263 | the second, C<s2>. Returns true or false.
|
|---|
| 264 |
|
|---|
| 265 | =for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
|
|---|
| 266 | Test two strings to see if they are different. The C<len> parameter
|
|---|
| 267 | indicates the number of bytes to compare. Returns true or false. (A
|
|---|
| 268 | wrapper for C<strncmp>).
|
|---|
| 269 |
|
|---|
| 270 | =for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
|
|---|
| 271 | Test two strings to see if they are equal. The C<len> parameter indicates
|
|---|
| 272 | the number of bytes to compare. Returns true or false. (A wrapper for
|
|---|
| 273 | C<strncmp>).
|
|---|
| 274 |
|
|---|
| 275 | =cut
|
|---|
| 276 | */
|
|---|
| 277 |
|
|---|
| 278 | #define strNE(s1,s2) (strcmp(s1,s2))
|
|---|
| 279 | #define strEQ(s1,s2) (!strcmp(s1,s2))
|
|---|
| 280 | #define strLT(s1,s2) (strcmp(s1,s2) < 0)
|
|---|
| 281 | #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
|
|---|
| 282 | #define strGT(s1,s2) (strcmp(s1,s2) > 0)
|
|---|
| 283 | #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
|
|---|
| 284 | #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
|
|---|
| 285 | #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
|
|---|
| 286 |
|
|---|
| 287 | #ifdef HAS_MEMCMP
|
|---|
| 288 | # define memNE(s1,s2,l) (memcmp(s1,s2,l))
|
|---|
| 289 | # define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
|
|---|
| 290 | #else
|
|---|
| 291 | # define memNE(s1,s2,l) (bcmp(s1,s2,l))
|
|---|
| 292 | # define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
|
|---|
| 293 | #endif
|
|---|
| 294 |
|
|---|
| 295 | /*
|
|---|
| 296 | * Character classes.
|
|---|
| 297 | *
|
|---|
| 298 | * Unfortunately, the introduction of locales means that we
|
|---|
| 299 | * can't trust isupper(), etc. to tell the truth. And when
|
|---|
| 300 | * it comes to /\w+/ with tainting enabled, we *must* be able
|
|---|
| 301 | * to trust our character classes.
|
|---|
| 302 | *
|
|---|
|
|---|