/* handy.h
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
* 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2012 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/* IMPORTANT NOTE: Everything whose name begins with an underscore is for
* internal core Perl use only. */
#ifndef PERL_HANDY_H_ /* Guard against nested #inclusion */
#define PERL_HANDY_H_
#ifndef PERL_CORE
# define Null(type) ((type)NULL)
/*
=head1 Handy Values
=for apidoc AmnU||Nullch
Null character pointer. (No longer available when C is
defined.)
=for apidoc AmnU||Nullsv
Null SV pointer. (No longer available when C is defined.)
=cut
*/
# define Nullch Null(char*)
# define Nullfp Null(PerlIO*)
# define Nullsv Null(SV*)
#endif
#ifdef TRUE
#undef TRUE
#endif
#ifdef FALSE
#undef FALSE
#endif
#define TRUE (1)
#define FALSE (0)
/* The MUTABLE_*() macros cast pointers to the types shown, in such a way
* (compiler permitting) that casting away const-ness will give a warning;
* e.g.:
*
* const SV *sv = ...;
* AV *av1 = (AV*)sv; <== BAD: the const has been silently cast away
* AV *av2 = MUTABLE_AV(sv); <== GOOD: it may warn
*/
#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
# define MUTABLE_PTR(p) ({ void *p_ = (p); p_; })
#else
# define MUTABLE_PTR(p) ((void *) (p))
#endif
#define MUTABLE_AV(p) ((AV *)MUTABLE_PTR(p))
#define MUTABLE_CV(p) ((CV *)MUTABLE_PTR(p))
#define MUTABLE_GV(p) ((GV *)MUTABLE_PTR(p))
#define MUTABLE_HV(p) ((HV *)MUTABLE_PTR(p))
#define MUTABLE_IO(p) ((IO *)MUTABLE_PTR(p))
#define MUTABLE_SV(p) ((SV *)MUTABLE_PTR(p))
#if defined(I_STDBOOL) && !defined(PERL_BOOL_AS_CHAR)
# include
# ifndef HAS_BOOL
# define HAS_BOOL 1
# endif
#endif
/* bool is built-in for g++-2.6.3 and later, which might be used
for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
be sure _G_config.h will be included before this file. _G_config.h
also defines _G_HAVE_BOOL for both gcc and g++, but only g++
actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
g++ can be identified by __GNUG__.
Andy Dougherty February 2000
*/
#ifdef __GNUG__ /* GNU g++ has bool built-in */
# ifndef PERL_BOOL_AS_CHAR
# ifndef HAS_BOOL
# define HAS_BOOL 1
# endif
# endif
#endif
#ifndef HAS_BOOL
# ifdef bool
# undef bool
# endif
# define bool char
# define HAS_BOOL 1
#endif
/*
=for apidoc Am|bool|cBOOL|bool expr
Cast-to-bool. A simple S>> cast may not do the right thing:
if C is defined as C, for example, then the cast from C is
implementation-defined.
C<(bool)!!(cbool)> in a ternary triggers a bug in xlc on AIX
=cut
*/
#define cBOOL(cbool) ((cbool) ? (bool)1 : (bool)0)
/* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
* XXX Should really be a Configure probe, with HAS__FUNCTION__
* and FUNCTION__ as results.
* XXX Similarly, a Configure probe for __FILE__ and __LINE__ is needed. */
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
# define FUNCTION__ __func__
#elif (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used, but not modern enough cc (in Tur64, -c99 not known, only -std1). */
# define FUNCTION__ ""
#else
# define FUNCTION__ __FUNCTION__ /* Common extension. */
#endif
/* XXX A note on the perl source internal type system. The
original intent was that I32 be *exactly* 32 bits.
Currently, we only guarantee that I32 is *at least* 32 bits.
Specifically, if int is 64 bits, then so is I32. (This is the case
for the Cray.) This has the advantage of meshing nicely with
standard library calls (where we pass an I32 and the library is
expecting an int), but the disadvantage that an I32 is not 32 bits.
Andy Dougherty August 1996
There is no guarantee that there is *any* integral type with
exactly 32 bits. It is perfectly legal for a system to have
sizeof(short) == sizeof(int) == sizeof(long) == 8.
Similarly, there is no guarantee that I16 and U16 have exactly 16
bits.
For dealing with issues that may arise from various 32/64-bit
systems, we will ask Configure to check out
SHORTSIZE == sizeof(short)
INTSIZE == sizeof(int)
LONGSIZE == sizeof(long)
LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
PTRSIZE == sizeof(void *)
DOUBLESIZE == sizeof(double)
LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
*/
#ifdef I_INTTYPES /* e.g. Linux has int64_t without */
# include
# ifdef INT32_MIN_BROKEN
# undef INT32_MIN
# define INT32_MIN (-2147483647-1)
# endif
# ifdef INT64_MIN_BROKEN
# undef INT64_MIN
# define INT64_MIN (-9223372036854775807LL-1)
# endif
#endif
typedef I8TYPE I8;
typedef U8TYPE U8;
typedef I16TYPE I16;
typedef U16TYPE U16;
typedef I32TYPE I32;
typedef U32TYPE U32;
#ifdef QUADKIND
typedef I64TYPE I64;
typedef U64TYPE U64;
#endif
#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
Please search CHAR_MAX in perl.h for further details. */
#define U8_MAX UINT8_MAX
#define U8_MIN UINT8_MIN
#define I16_MAX INT16_MAX
#define I16_MIN INT16_MIN
#define U16_MAX UINT16_MAX
#define U16_MIN UINT16_MIN
#define I32_MAX INT32_MAX
#define I32_MIN INT32_MIN
#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
# define U32_MAX UINT32_MAX
#else
# define U32_MAX 4294967295U
#endif
#define U32_MIN UINT32_MIN
#else
/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
Please search CHAR_MAX in perl.h for further details. */
#define U8_MAX PERL_UCHAR_MAX
#define U8_MIN PERL_UCHAR_MIN
#define I16_MAX PERL_SHORT_MAX
#define I16_MIN PERL_SHORT_MIN
#define U16_MAX PERL_USHORT_MAX
#define U16_MIN PERL_USHORT_MIN
#if LONGSIZE > 4
# define I32_MAX PERL_INT_MAX
# define I32_MIN PERL_INT_MIN
# define U32_MAX PERL_UINT_MAX
# define U32_MIN PERL_UINT_MIN
#else
# define I32_MAX PERL_LONG_MAX
# define I32_MIN PERL_LONG_MIN
# define U32_MAX PERL_ULONG_MAX
# define U32_MIN PERL_ULONG_MIN
#endif
#endif
/* These C99 typedefs are useful sometimes for, say, loop variables whose
* maximum values are small, but for which speed trumps size. If we have a C99
* compiler, use that. Otherwise, a plain 'int' should be good enough.
*
* Restrict these to core for now until we are more certain this is a good
* idea. */
#if defined(PERL_CORE) || defined(PERL_EXT)
# ifdef I_STDINT
typedef int_fast8_t PERL_INT_FAST8_T;
typedef uint_fast8_t PERL_UINT_FAST8_T;
typedef int_fast16_t PERL_INT_FAST16_T;
typedef uint_fast16_t PERL_UINT_FAST16_T;
# else
typedef int PERL_INT_FAST8_T;
typedef unsigned int PERL_UINT_FAST8_T;
typedef int PERL_INT_FAST16_T;
typedef unsigned int PERL_UINT_FAST16_T;
# endif
#endif
/* log(2) (i.e., log base 10 of 2) is pretty close to 0.30103, just in case
* anyone is grepping for it */
#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log10(2) =~ 146/485 */
#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
/* Unused by core; should be deprecated */
#define Ctl(ch) ((ch) & 037)
#if defined(PERL_CORE) || defined(PERL_EXT)
# ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
# endif
# ifndef MAX
# define MAX(a,b) ((a) > (b) ? (a) : (b))
# endif
#endif
/* Returns a boolean as to whether the input unsigned number is a power of 2
* (2**0, 2**1, etc). In other words if it has just a single bit set.
* If not, subtracting 1 would leave the uppermost bit set, so the & would
* yield non-zero */
#if defined(PERL_CORE) || defined(PERL_EXT)
# define isPOWER_OF_2(n) ((n) && ((n) & ((n)-1)) == 0)
#endif
/*
=for apidoc Am|void|__ASSERT_|bool expr
This is a helper macro to avoid preprocessor issues, replaced by nothing
unless under DEBUGGING, where it expands to an assert of its argument,
followed by a comma (hence the comma operator). If we just used a straight
assert(), we would get a comma with nothing before it when not DEBUGGING.
=cut
We also use empty definition under Coverity since the __ASSERT__
checks often check for things that Really Cannot Happen, and Coverity
detects that and gets all excited. */
#if defined(DEBUGGING) && !defined(__COVERITY__) \
&& ! defined(PERL_SMALL_MACRO_BUFFER)
# define __ASSERT_(statement) assert(statement),
#else
# define __ASSERT_(statement)
#endif
/*
=head1 SV Manipulation Functions
=for apidoc Ama|SV*|newSVpvs|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Ama|SV*|newSVpvs_flags|"literal string"|U32 flags
Like C, but takes a literal string instead of
a string/length pair.
=for apidoc Ama|SV*|newSVpvs_share|"literal string"
Like C, but takes a literal string instead of
a string/length pair and omits the hash parameter.
=for apidoc Am|void|sv_catpvs_flags|SV* sv|"literal string"|I32 flags
Like C, but takes a literal string instead
of a string/length pair.
=for apidoc Am|void|sv_catpvs_nomg|SV* sv|"literal string"
Like C, but takes a literal string instead of
a string/length pair.
=for apidoc Am|void|sv_catpvs|SV* sv|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Am|void|sv_catpvs_mg|SV* sv|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Am|void|sv_setpvs|SV* sv|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Am|void|sv_setpvs_mg|SV* sv|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Am|SV *|sv_setref_pvs|SV *const rv|const char *const classname|"literal string"
Like C, but takes a literal string instead of
a string/length pair.
=head1 Memory Management
=for apidoc Ama|char*|savepvs|"literal string"
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Ama|char*|savesharedpvs|"literal string"
A version of C which allocates the duplicate string in memory
which is shared between threads.
=head1 GV Functions
=for apidoc Am|HV*|gv_stashpvs|"name"|I32 create
Like C, but takes a literal string instead of a
string/length pair.
=head1 Hash Manipulation Functions
=for apidoc Am|SV**|hv_fetchs|HV* tb|"key"|I32 lval
Like C, but takes a literal string instead of a
string/length pair.
=for apidoc Am|SV**|hv_stores|HV* tb|"key"|SV* val
Like C, but takes a literal string instead of a
string/length pair
and omits the hash parameter.
=head1 Lexer interface
=for apidoc Amx|void|lex_stuff_pvs|"pv"|U32 flags
Like L, but takes a literal string instead of
a string/length pair.
=cut
*/
/*
=head1 Handy Values
=for apidoc Amu|pair|STR_WITH_LEN|"literal string"
Returns two comma separated tokens of the input literal string, and its length.
This is convenience macro which helps out in some API calls.
Note that it can't be used as an argument to macros or functions that under
some configurations might be macros, which means that it requires the full
Perl_xxx(aTHX_ ...) form for any API calls where it's used.
=cut
*/
#define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
/* STR_WITH_LEN() shortcuts */
#define newSVpvs(str) Perl_newSVpvn(aTHX_ STR_WITH_LEN(str))
#define newSVpvs_flags(str,flags) \
Perl_newSVpvn_flags(aTHX_ STR_WITH_LEN(str), flags)
#define newSVpvs_share(str) Perl_newSVpvn_share(aTHX_ STR_WITH_LEN(str), 0)
#define sv_catpvs_flags(sv, str, flags) \
Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), flags)
#define sv_catpvs_nomg(sv, str) \
Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), 0)
#define sv_catpvs(sv, str) \
Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC)
#define sv_catpvs_mg(sv, str) \
Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC|SV_SMAGIC)
#define sv_setpvs(sv, str) Perl_sv_setpvn(aTHX_ sv, STR_WITH_LEN(str))
#define sv_setpvs_mg(sv, str) Perl_sv_setpvn_mg(aTHX_ sv, STR_WITH_LEN(str))
#define sv_setref_pvs(rv, classname, str) \
Perl_sv_setref_pvn(aTHX_ rv, classname, STR_WITH_LEN(str))
#define savepvs(str) Perl_savepvn(aTHX_ STR_WITH_LEN(str))
#define savesharedpvs(str) Perl_savesharedpvn(aTHX_ STR_WITH_LEN(str))
#define gv_stashpvs(str, create) \
Perl_gv_stashpvn(aTHX_ STR_WITH_LEN(str), create)
#define gv_fetchpvs(namebeg, add, sv_type) \
Perl_gv_fetchpvn_flags(aTHX_ STR_WITH_LEN(namebeg), add, sv_type)
#define gv_fetchpvn(namebeg, len, add, sv_type) \
Perl_gv_fetchpvn_flags(aTHX_ namebeg, len, add, sv_type)
#define sv_catxmlpvs(dsv, str, utf8) \
Perl_sv_catxmlpvn(aTHX_ dsv, STR_WITH_LEN(str), utf8)
#define lex_stuff_pvs(pv,flags) Perl_lex_stuff_pvn(aTHX_ STR_WITH_LEN(pv), flags)
#define get_cvs(str, flags) \
Perl_get_cvn_flags(aTHX_ STR_WITH_LEN(str), (flags))
/*
=head1 Miscellaneous Functions
=for apidoc Am|bool|strNE|char* s1|char* s2
Test two C-terminated strings to see if they are different. Returns true
or false.
=for apidoc Am|bool|strEQ|char* s1|char* s2
Test two C-terminated strings to see if they are equal. Returns true or
false.
=for apidoc Am|bool|strLT|char* s1|char* s2
Test two C-terminated strings to see if the first, C, is less than the
second, C. Returns true or false.
=for apidoc Am|bool|strLE|char* s1|char* s2
Test two C-terminated strings to see if the first, C, is less than or
equal to the second, C. Returns true or false.
=for apidoc Am|bool|strGT|char* s1|char* s2
Test two C-terminated strings to see if the first, C, is greater than
the second, C. Returns true or false.
=for apidoc Am|bool|strGE|char* s1|char* s2
Test two C-terminated strings to see if the first, C, is greater than
or equal to the second, C. Returns true or false.
=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
Test two C-terminated strings to see if they are different. The C
parameter indicates the number of bytes to compare. Returns true or false. (A
wrapper for C).
=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
Test two C-terminated strings to see if they are equal. The C
parameter indicates the number of bytes to compare. Returns true or false. (A
wrapper for C).
=for apidoc Am|bool|memEQ|char* s1|char* s2|STRLEN len
Test two buffers (which may contain embedded C characters, to see if they
are equal. The C parameter indicates the number of bytes to compare.
Returns zero if equal, or non-zero if non-equal.
=for apidoc Am|bool|memEQs|char* s1|STRLEN l1|"s2"
Like L, but the second string is a literal enclosed in double quotes,
C gives the number of bytes in C.
Returns zero if equal, or non-zero if non-equal.
=for apidoc Am|bool|memNE|char* s1|char* s2|STRLEN len
Test two buffers (which may contain embedded C characters, to see if they
are not equal. The C parameter indicates the number of bytes to compare.
Returns zero if non-equal, or non-zero if equal.
=for apidoc Am|bool|memNEs|char* s1|STRLEN l1|"s2"
Like L, but the second string is a literal enclosed in double quotes,
C gives the number of bytes in C.
Returns zero if non-equal, or zero if non-equal.
=for apidoc Am|bool|memCHRs|"list"|char c
Returns the position of the first occurence of the byte C in the literal
string C<"list">, or NULL if C doesn't appear in C<"list">. All bytes are
treated as unsigned char. Thus this macro can be used to determine if C is
in a set of particular characters. Unlike L, it works even if C
is C (and the set doesn't include C).
=cut
New macros should use the following conventions for their names (which are
based on the underlying C library functions):
(mem | str n? ) (EQ | NE | LT | GT | GE | (( BEGIN | END ) P? )) l? s?
Each has two main parameters, string-like operands that are compared
against each other, as specified by the macro name. Some macros may
additionally have one or potentially even two length parameters. If a length
parameter applies to both string parameters, it will be positioned third;
otherwise any length parameter immediately follows the string parameter it
applies to.
If the prefix to the name is 'str', the string parameter is a pointer to a C
language string. Such a string does not contain embedded NUL bytes; its
length may be unknown, but can be calculated by C, since it is
terminated by a NUL, which isn't included in its length.
The optional 'n' following 'str' means that there is a third parameter,
giving the maximum number of bytes to look at in each string. Even if both
strings are longer than the length parameter, those extra bytes will be
unexamined.
The 's' suffix means that the 2nd byte string parameter is a literal C
double-quoted string. Its length will automatically be calculated by the
macro, so no length parameter will ever be needed for it.
If the prefix is 'mem', the string parameters don't have to be C strings;
they may contain embedded NUL bytes, do not necessarily have a terminating
NUL, and their lengths can be known only through other means, which in
practice are additional parameter(s) passed to the function. All 'mem'
functions have at least one length parameter. Barring any 'l' or 's' suffix,
there is a single length parameter, in position 3, which applies to both
string parameters. The 's' suffix means, as described above, that the 2nd
string is a literal double-quoted C string (hence its length is calculated by
the macro, and the length parameter to the function applies just to the first
string parameter, and hence is positioned just after it). An 'l' suffix
means that the 2nd string parameter has its own length parameter, and the
signature will look like memFOOl(s1, l1, s2, l2).
BEGIN (and END) are for testing if the 2nd string is an initial (or final)
substring of the 1st string. 'P' if present indicates that the substring
must be a "proper" one in tha mathematical sense that the first one must be
strictly larger than the 2nd.
*/
#define strNE(s1,s2) (strcmp(s1,s2) != 0)
#define strEQ(s1,s2) (strcmp(s1,s2) == 0)
#define strLT(s1,s2) (strcmp(s1,s2) < 0)
#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
#define strGT(s1,s2) (strcmp(s1,s2) > 0)
#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
#define strnNE(s1,s2,l) (strncmp(s1,s2,l) != 0)
#define strnEQ(s1,s2,l) (strncmp(s1,s2,l) == 0)
#define memEQ(s1,s2,l) (memcmp(((const void *) (s1)), ((const void *) (s2)), l) == 0)
#define memNE(s1,s2,l) (! memEQ(s1,s2,l))
/* memEQ and memNE where second comparand is a string constant */
#define memEQs(s1, l, s2) \
(((sizeof(s2)-1) == (l)) && memEQ((s1), ("" s2 ""), (sizeof(s2)-1)))
#define memNEs(s1, l, s2) (! memEQs(s1, l, s2))
/* Keep these private until we decide it was a good idea */
#if defined(PERL_CORE) || defined(PERL_EXT) || defined(PERL_EXT_POSIX)
#define strBEGINs(s1,s2) (strncmp(s1,"" s2 "", sizeof(s2)-1) == 0)
#define memBEGINs(s1, l, s2) \
( (Ptrdiff_t) (l) >= (Ptrdiff_t) sizeof(s2) - 1 \
&& memEQ(s1, "" s2 "", sizeof(s2)-1))
#define memBEGINPs(s1, l, s2) \
( (Ptrdiff_t) (l) > (Ptrdiff_t) sizeof(s2) - 1 \
&& memEQ(s1, "" s2 "", sizeof(s2)-1))
#define memENDs(s1, l, s2) \
( (Ptrdiff_t) (l) >= (Ptrdiff_t) sizeof(s2) - 1 \
&& memEQ(s1 + (l) - (sizeof(s2) - 1), "" s2 "", sizeof(s2)-1))
#define memENDPs(s1, l, s2) \
( (Ptrdiff_t) (l) > (Ptrdiff_t) sizeof(s2) \
&& memEQ(s1 + (l) - (sizeof(s2) - 1), "" s2 "", sizeof(s2)-1))
#endif /* End of making macros private */
#define memLT(s1,s2,l) (memcmp(s1,s2,l) < 0)
#define memLE(s1,s2,l) (memcmp(s1,s2,l) <= 0)
#define memGT(s1,s2,l) (memcmp(s1,s2,l) > 0)
#define memGE(s1,s2,l) (memcmp(s1,s2,l) >= 0)
#define memCHRs(s1,c) ((const char *) memchr("" s1 "" , c, sizeof(s1)-1))
/*
* Character classes.
*
* Unfortunately, the introduction of locales means that we
* can't trust isupper(), etc. to tell the truth. And when
* it comes to /\w+/ with tainting enabled, we *must* be able
* to trust our character classes.
*
* Therefore, the default tests in the text of Perl will be
* independent of locale. Any code that wants to depend on
* the current locale will use the tests that begin with "lc".
*/
#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
# ifndef CTYPE256
# define CTYPE256
# endif
#endif
/*
=head1 Character classification
This section is about functions (really macros) that classify characters
into types, such as punctuation versus alphabetic, etc. Most of these are
analogous to regular expression character classes. (See
L.) There are several variants for
each class. (Not all macros have all variants; each item below lists the
ones valid for it.) None are affected by C