This function is equivalent to the libc strtod() function, and is available even on platforms that lack plain strtod(). Its return value is the best available precision depending on platform capabilities and Configure options.
It properly handles the locale radix character, meaning it expects a dot except when called from within the scope of use locale
, in which case the radix character should be that specified by the current locale.
The synonym Strtod() may be used instead.
converts a string representing a binary number to numeric form.
On entry start
and *len_p
give the string to scan, *flags
gives conversion flags, and result
should be NULL
or a pointer to an NV. The scan stops at the end of the string, or at just before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT
is set in *flags
, encountering an invalid character (except NUL) will also trigger a warning. On return *len_p
is set to the length of the scanned string, and *flags
gives output flags.
If the value is <= UV_MAX
it is returned as a UV, the output flags are clear, and nothing is written to *result
. If the value is > UV_MAX
, grok_bin
returns UV_MAX
, sets PERL_SCAN_GREATER_THAN_UV_MAX
in the output flags, and writes an approximation of the correct value into *result
(which is an NV; or the approximation is discarded if result
is NULL).
The binary number may optionally be prefixed with "0b"
or "b"
unless PERL_SCAN_DISALLOW_PREFIX
is set in *flags
on entry.
If PERL_SCAN_ALLOW_UNDERSCORES
is set in *flags
then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted.
converts a string representing a hex number to numeric form.
On entry start
and *len_p
give the string to scan, *flags
gives conversion flags, and result
should be NULL
or a pointer to an NV. The scan stops at the end of the string, or at just before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT
is set in *flags
, encountering an invalid character (except NUL) will also trigger a warning. On return *len_p
is set to the length of the scanned string, and *flags
gives output flags.
If the value is <= UV_MAX
it is returned as a UV, the output flags are clear, and nothing is written to *result
. If the value is > UV_MAX
, grok_hex
returns UV_MAX
, sets PERL_SCAN_GREATER_THAN_UV_MAX
in the output flags, and writes an approximation of the correct value into *result
(which is an NV; or the approximation is discarded if result
is NULL).
The hex number may optionally be prefixed with "0x"
or "x"
unless PERL_SCAN_DISALLOW_PREFIX
is set in *flags
on entry.
If PERL_SCAN_ALLOW_UNDERSCORES
is set in *flags
then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted.
converts a string representing an octal number to numeric form.
On entry start
and *len_p
give the string to scan, *flags
gives conversion flags, and result
should be NULL
or a pointer to an NV. The scan stops at the end of the string, or at just before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT
is set in *flags
, encountering an invalid character (except NUL) will also trigger a warning. On return *len_p
is set to the length of the scanned string, and *flags
gives output flags.
If the value is <= UV_MAX
it is returned as a UV, the output flags are clear, and nothing is written to *result
. If the value is > UV_MAX
, grok_oct
returns UV_MAX
, sets PERL_SCAN_GREATER_THAN_UV_MAX
in the output flags, and writes an approximation of the correct value into *result
(which is an NV; or the approximation is discarded if result
is NULL).
If PERL_SCAN_ALLOW_UNDERSCORES
is set in *flags
then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted.
The PERL_SCAN_DISALLOW_PREFIX
flag is always treated as being set for this function.
For backwards compatibility. Use grok_bin
instead.
For backwards compatibility. Use grok_hex
instead.
For backwards compatibility. Use grok_oct
instead.
Scan and skip for a numeric decimal separator (radix).
Helper for grok_number()
, accepts various ways of spelling "infinity" or "not a number", and returns one of the following flag combinations:
IS_NUMBER_INFINITY
IS_NUMBER_NAN
IS_NUMBER_INFINITY | IS_NUMBER_NEG
IS_NUMBER_NAN | IS_NUMBER_NEG
0
possibly |-ed with IS_NUMBER_TRAILING
.
If an infinity or a not-a-number is recognized, *sp
will point to one byte past the end of the recognized string. If the recognition fails, zero is returned, and *sp
will not move.
Recognise (or not) a number. The type of the number is returned (0 if unrecognised), otherwise it is a bit-ORed combination of IS_NUMBER_IN_UV
, IS_NUMBER_GREATER_THAN_UV_MAX
, IS_NUMBER_NOT_INT
, IS_NUMBER_NEG
, IS_NUMBER_INFINITY
, IS_NUMBER_NAN
(defined in perl.h).
If the value of the number can fit in a UV, it is returned in *valuep
. IS_NUMBER_IN_UV
will be set to indicate that *valuep
is valid, IS_NUMBER_IN_UV
will never be set unless *valuep
is valid, but *valuep
may have been assigned to during processing even though IS_NUMBER_IN_UV
is not set on return. If valuep
is NULL
, IS_NUMBER_IN_UV
will be set for the same cases as when valuep
is non-NULL
, but no actual assignment (or SEGV) will occur.
IS_NUMBER_NOT_INT
will be set with IS_NUMBER_IN_UV
if trailing decimals were seen (in which case *valuep
gives the true value truncated to an integer), and IS_NUMBER_NEG
if the number is negative (in which case *valuep
holds the absolute value). IS_NUMBER_IN_UV
is not set if e
notation was used or the number is larger than a UV.
flags
allows only PERL_SCAN_TRAILING
, which allows for trailing non-numeric text on an otherwise successful grok, setting IS_NUMBER_TRAILING
on the result.
Identical to grok_number_flags()
with flags
set to zero.
parse a string, looking for a decimal unsigned integer.
On entry, pv
points to the beginning of the string; valptr
points to a UV that will receive the converted value, if found; endptr
is either NULL or points to a variable that points to one byte beyond the point in pv
that this routine should examine. If endptr
is NULL, pv
is assumed to be NUL-terminated.
Returns FALSE if pv
doesn't represent a valid unsigned integer value (with no leading zeros). Otherwise it returns TRUE, and sets *valptr
to that value.
If you constrain the portion of pv
that is looked at by this function (by passing a non-NULL endptr
), and if the initial bytes of that portion form a valid value, it will return TRUE, setting *endptr
to the byte following the final digit of the value. But if there is no constraint at what's looked at, all of pv
must be valid in order for TRUE to be returned. *endptr
is unchanged from its value on input if FALSE is returned;
The only characters this accepts are the decimal digits '0'..'9'.
As opposed to atoi(3) or strtol(3), grok_atoUV
does NOT allow optional leading whitespace, nor negative inputs. If such features are required, the calling code needs to explicitly implement those.
Note that this function returns FALSE for inputs that would overflow a UV, or have leading zeros. Thus a single 0
is accepted, but not 00
nor 01
, 002
, etc.
Background: atoi
has severe problems with illegal inputs, it cannot be used for incremental parsing, and therefore should be avoided atoi
and strtol
are also affected by locale settings, which can also be seen as a bug (global state controlled by user environment).
atof
(3), but properly works with Perl locale handling, accepting a dot radix character always, but also the current locale's radix character if and only if called from within the lexical scope of a Perl use locale
statement.
N.B. s
must be NUL terminated.
Perl_isinfnan()
is a utility function that returns true if the NV argument is either an infinity or a NaN
, false otherwise. To test in more detail, use Perl_isinf()
and Perl_isnan()
.
This is also the logical inverse of Perl_isfinite().
Checks whether the argument would be either an infinity or NaN
when used as a number, but is careful not to trigger non-numeric or uninitialized warnings. it assumes the caller has done SvGETMAGIC(sv)
already.
Note that this always accepts trailing garbage (similar to grok_number_flags
with PERL_SCAN_TRAILING
), so "inferior"
and "NAND gates"
will return true.
Return a non-zero integer if the sign bit on an NV is set, and 0 if it is not.
If Configure detects this system has a signbit()
that will work with our NVs, then we just use it via the #define
in perl.h. Otherwise, fall back on this implementation. The main use of this function is catching -0.0
.
Configure
notes: This function is called 'Perl_signbit'
instead of a plain 'signbit'
because it is easy to imagine a system having a signbit()
function or macro that doesn't happen to work with our particular choice of NVs. We shouldn't just re-#define
signbit
as Perl_signbit
and expect the standard system headers to be happy. Also, this is a no-context function (no pTHX_
) because Perl_signbit()
is usually re-#defined
in perl.h as a simple macro call to the system's signbit()
. Users should just always call Perl_signbit()
.