lib/string-number.x
Numeric parsing from canonical byte strings.
Advanced and interop API
| Function | Summary |
|---|---|
String.try_double | Parses all of str as a floating-point number, writing out. |
String.try_long | Parses all of str as an integer, writing it through out. |
String
String.try_double
int String.try_double(String str, double *out)
Parses all of str as a floating-point number, writing out.
Returns 1 and writes out on success; returns 0 and leaves out
untouched on failure.
The whole string must be the number, on the same terms as
String.try_long: leading and trailing whitespace are allowed and any
other unconsumed character is a failure, so 1.5junk does not parse.
It accepts what C’s strtod accepts, including hex-float literals such
as 0x1p4 and the inf and nan spellings. A value that overflows or
underflows double fails. It does not know the 0o and 0b prefixes
that the integer parser adds, so 0b101 fails instead of yielding the
leading zero.
Source: lib/string-number.x:86
String.try_long
int String.try_long(String str, long *out)
Parses all of str as an integer, writing it through out.
Returns 1 and writes out on success; returns 0 and leaves out
untouched on failure.
The whole string must be the number. Leading and trailing whitespace are
allowed and any other unconsumed character is a failure, so 42 succeeds
and 42junk does not. A + or - sign is accepted, and the radix
prefixes 0x, 0o, 0b, and C’s leading-zero octal are recognized.
A value outside the range of long fails.
long value = 0;
printf("%d %ld\n", %" -0b101 ".try_long(&value), value);
printf("%d\n", %"42junk".try_long(&value));
Source: lib/string-number.x:39
Design notes
The readers require a complete parse and leave caller output untouched on failure. Parsing allocates no storage.