mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
Add PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA
This commit is contained in:
1 parent
fcb6416c16
commit
e0c5de9d8d
5 files changed
+236
-115
No files matched your search
@@ -134,7 +134,8 @@ wuffs_base__private_implementation__high_prec_dec__assign(
|
||||
static wuffs_base__status //
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
wuffs_base__private_implementation__high_prec_dec* h,
|
||||
wuffs_base__slice_u8 s) {
|
||||
wuffs_base__slice_u8 s,
|
||||
uint32_t options) {
|
||||
if (!h) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
@@ -187,7 +188,11 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
goto after_all;
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p ==
|
||||
((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
goto after_sep;
|
||||
} else if ((*p == 'E') || (*p == 'e')) {
|
||||
@@ -213,7 +218,11 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
// Long-tail non-zeroes set the truncated bit.
|
||||
h->truncated = true;
|
||||
}
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p ==
|
||||
((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
goto after_sep;
|
||||
} else if ((*p == 'E') || (*p == 'e')) {
|
||||
@@ -224,7 +233,10 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
}
|
||||
}
|
||||
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p == ((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
no_digits_before_separator = true;
|
||||
|
||||
@@ -963,8 +975,9 @@ wuffs_base__private_implementation__parse_number_f64_eisel(uint64_t man,
|
||||
// --------
|
||||
|
||||
static wuffs_base__result_f64 //
|
||||
wuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s,
|
||||
const char* fallback_status_repr) {
|
||||
wuffs_base__private_implementation__parse_number_f64_special(
|
||||
wuffs_base__slice_u8 s,
|
||||
const char* fallback_status_repr) {
|
||||
do {
|
||||
uint8_t* p = s.ptr;
|
||||
uint8_t* q = s.ptr + s.len;
|
||||
@@ -1064,7 +1077,7 @@ fallback:
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
|
||||
wuffs_base__private_implementation__parse_number_f64__fallback(
|
||||
wuffs_base__private_implementation__high_prec_dec__to_f64(
|
||||
wuffs_base__private_implementation__high_prec_dec* h) {
|
||||
do {
|
||||
// powers converts decimal powers of 10 to binary powers of 2. For example,
|
||||
@@ -1300,7 +1313,10 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
// Walk the "d"s after the optional decimal separator ('.' or ','),
|
||||
// updating the man and exp10 variables.
|
||||
int32_t exp10 = 0;
|
||||
if ((*p == '.') || (*p == ',')) {
|
||||
if (*p ==
|
||||
((options & WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
const uint8_t* first_after_separator_ptr = p;
|
||||
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
|
||||
@@ -1439,11 +1455,13 @@ fallback:
|
||||
do {
|
||||
wuffs_base__private_implementation__high_prec_dec h;
|
||||
wuffs_base__status status =
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(&h, s);
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(&h, s,
|
||||
options);
|
||||
if (status.repr) {
|
||||
return wuffs_base__parse_number_f64_special(s, status.repr);
|
||||
return wuffs_base__private_implementation__parse_number_f64_special(
|
||||
s, status.repr);
|
||||
}
|
||||
return wuffs_base__private_implementation__parse_number_f64__fallback(&h);
|
||||
return wuffs_base__private_implementation__high_prec_dec__to_f64(&h);
|
||||
} while (0);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,32 @@
|
||||
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES means to accept
|
||||
// inputs like "00", "0644" and "00.7". By default, they are rejected.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES \
|
||||
((uint32_t)0x00000001)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES means to accept inputs like
|
||||
// "1__2" and "_3.141_592". By default, they are rejected.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES ((uint32_t)0x00000002)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to accept
|
||||
// "1,5" and not "1.5" as one-and-a-half.
|
||||
//
|
||||
// If the caller wants to accept either, it is responsible for canonicalizing
|
||||
// the input before calling wuffs_base__parse_number_fxx. The caller also has
|
||||
// more context on e.g. exactly how to treat something like "$1,234".
|
||||
#define WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA \
|
||||
((uint32_t)0x00000010)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN means to reject inputs that
|
||||
// would lead to infinite or Not-a-Number floating point values. By default,
|
||||
// they are accepted.
|
||||
//
|
||||
// This affects the literal "inf" as input, but also affects inputs like
|
||||
// "1e999" that would overflow double-precision floating point.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN ((uint32_t)0x00000020)
|
||||
|
||||
// --------
|
||||
|
||||
// Options (bitwise or'ed together) for wuffs_base__render_number_xxx
|
||||
@@ -34,16 +60,16 @@
|
||||
// (higher indexes) of the destination slice, leaving any untouched bytes on
|
||||
// the left side (lower indexes). The default is vice versa: rendering on the
|
||||
// left with slack on the right.
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000010)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000100)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN means to render the leading
|
||||
// "+" for non-negative numbers: "+0" and "+12.3" instead of "0" and "12.3".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000020)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000200)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to render
|
||||
// one-and-a-half as "1,5" instead of "1.5".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA \
|
||||
((uint32_t)0x00000100)
|
||||
((uint32_t)0x00001000)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ETC means whether to never
|
||||
// (EXPONENT_ABSENT, equivalent to printf's "%f") or to always
|
||||
@@ -53,8 +79,8 @@
|
||||
// Having both bits set is the same has having neither bit set, where the
|
||||
// notation used depends on whether the exponent is sufficiently large: "0.5"
|
||||
// is preferred over "5e-01" but "5e-09" is preferred over "0.000000005".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00000200)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00000400)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00002000)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00004000)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION means to render the
|
||||
// smallest number of digits so that parsing the resultant string will recover
|
||||
@@ -66,7 +92,7 @@
|
||||
// 0.3000000000000000444089209850062616169452667236328125 will produce
|
||||
// "0.30000000000000004".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION \
|
||||
((uint32_t)0x00000800)
|
||||
((uint32_t)0x00008000)
|
||||
|
||||
// ---------------- IEEE 754 Floating Point
|
||||
|
||||
@@ -88,20 +114,17 @@
|
||||
// - It does not take an optional endptr argument. It does not allow a partial
|
||||
// parse: it returns an error unless all of s is consumed.
|
||||
// - It does not allow whitespace, leading or otherwise.
|
||||
// - It does not allow unnecessary leading zeroes ("0" is valid and its sole
|
||||
// zero is necessary). All of "00", "0644" and "00.7" are invalid.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
// - Conversely, it always accepts either ',' or '.' as a decimal separator.
|
||||
// In particular, "3,141,592" is always invalid but "3,141" is always valid
|
||||
// (and approximately π). The caller is responsible for e.g. previously
|
||||
// rejecting or filtering s if it contains a comma, if that is unacceptable
|
||||
// to the caller. For example, JSON numbers always use a dot '.' and never a
|
||||
// comma ',', regardless of the LOCALE environment variable.
|
||||
// - It does allow arbitrary underscores. For example, "_3.141_592" would
|
||||
// successfully parse, again approximately π.
|
||||
// - It does allow "inf", "+Infinity" and "-NAN", case insensitive, but it
|
||||
// does not permit "nan" to be followed by an integer mantissa.
|
||||
// - It does not allow hexadecimal floating point numbers.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
//
|
||||
// The options argument can change these, but by default, it:
|
||||
// - Allows "inf", "+Infinity" and "-NAN", case insensitive. Similarly,
|
||||
// without an explicit opt-out, it would successfully parse "1e999" as
|
||||
// infinity, even though it overflows double-precision floating point.
|
||||
// - Rejects underscores. With an explicit opt-in, "_3.141_592" would
|
||||
// successfully parse as an approximation to π.
|
||||
// - Rejects unnecessary leading zeroes: "00", "0644" and "00.7".
|
||||
// - Uses a dot '1.5' instead of a comma '1,5' for the decimal separator.
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not
|
||||
@@ -170,17 +193,19 @@ wuffs_base__parse_number_i64(wuffs_base__slice_u8 s, uint32_t options);
|
||||
// parse: it returns an error unless all of s is consumed.
|
||||
// - It does not allow whitespace, leading or otherwise.
|
||||
// - It does not allow a leading '+' or '-'.
|
||||
// - It does not allow unnecessary leading zeroes ("0" is valid and its sole
|
||||
// zero is necessary). All of "00", "0644" and "007" are invalid.
|
||||
// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it
|
||||
// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g.
|
||||
// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex
|
||||
// numbers if they are unwanted. For example, Wuffs' JSON decoder will only
|
||||
// produce a wuffs_base__token for decimal numbers, not hexadecimal.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
// - It does allow arbitrary underscores, except inside the optional 2-byte
|
||||
// opening "0d" or "0X" that denotes base-10 or base-16. For example,
|
||||
// "__0D_1_002" would successfully parse as "one thousand and two".
|
||||
//
|
||||
// The options argument can change these, but by default, it:
|
||||
// - Rejects underscores. With an explicit opt-in, "__0D_1_002" would
|
||||
// successfully parse as "one thousand and two". Underscores are still
|
||||
// rejected inside the optional 2-byte opening "0d" or "0X" that denotes
|
||||
// base-10 or base-16.
|
||||
// - Rejects unnecessary leading zeroes: "00" and "0644".
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not
|
||||
|
||||
+32
-31
@@ -33,12 +33,12 @@ const BaseFloatConvSubmoduleCodeC = "" +
|
||||
"e ±2047\n// bounds are further away from zero than ±(324 + 800), where 800 and 2047 is\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION and\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.\n//\n// digits[.. num_digits] are the number's digits in big-endian order. The\n// uint8_t values are in the range [0 ..= 9], not ['0' ..= '9'], where e.g. '7'\n// is the ASCII value 0x37.\n//\n// decimal_point is the index (within digits) of the decimal point. It may be\n// negative or be larger than num_digits, in which case the explicit digits are\n// padded with implicit zeroes.\n//\n// For example, if num_digits is 3 and digits is \"\\x07\\x08\\x09\":\n// - A decimal_point of -2 means \".00789\"\n// - A decimal_point of -1 means \".0789\"\n// - A decimal_point of +0 means \".789\"\n// - A decimal_point of +1 means \"7.89\"\n// - A decimal_point of +2 means \"78.9\"\n// - A decimal_point of +3 means \"789.\"\n// - A decimal_point of +4 means \"7890.\"\n// - A decimal_point of +5 means \"78900.\"\n//\n// As above, a" +
|
||||
" decimal_point higher than +2047 means that the overall value is\n// infinity, lower than -2047 means zero.\n//\n// negative is a sign bit. An HPD can distinguish positive and negative zero.\n//\n// truncated is whether there are more than\n// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION digits, and at\n// least one of those extra digits are non-zero. The existence of long-tail\n// digits can affect rounding.\n//\n// The \"all fields are zero\" value is valid, and represents the number +0.\ntypedef struct {\n uint32_t num_digits;\n int32_t decimal_point;\n bool negative;\n bool truncated;\n uint8_t digits[WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION];\n} wuffs_base__private_implementation__high_prec_dec;\n\n// wuffs_base__private_implementation__high_prec_dec__trim trims trailing\n// zeroes from the h->digits[.. h->num_digits] slice. They have no benefit,\n// since we explicitly track h->decimal_point.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic inline void //\nwuffs_base__private_implementation_" +
|
||||
"_high_prec_dec__trim(\n wuffs_base__private_implementation__high_prec_dec* h) {\n while ((h->num_digits > 0) && (h->digits[h->num_digits - 1] == 0)) {\n h->num_digits--;\n }\n}\n\n// wuffs_base__private_implementation__high_prec_dec__assign sets h to\n// represent the number x.\n//\n// Preconditions:\n// - h is non-NULL.\nstatic void //\nwuffs_base__private_implementation__high_prec_dec__assign(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint64_t x,\n bool negative) {\n uint32_t n = 0;\n\n // Set h->digits.\n if (x > 0) {\n // Calculate the digits, working right-to-left. After we determine n (how\n // many digits there are), copy from buf to h->digits.\n //\n // UINT64_MAX, 18446744073709551615, is 20 digits long. It can be faster to\n // copy a constant number of bytes than a variable number (20 instead of\n // n). Make buf large enough (and start writing to it from the middle) so\n // that can we always copy 20 bytes: the slice buf[(20-n) .. (40-n)].\n uint8_t buf[40] = {0};" +
|
||||
"\n uint8_t* ptr = &buf[20];\n do {\n uint64_t remaining = x / 10;\n x -= remaining * 10;\n ptr--;\n *ptr = (uint8_t)x;\n n++;\n x = remaining;\n } while (x > 0);\n memcpy(h->digits, ptr, 20);\n }\n\n // Set h's other fields.\n h->num_digits = n;\n h->decimal_point = (int32_t)n;\n h->negative = negative;\n h->truncated = false;\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n}\n\nstatic wuffs_base__status //\nwuffs_base__private_implementation__high_prec_dec__parse(\n wuffs_base__private_implementation__high_prec_dec* h,\n wuffs_base__slice_u8 s) {\n if (!h) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n h->num_digits = 0;\n h->decimal_point = 0;\n h->negative = false;\n h->truncated = false;\n\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n\n // Parse sign.\n do {\n if (*p" +
|
||||
" == '+') {\n p++;\n } else if (*p == '-') {\n h->negative = true;\n p++;\n } else {\n break;\n }\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n } while (0);\n\n // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each\n // limb in this if-else chain:\n // - \"0.789\"\n // - \"1002.789\"\n // - \".789\"\n // - Other (invalid input).\n uint32_t nd = 0;\n int32_t dp = 0;\n bool no_digits_before_separator = false;\n if ('0' == *p) {\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if ((*p == '.') || (*p == ',')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p != '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (('0' < *p) && (*p <= '9')) {\n h->digits[nd++] = (uint" +
|
||||
"8_t)(*p - '0');\n dp = (int32_t)nd;\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (('0' <= *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n } else if ('0' != *p) {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if ((*p == '.') || (*p == ',')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p != '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if ((*p == '.') || (*p == ',')) {\n p++;\n no_digits_before_separator = true;\n\n } else {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\nafter_sep:\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if ('0' == *p) {\n if (nd == 0) {\n " +
|
||||
" // Track leading zeroes implicitly.\n dp--;\n } else if (nd <\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n }\n } else if (('0' < *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n } else {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p != '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\nafter_exp:\n do {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n\n int32_t exp_sign = +1;\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n exp_sign = -1;\n p++;\n }\n\n int32_t exp = 0;\n const int3" +
|
||||
"2_t exp_large =\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;\n bool saw_exp_digits = false;\n for (; p < q; p++) {\n if (*p == '_') {\n // No-op.\n } else if (('0' <= *p) && (*p <= '9')) {\n saw_exp_digits = true;\n if (exp < exp_large) {\n exp = (10 * exp) + ((int32_t)(*p - '0'));\n }\n } else {\n break;\n }\n }\n if (!saw_exp_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n dp += exp_sign * exp;\n } while (0);\n\nafter_all:\n if (p != q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->num_digits = nd;\n if (nd == 0) {\n if (no_digits_before_separator) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->decimal_point = 0;\n } else if (dp <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n -" +
|
||||
"WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;\n } else if (dp >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;\n } else {\n h->decimal_point = dp;\n }\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n return wuffs_base__make_status(NULL);\n}\n\n" +
|
||||
"\n uint8_t* ptr = &buf[20];\n do {\n uint64_t remaining = x / 10;\n x -= remaining * 10;\n ptr--;\n *ptr = (uint8_t)x;\n n++;\n x = remaining;\n } while (x > 0);\n memcpy(h->digits, ptr, 20);\n }\n\n // Set h's other fields.\n h->num_digits = n;\n h->decimal_point = (int32_t)n;\n h->negative = negative;\n h->truncated = false;\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n}\n\nstatic wuffs_base__status //\nwuffs_base__private_implementation__high_prec_dec__parse(\n wuffs_base__private_implementation__high_prec_dec* h,\n wuffs_base__slice_u8 s,\n uint32_t options) {\n if (!h) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n h->num_digits = 0;\n h->decimal_point = 0;\n h->negative = false;\n h->truncated = false;\n\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n\n // Parse s" +
|
||||
"ign.\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n h->negative = true;\n p++;\n } else {\n break;\n }\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n } while (0);\n\n // Parse digits, up to (and including) a '.', 'E' or 'e'. Examples for each\n // limb in this if-else chain:\n // - \"0.789\"\n // - \"1002.789\"\n // - \".789\"\n // - Other (invalid input).\n uint32_t nd = 0;\n int32_t dp = 0;\n bool no_digits_before_separator = false;\n if ('0' == *p) {\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (*p ==\n ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p !" +
|
||||
"= '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\n } else if (('0' < *p) && (*p <= '9')) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n p++;\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if (('0' <= *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n dp = (int32_t)nd;\n } else if ('0' != *p) {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if (*p ==\n ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n goto after_sep;\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p != '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n " +
|
||||
" }\n }\n\n } else if (*p == ((options &\n WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p++;\n no_digits_before_separator = true;\n\n } else {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\nafter_sep:\n for (;; p++) {\n if (p >= q) {\n goto after_all;\n } else if ('0' == *p) {\n if (nd == 0) {\n // Track leading zeroes implicitly.\n dp--;\n } else if (nd <\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n }\n } else if (('0' < *p) && (*p <= '9')) {\n if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {\n h->digits[nd++] = (uint8_t)(*p - '0');\n } else {\n // Long-tail non-zeroes set the truncated bit.\n h->truncated = true;\n }\n } else if ((*p == 'E') || (*p == 'e')) {\n p++;\n goto after_exp;\n } else if (*p " +
|
||||
"!= '_') {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n }\n\nafter_exp:\n do {\n for (;; p++) {\n if (p >= q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n } else if (*p != '_') {\n break;\n }\n }\n\n int32_t exp_sign = +1;\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n exp_sign = -1;\n p++;\n }\n\n int32_t exp = 0;\n const int32_t exp_large =\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +\n WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;\n bool saw_exp_digits = false;\n for (; p < q; p++) {\n if (*p == '_') {\n // No-op.\n } else if (('0' <= *p) && (*p <= '9')) {\n saw_exp_digits = true;\n if (exp < exp_large) {\n exp = (10 * exp) + ((int32_t)(*p - '0'));\n }\n } else {\n break;\n }\n }\n if (!saw_exp_digits) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n dp +" +
|
||||
"= exp_sign * exp;\n } while (0);\n\nafter_all:\n if (p != q) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->num_digits = nd;\n if (nd == 0) {\n if (no_digits_before_separator) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n h->decimal_point = 0;\n } else if (dp <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;\n } else if (dp >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n h->decimal_point =\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;\n } else {\n h->decimal_point = dp;\n }\n wuffs_base__private_implementation__high_prec_dec__trim(h);\n return wuffs_base__make_status(NULL);\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits\n// returns the number of additional decimal digits when left-shifting by shift.\n//\n// See below for preconditions.\nstatic uint32_t //\nwuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t shift) {\n // Masking with 0x3F should be unnecessary (assuming the preconditions) but\n // it's cheap and ensures that we don't overflow the\n // wuffs_base__private_implementation__hpd_left_shift array.\n shift &= 63;\n\n uint32_t x_a = wuffs_base__private_implementation__hpd_left_shift[shift];\n uint32_t x_b = wuffs_base__private_implementation__hpd_left_shift[shift + 1];\n uint32_t num_new_digits = x_a >> 11;\n uint32_t pow5_a = 0x7FF & x_a;\n uint32_t pow5_b = 0x7FF & x_b;\n\n const uint8_t* pow5 =\n &wuffs_base__private_implementation__powers_of_5[pow5_a];\n uint32_t i = 0;\n uint32_t n = pow5_b - pow5_a;\n for (; i < n; i++) {\n if (i >" +
|
||||
"= h->num_digits) {\n return num_new_digits - 1;\n } else if (h->digits[i] == pow5[i]) {\n continue;\n } else if (h->digits[i] < pow5[i]) {\n return num_new_digits - 1;\n } else {\n return num_new_digits;\n }\n }\n return num_new_digits;\n}\n\n" +
|
||||
@@ -70,23 +70,23 @@ const BaseFloatConvSubmoduleCodeC = "" +
|
||||
"o \"73 or 74,\n // depending on msb\", but a flat \"73\" is simpler.\n if ((x_lo == 0) && ((x_hi & 0x1FF) == 0) && ((ret_mantissa & 3) == 1)) {\n return -1;\n }\n\n // If we're not halfway then it's rounding to-nearest. Starting with a 54-bit\n // number, carry the lowest bit (bit 0) up if it's on. Regardless of whether\n // it was on or off, shifting right by one then produces a 53-bit number. If\n // carrying up overflowed, shift again.\n ret_mantissa += ret_mantissa & 1;\n ret_mantissa >>= 1;\n if ((ret_mantissa >> 53) > 0) {\n ret_mantissa >>= 1;\n ret_exp2++;\n }\n\n // Starting with a 53-bit number, IEEE 754 double-precision normal numbers\n // have an implicit mantissa bit. Mask that away and keep the low 52 bits.\n ret_mantissa &= 0x000FFFFFFFFFFFFF;\n\n // IEEE 754 double-precision floating point has 11 exponent bits. All off (0)\n // means subnormal numbers. All on (2047) means infinity or NaN.\n if ((ret_exp2 <= 0) || (2047 <= ret_exp2)) {\n return -1;\n }\n\n // Pack the bits and return.\n return " +
|
||||
"((int64_t)(ret_mantissa | (ret_exp2 << 52)));\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\nstatic wuffs_base__result_f64 //\nwuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s,\n const char* fallback_status_repr) {\n do {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n goto fallback;\n }\n\n // Parse sign.\n bool negative = false;\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n negative = true;\n p++;\n } else {\n break;\n }\n for (; (p < q) && (*p == '_'); p++) {\n }\n } while (0);\n if (p >= q) {\n goto fallback;\n }\n\n bool nan = false;\n switch (p[0]) {\n case 'I':\n case 'i':\n if (((q - p) < 3) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'F') && (p[2] != 'f'))) {\n goto fallback;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n break;\n } else if (((q - p) < 5) || " +
|
||||
" //\n ((p[0] != 'I') && (p[0] != 'i')) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'I') && (p[2] != 'i')) || //\n ((p[3] != 'T') && (p[3] != 't')) || //\n ((p[4] != 'Y') && (p[4] != 'y'))) {\n goto fallback;\n }\n p += 5;\n\n if ((p >= q) || (*p == '_')) {\n break;\n }\n goto fallback;\n\n case 'N':\n case 'n':\n if (((q - p) < 3) || //\n ((p[1] != 'A') && (p[1] != 'a')) || //\n ((p[2] != 'N') && (p[2] != 'n'))) {\n goto fallback;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n nan = true;\n break;\n }\n goto fallback;\n\n default:\n goto fallback;\n }\n\n // Finish.\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p != q) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.va" +
|
||||
"lue = wuffs_base__ieee_754_bit_representation__to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = fallback_status_repr;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__private_implementation__parse_number_f64__fallback(\n wuffs_base__private_implementation__high_prec_dec* h) {\n do {\n // powers converts decimal powers of 10 to binary powers of 2. For example,\n // (10000 >> 13) is 1. It stops before the elements exceed 60, also known\n // as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.\n static const uint32_t num_powers = 19;\n static const uint8_t powers[19] = {\n 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, //\n 33, 36, 39, 43, 46, 49, 53, 56, 59, //\n };\n\n // Handle zero and obvious extremes. The largest and smallest positive\n // f" +
|
||||
"inite f64 values are approximately 1.8e+308 and 4.9e-324.\n if ((h->num_digits == 0) || (h->decimal_point < -326)) {\n goto zero;\n } else if (h->decimal_point > 310) {\n goto infinity;\n }\n\n // Try the fast Eisel algorithm again. Calculating the (man, exp10) pair\n // from the high_prec_dec h is more correct but slower than the approach\n // taken in wuffs_base__parse_number_f64. The latter is optimized for the\n // common cases (e.g. assuming no underscores or a leading '+' sign) rather\n // than the full set of cases allowed by the Wuffs API.\n if (h->num_digits <= 19) {\n uint64_t man = 0;\n uint32_t i;\n for (i = 0; i < h->num_digits; i++) {\n man = (10 * man) + h->digits[i];\n }\n int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));\n if ((man != 0) && (-326 <= exp10) && (exp10 <= 310)) {\n int64_t r = wuffs_base__private_implementation__parse_number_f64_eisel(\n man, exp10);\n if (r >= 0) {\n wuffs_base__re" +
|
||||
"sult_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)(h->negative)) << 63));\n return ret;\n }\n }\n }\n\n // Scale by powers of 2 until we're in the range [½ .. 1], which gives us\n // our exponent (in base-2). First we shift right, possibly a little too\n // far, ending with a value certainly below 1 and possibly below ½...\n const int32_t f64_bias = -1023;\n int32_t exp2 = 0;\n while (h->decimal_point > 0) {\n uint32_t n = (uint32_t)(+h->decimal_point);\n uint32_t shift =\n (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);\n if (h->decimal_point <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto zero;\n }\n exp2 += (int32_t)shift;\n }\n // ...then we " +
|
||||
"shift left, putting us in [½ .. 1].\n while (h->decimal_point <= 0) {\n uint32_t shift;\n if (h->decimal_point == 0) {\n if (h->digits[0] >= 5) {\n break;\n }\n shift = (h->digits[0] <= 2) ? 2 : 1;\n } else {\n uint32_t n = (uint32_t)(-h->decimal_point);\n shift = (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);\n if (h->decimal_point >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto infinity;\n }\n exp2 -= (int32_t)shift;\n }\n\n // We're in the range [½ .. 1] but f64 uses [1 .. 2].\n exp2--;\n\n // The minimum normal exponent is (f64_bias + 1).\n while ((f64_bias + 1) > exp2) {\n uint32_t n = (uint32_t)((f64_bias + 1) - exp2);\n if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {\n n = WUFFS_BASE__" +
|
||||
"PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);\n exp2 += (int32_t)n;\n }\n\n // Check for overflow.\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n\n // Extract 53 bits for the mantissa (in base-2).\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, 53);\n uint64_t man2 =\n wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);\n\n // Rounding might have added one bit. If so, shift and re-check overflow.\n if ((man2 >> 53) != 0) {\n man2 >>= 1;\n exp2++;\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n }\n\n // Handle subnormal numbers.\n if ((man2 >> 52) == 0) {\n exp2 = f64_bias;\n }\n\n // Pack the bits and return.\n uint64_t exp2_bits =\n (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.\n uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | " +
|
||||
" // (1 << 52) - 1.\n (exp2_bits << 52) | //\n (h->negative ? 0x8000000000000000 : 0); // (1 << 63).\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\nzero:\n do {\n uint64_t bits = h->negative ? 0x8000000000000000 : 0;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\ninfinity:\n do {\n uint64_t bits = h->negative ? 0xFFF0000000000000 : 0x7FF0000000000000;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n}\n\nstatic inline bool //\nwuffs_base__private_implementation__is_decimal_digit(uint8_t c) {\n return ('0' <= c) && (c <= '9');\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_numb" +
|
||||
"er_f64(wuffs_base__slice_u8 s, uint32_t options) {\n // In practice, almost all \"dd.ddddE±xxx\" numbers can be represented\n // losslessly by a uint64_t mantissa \"dddddd\" and an int32_t base-10\n // exponent, adjusting \"xxx\" for the position (if present) of the decimal\n // separator '.' or ','.\n //\n // This (u64 man, i32 exp10) data structure is superficially similar to the\n // \"Do It Yourself Floating Point\" type from Loitsch (†), but the exponent\n // here is base-10, not base-2.\n //\n // If s's number fits in a (man, exp10), parse that pair with the Eisel\n // algorithm. If not, or if Eisel fails, parsing s with the fallback\n // algorithm is slower but comprehensive.\n //\n // † \"Printing Floating-Point Numbers Quickly and Accurately with Integers\"\n // (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).\n // Florian Loitsch is also the primary contributor to\n // https://github.com/google/double-conversion\n do {\n // Calculating that (man, exp10) pair needs to stay within" +
|
||||
" s's bounds.\n // Provided that s isn't extremely long, work on a NUL-terminated copy of\n // s's contents. The NUL byte isn't a valid part of \"±dd.ddddE±xxx\".\n //\n // As the pointer p walks the contents, it's faster to repeatedly check \"is\n // *p a valid digit\" than \"is p within bounds and *p a valid digit\".\n if (s.len >= 256) {\n goto fallback;\n }\n uint8_t z[256];\n memcpy(&z[0], s.ptr, s.len);\n z[s.len] = 0;\n const uint8_t* p = &z[0];\n\n // Look for a leading minus sign. Technically, we could also look for an\n // optional plus sign, but the \"script/process-json-numbers.c with -p\"\n // benchmark is noticably slower if we do. It's optional and, in practice,\n // usually absent. Let the fallback catch it.\n bool negative = (*p == '-');\n if (negative) {\n p++;\n }\n\n // After walking \"dd.dddd\", comparing p later with p now will produce the\n // number of \"d\"s and \".\"s.\n const uint8_t* const start_of_digits_ptr = p;\n\n // Walk the \"d\"s before a '." +
|
||||
"', 'E', NUL byte, etc. If it starts with '0',\n // it must be a single '0'. If it starts with a non-zero decimal digit, it\n // can be a sequence of decimal digits.\n //\n // Update the man variable during the walk. It's OK if man overflows now.\n // We'll detect that later.\n uint64_t man;\n if (*p == '0') {\n man = 0;\n p++;\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n } else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n man = ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n } else {\n goto fallback;\n }\n\n // Walk the \"d\"s after the optional decimal separator ('.' or ','),\n // updating the man and exp10 variables.\n int32_t exp10 = 0;\n if ((*p == '.') || (*p == ',')) {\n p++;\n const uint8_t* first_after_separator_ptr = p;\n if (!wuffs_base__private_im" +
|
||||
"plementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n man = (10 * man) + ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n exp10 = ((int32_t)(first_after_separator_ptr - p));\n }\n\n // Count the number of digits:\n // - for an input of \"314159\", digit_count is 6.\n // - for an input of \"3.14159\", digit_count is 7.\n //\n // This is off-by-one if there is a decimal separator. That's OK for now.\n // We'll correct for that later. The \"script/process-json-numbers.c with\n // -p\" benchmark is noticably slower if we try to correct for that now.\n uint32_t digit_count = (uint32_t)(p - start_of_digits_ptr);\n\n // Update exp10 for the optional exponent, starting with 'E' or 'e'.\n if ((*p | 0x20) == 'e') {\n p++;\n int32_t exp_sign = +1;\n if (*p == '-') {\n p++;\n exp_sign = -1;\n } else if (*p == '+') {\n p++;\n " +
|
||||
" }\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n int32_t exp_num = ((uint8_t)(*p - '0'));\n p++;\n // The rest of the exp_num walking has a peculiar control flow but, once\n // again, the \"script/process-json-numbers.c with -p\" benchmark is\n // sensitive to alternative formulations.\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n while (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n if (exp_num > 0x1000000) {\n goto fallback;\n }\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n exp10 += exp_sign * exp_num;\n }\n\n // The Wuffs API is that the original slice has no trailing data. It also\n // allows unde" +
|
||||
"rscores, which we don't catch here but the fallback should.\n if (p != &z[s.len]) {\n goto fallback;\n }\n\n // Check that the uint64_t typed man variable has not overflowed, based on\n // digit_count.\n //\n // For reference:\n // - (1 << 63) is 9223372036854775808, which has 19 decimal digits.\n // - (1 << 64) is 18446744073709551616, which has 20 decimal digits.\n // - 19 nines, 9999999999999999999, is 0x8AC7230489E7FFFF, which has 64\n // bits and 16 hexadecimal digits.\n // - 20 nines, 99999999999999999999, is 0x56BC75E2D630FFFFF, which has 67\n // bits and 17 hexadecimal digits.\n if (digit_count > 19) {\n // Even if we have more than 19 pseudo-digits, it's not yet definitely an\n // overflow. Recall that digit_count might be off-by-one (too large) if\n // there's a decimal separator. It will also over-report the number of\n // meaningful digits if the input looks something like \"0.000dddExxx\".\n //\n // We adjust by the number of l" +
|
||||
"eading '0's and '.'s and re-compare to 19.\n // Once again, technically, we could skip ','s too, but that perturbs the\n // \"script/process-json-numbers.c with -p\" benchmark.\n const uint8_t* q = start_of_digits_ptr;\n for (; (*q == '0') || (*q == '.'); q++) {\n }\n digit_count -= (uint32_t)(q - start_of_digits_ptr);\n if (digit_count > 19) {\n goto fallback;\n }\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that exp10 is in the range -326 ..= 310.\n if ((exp10 < -326) || (310 < exp10)) {\n goto fallback;\n }\n\n // If man and exp10 are small enough, all three of (man), (10 ** exp10) and\n // (man ** (10 ** exp10)) are exactly representable by a double. We don't\n // need to run the Eisel algorithm.\n if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {\n double d = (double)man;\n if (exp10 >= 0) {\n d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];\n } el" +
|
||||
"se {\n d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = negative ? -d : +d;\n return ret;\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that man is non-zero. Parsing \"0\" should be caught\n // by the \"If man and exp10 are small enough\" above, but \"0e99\" might not.\n if (man == 0) {\n goto fallback;\n }\n\n // Our man and exp10 are in range. Run the Eisel algorithm.\n int64_t r =\n wuffs_base__private_implementation__parse_number_f64_eisel(man, exp10);\n if (r < 0) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)negative) << 63));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__private_implementation__high_prec_dec h;\n wuffs_base__status status =\n wu" +
|
||||
"ffs_base__private_implementation__high_prec_dec__parse(&h, s);\n if (status.repr) {\n return wuffs_base__parse_number_f64_special(s, status.repr);\n }\n return wuffs_base__private_implementation__parse_number_f64__fallback(&h);\n } while (0);\n}\n\n" +
|
||||
"// --------\n\nstatic wuffs_base__result_f64 //\nwuffs_base__private_implementation__parse_number_f64_special(\n wuffs_base__slice_u8 s,\n const char* fallback_status_repr) {\n do {\n uint8_t* p = s.ptr;\n uint8_t* q = s.ptr + s.len;\n\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p >= q) {\n goto fallback;\n }\n\n // Parse sign.\n bool negative = false;\n do {\n if (*p == '+') {\n p++;\n } else if (*p == '-') {\n negative = true;\n p++;\n } else {\n break;\n }\n for (; (p < q) && (*p == '_'); p++) {\n }\n } while (0);\n if (p >= q) {\n goto fallback;\n }\n\n bool nan = false;\n switch (p[0]) {\n case 'I':\n case 'i':\n if (((q - p) < 3) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'F') && (p[2] != 'f'))) {\n goto fallback;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n break;\n } else if (((q - p) < 5) || " +
|
||||
" //\n ((p[0] != 'I') && (p[0] != 'i')) || //\n ((p[1] != 'N') && (p[1] != 'n')) || //\n ((p[2] != 'I') && (p[2] != 'i')) || //\n ((p[3] != 'T') && (p[3] != 't')) || //\n ((p[4] != 'Y') && (p[4] != 'y'))) {\n goto fallback;\n }\n p += 5;\n\n if ((p >= q) || (*p == '_')) {\n break;\n }\n goto fallback;\n\n case 'N':\n case 'n':\n if (((q - p) < 3) || //\n ((p[1] != 'A') && (p[1] != 'a')) || //\n ((p[2] != 'N') && (p[2] != 'n'))) {\n goto fallback;\n }\n p += 3;\n\n if ((p >= q) || (*p == '_')) {\n nan = true;\n break;\n }\n goto fallback;\n\n default:\n goto fallback;\n }\n\n // Finish.\n for (; (p < q) && (*p == '_'); p++) {\n }\n if (p != q) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value " +
|
||||
"= wuffs_base__ieee_754_bit_representation__to_f64(\n (nan ? 0x7FFFFFFFFFFFFFFF : 0x7FF0000000000000) |\n (negative ? 0x8000000000000000 : 0));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_base__result_f64 ret;\n ret.status.repr = fallback_status_repr;\n ret.value = 0;\n return ret;\n } while (0);\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__private_implementation__high_prec_dec__to_f64(\n wuffs_base__private_implementation__high_prec_dec* h) {\n do {\n // powers converts decimal powers of 10 to binary powers of 2. For example,\n // (10000 >> 13) is 1. It stops before the elements exceed 60, also known\n // as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.\n static const uint32_t num_powers = 19;\n static const uint8_t powers[19] = {\n 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, //\n 33, 36, 39, 43, 46, 49, 53, 56, 59, //\n };\n\n // Handle zero and obvious extremes. The largest and smallest positive\n // finite f64" +
|
||||
" values are approximately 1.8e+308 and 4.9e-324.\n if ((h->num_digits == 0) || (h->decimal_point < -326)) {\n goto zero;\n } else if (h->decimal_point > 310) {\n goto infinity;\n }\n\n // Try the fast Eisel algorithm again. Calculating the (man, exp10) pair\n // from the high_prec_dec h is more correct but slower than the approach\n // taken in wuffs_base__parse_number_f64. The latter is optimized for the\n // common cases (e.g. assuming no underscores or a leading '+' sign) rather\n // than the full set of cases allowed by the Wuffs API.\n if (h->num_digits <= 19) {\n uint64_t man = 0;\n uint32_t i;\n for (i = 0; i < h->num_digits; i++) {\n man = (10 * man) + h->digits[i];\n }\n int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));\n if ((man != 0) && (-326 <= exp10) && (exp10 <= 310)) {\n int64_t r = wuffs_base__private_implementation__parse_number_f64_eisel(\n man, exp10);\n if (r >= 0) {\n wuffs_base__result_f64 " +
|
||||
"ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)(h->negative)) << 63));\n return ret;\n }\n }\n }\n\n // Scale by powers of 2 until we're in the range [½ .. 1], which gives us\n // our exponent (in base-2). First we shift right, possibly a little too\n // far, ending with a value certainly below 1 and possibly below ½...\n const int32_t f64_bias = -1023;\n int32_t exp2 = 0;\n while (h->decimal_point > 0) {\n uint32_t n = (uint32_t)(+h->decimal_point);\n uint32_t shift =\n (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);\n if (h->decimal_point <\n -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto zero;\n }\n exp2 += (int32_t)shift;\n }\n // ...then we shift lef" +
|
||||
"t, putting us in [½ .. 1].\n while (h->decimal_point <= 0) {\n uint32_t shift;\n if (h->decimal_point == 0) {\n if (h->digits[0] >= 5) {\n break;\n }\n shift = (h->digits[0] <= 2) ? 2 : 1;\n } else {\n uint32_t n = (uint32_t)(-h->decimal_point);\n shift = (n < num_powers)\n ? powers[n]\n : WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);\n if (h->decimal_point >\n +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {\n goto infinity;\n }\n exp2 -= (int32_t)shift;\n }\n\n // We're in the range [½ .. 1] but f64 uses [1 .. 2].\n exp2--;\n\n // The minimum normal exponent is (f64_bias + 1).\n while ((f64_bias + 1) > exp2) {\n uint32_t n = (uint32_t)((f64_bias + 1) - exp2);\n if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {\n n = WUFFS_BASE__PRIVATE_I" +
|
||||
"MPLEMENTATION__HPD__SHIFT__MAX_INCL;\n }\n wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);\n exp2 += (int32_t)n;\n }\n\n // Check for overflow.\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n\n // Extract 53 bits for the mantissa (in base-2).\n wuffs_base__private_implementation__high_prec_dec__small_lshift(h, 53);\n uint64_t man2 =\n wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);\n\n // Rounding might have added one bit. If so, shift and re-check overflow.\n if ((man2 >> 53) != 0) {\n man2 >>= 1;\n exp2++;\n if ((exp2 - f64_bias) >= 0x07FF) { // (1 << 11) - 1.\n goto infinity;\n }\n }\n\n // Handle subnormal numbers.\n if ((man2 >> 52) == 0) {\n exp2 = f64_bias;\n }\n\n // Pack the bits and return.\n uint64_t exp2_bits =\n (uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.\n uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | " +
|
||||
"// (1 << 52) - 1.\n (exp2_bits << 52) | //\n (h->negative ? 0x8000000000000000 : 0); // (1 << 63).\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\nzero:\n do {\n uint64_t bits = h->negative ? 0x8000000000000000 : 0;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n\ninfinity:\n do {\n uint64_t bits = h->negative ? 0xFFF0000000000000 : 0x7FF0000000000000;\n\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(bits);\n return ret;\n } while (0);\n}\n\nstatic inline bool //\nwuffs_base__private_implementation__is_decimal_digit(uint8_t c) {\n return ('0' <= c) && (c <= '9');\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wu" +
|
||||
"ffs_base__slice_u8 s, uint32_t options) {\n // In practice, almost all \"dd.ddddE±xxx\" numbers can be represented\n // losslessly by a uint64_t mantissa \"dddddd\" and an int32_t base-10\n // exponent, adjusting \"xxx\" for the position (if present) of the decimal\n // separator '.' or ','.\n //\n // This (u64 man, i32 exp10) data structure is superficially similar to the\n // \"Do It Yourself Floating Point\" type from Loitsch (†), but the exponent\n // here is base-10, not base-2.\n //\n // If s's number fits in a (man, exp10), parse that pair with the Eisel\n // algorithm. If not, or if Eisel fails, parsing s with the fallback\n // algorithm is slower but comprehensive.\n //\n // † \"Printing Floating-Point Numbers Quickly and Accurately with Integers\"\n // (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).\n // Florian Loitsch is also the primary contributor to\n // https://github.com/google/double-conversion\n do {\n // Calculating that (man, exp10) pair needs to stay within s's boun" +
|
||||
"ds.\n // Provided that s isn't extremely long, work on a NUL-terminated copy of\n // s's contents. The NUL byte isn't a valid part of \"±dd.ddddE±xxx\".\n //\n // As the pointer p walks the contents, it's faster to repeatedly check \"is\n // *p a valid digit\" than \"is p within bounds and *p a valid digit\".\n if (s.len >= 256) {\n goto fallback;\n }\n uint8_t z[256];\n memcpy(&z[0], s.ptr, s.len);\n z[s.len] = 0;\n const uint8_t* p = &z[0];\n\n // Look for a leading minus sign. Technically, we could also look for an\n // optional plus sign, but the \"script/process-json-numbers.c with -p\"\n // benchmark is noticably slower if we do. It's optional and, in practice,\n // usually absent. Let the fallback catch it.\n bool negative = (*p == '-');\n if (negative) {\n p++;\n }\n\n // After walking \"dd.dddd\", comparing p later with p now will produce the\n // number of \"d\"s and \".\"s.\n const uint8_t* const start_of_digits_ptr = p;\n\n // Walk the \"d\"s before a '.', 'E', N" +
|
||||
"UL byte, etc. If it starts with '0',\n // it must be a single '0'. If it starts with a non-zero decimal digit, it\n // can be a sequence of decimal digits.\n //\n // Update the man variable during the walk. It's OK if man overflows now.\n // We'll detect that later.\n uint64_t man;\n if (*p == '0') {\n man = 0;\n p++;\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n } else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n man = ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n } else {\n goto fallback;\n }\n\n // Walk the \"d\"s after the optional decimal separator ('.' or ','),\n // updating the man and exp10 variables.\n int32_t exp10 = 0;\n if (*p ==\n ((options & WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)\n ? ','\n : '.')) {\n p" +
|
||||
"++;\n const uint8_t* first_after_separator_ptr = p;\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n man = (10 * man) + ((uint8_t)(*p - '0'));\n p++;\n for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {\n man = (10 * man) + ((uint8_t)(*p - '0'));\n }\n exp10 = ((int32_t)(first_after_separator_ptr - p));\n }\n\n // Count the number of digits:\n // - for an input of \"314159\", digit_count is 6.\n // - for an input of \"3.14159\", digit_count is 7.\n //\n // This is off-by-one if there is a decimal separator. That's OK for now.\n // We'll correct for that later. The \"script/process-json-numbers.c with\n // -p\" benchmark is noticably slower if we try to correct for that now.\n uint32_t digit_count = (uint32_t)(p - start_of_digits_ptr);\n\n // Update exp10 for the optional exponent, starting with 'E' or 'e'.\n if ((*p | 0x20) == 'e') {\n p++;\n int32_t exp_sign = +1;\n if (*p ==" +
|
||||
" '-') {\n p++;\n exp_sign = -1;\n } else if (*p == '+') {\n p++;\n }\n if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {\n goto fallback;\n }\n int32_t exp_num = ((uint8_t)(*p - '0'));\n p++;\n // The rest of the exp_num walking has a peculiar control flow but, once\n // again, the \"script/process-json-numbers.c with -p\" benchmark is\n // sensitive to alternative formulations.\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n if (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n while (wuffs_base__private_implementation__is_decimal_digit(*p)) {\n if (exp_num > 0x1000000) {\n goto fallback;\n }\n exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));\n p++;\n }\n exp10 += exp_sign * exp_num;\n }\n\n // " +
|
||||
"The Wuffs API is that the original slice has no trailing data. It also\n // allows underscores, which we don't catch here but the fallback should.\n if (p != &z[s.len]) {\n goto fallback;\n }\n\n // Check that the uint64_t typed man variable has not overflowed, based on\n // digit_count.\n //\n // For reference:\n // - (1 << 63) is 9223372036854775808, which has 19 decimal digits.\n // - (1 << 64) is 18446744073709551616, which has 20 decimal digits.\n // - 19 nines, 9999999999999999999, is 0x8AC7230489E7FFFF, which has 64\n // bits and 16 hexadecimal digits.\n // - 20 nines, 99999999999999999999, is 0x56BC75E2D630FFFFF, which has 67\n // bits and 17 hexadecimal digits.\n if (digit_count > 19) {\n // Even if we have more than 19 pseudo-digits, it's not yet definitely an\n // overflow. Recall that digit_count might be off-by-one (too large) if\n // there's a decimal separator. It will also over-report the number of\n // meaningful digits if the " +
|
||||
"input looks something like \"0.000dddExxx\".\n //\n // We adjust by the number of leading '0's and '.'s and re-compare to 19.\n // Once again, technically, we could skip ','s too, but that perturbs the\n // \"script/process-json-numbers.c with -p\" benchmark.\n const uint8_t* q = start_of_digits_ptr;\n for (; (*q == '0') || (*q == '.'); q++) {\n }\n digit_count -= (uint32_t)(q - start_of_digits_ptr);\n if (digit_count > 19) {\n goto fallback;\n }\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that exp10 is in the range -326 ..= 310.\n if ((exp10 < -326) || (310 < exp10)) {\n goto fallback;\n }\n\n // If man and exp10 are small enough, all three of (man), (10 ** exp10) and\n // (man ** (10 ** exp10)) are exactly representable by a double. We don't\n // need to run the Eisel algorithm.\n if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {\n double d = (double)man;\n if (exp10 >= 0" +
|
||||
") {\n d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];\n } else {\n d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = negative ? -d : +d;\n return ret;\n }\n\n // The wuffs_base__private_implementation__parse_number_f64_eisel\n // preconditions include that man is non-zero. Parsing \"0\" should be caught\n // by the \"If man and exp10 are small enough\" above, but \"0e99\" might not.\n if (man == 0) {\n goto fallback;\n }\n\n // Our man and exp10 are in range. Run the Eisel algorithm.\n int64_t r =\n wuffs_base__private_implementation__parse_number_f64_eisel(man, exp10);\n if (r < 0) {\n goto fallback;\n }\n wuffs_base__result_f64 ret;\n ret.status.repr = NULL;\n ret.value = wuffs_base__ieee_754_bit_representation__to_f64(\n ((uint64_t)r) | (((uint64_t)negative) << 63));\n return ret;\n } while (0);\n\nfallback:\n do {\n wuffs_" +
|
||||
"base__private_implementation__high_prec_dec h;\n wuffs_base__status status =\n wuffs_base__private_implementation__high_prec_dec__parse(&h, s,\n options);\n if (status.repr) {\n return wuffs_base__private_implementation__parse_number_f64_special(\n s, status.repr);\n }\n return wuffs_base__private_implementation__high_prec_dec__to_f64(&h);\n } while (0);\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\nstatic inline size_t //\nwuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,\n bool neg,\n uint32_t options) {\n if (neg) {\n if (dst.len < 4) {\n return 0;\n }\n wuffs_base__store_u32le__no_bounds_check(dst.ptr, 0x666E492D); // '-Inf'le.\n return 4;\n }\n\n if (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN) {\n if (dst.len < 4) {\n return 0;\n }\n wuffs_base__store_u32le__no_bounds_check(dst.ptr, 0x666E492B); // '+Inf'le.\n return 4;\n }\n\n if (dst.len < 3) {\n return 0;\n }\n wuffs_base__store_u24le__no_bounds_check(dst.ptr, 0x666E49); // 'Inf'le.\n return 3;\n}\n\nstatic inline size_t //\nwuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {\n if (dst.len < 3) {\n return 0;\n }\n wuffs_base__store_u24le__no_bounds_check(dst.ptr, 0x4E614E); // 'NaN'le.\n return 3;\n}\n\nstatic size_t //\nwuffs_base__private_implementation__high" +
|
||||
"_prec_dec__render_exponent_absent(\n wuffs_base__slice_u8 dst,\n wuffs_base__private_implementation__high_prec_dec* h,\n uint32_t precision,\n uint32_t options) {\n size_t n = (h->negative ||\n (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN))\n ? 1\n : 0;\n if (h->decimal_point <= 0) {\n n += 1;\n } else {\n n += (size_t)(h->decimal_point);\n }\n if (precision > 0) {\n n += precision + 1; // +1 for the '.'.\n }\n\n // Don't modify dst if the formatted number won't fit.\n if (n > dst.len) {\n return 0;\n }\n\n // Align-left or align-right.\n uint8_t* ptr = (options & WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT)\n ? &dst.ptr[dst.len - n]\n : &dst.ptr[0];\n\n // Leading \"±\".\n if (h->negative) {\n *ptr++ = '-';\n } else if (options & WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN) {\n *ptr++ = '+';\n }\n\n // Integral digits.\n if (h->decimal_point <= 0) {\n *ptr++ = '0';\n } else {\n uint32_t m =\n" +
|
||||
@@ -507,21 +507,22 @@ const BaseStrConvPrivateH = "" +
|
||||
""
|
||||
|
||||
const BaseStrConvPublicH = "" +
|
||||
"// ---------------- String Conversions\n\n// Options (bitwise or'ed together) for wuffs_base__parse_number_xxx\n// functions. The XXX options apply to both integer and floating point. The FXX\n// options apply only to floating point.\n\n#define WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n" +
|
||||
"// ---------------- String Conversions\n\n// Options (bitwise or'ed together) for wuffs_base__parse_number_xxx\n// functions. The XXX options apply to both integer and floating point. The FXX\n// options apply only to floating point.\n\n#define WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES means to accept\n// inputs like \"00\", \"0644\" and \"00.7\". By default, they are rejected.\n#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES \\\n ((uint32_t)0x00000001)\n\n// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES means to accept inputs like\n// \"1__2\" and \"_3.141_592\". By default, they are rejected.\n#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES ((uint32_t)0x00000002)\n\n// WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to accept\n// \"1,5\" and not \"1.5\" as one-and-a-half.\n//\n// If the caller wants to accept either, it is responsible for canonicalizing\n// the input before calling wuffs_base__parse_numb" +
|
||||
"er_fxx. The caller also has\n// more context on e.g. exactly how to treat something like \"$1,234\".\n#define WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA \\\n ((uint32_t)0x00000010)\n\n// WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN means to reject inputs that\n// would lead to infinite or Not-a-Number floating point values. By default,\n// they are accepted.\n//\n// This affects the literal \"inf\" as input, but also affects inputs like\n// \"1e999\" that would overflow double-precision floating point.\n#define WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN ((uint32_t)0x00000020)\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// Options (bitwise or'ed together) for wuffs_base__render_number_xxx\n// functions. The XXX options apply to both integer and floating point. The FXX\n// options apply only to floating point.\n\n#define WUFFS_BASE__RENDER_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT means to render to the right side\n// (higher indexes) of the destination slice, leaving any untouched bytes on\n// the left side (lower indexes). The default is vice versa: rendering on the\n// left with slack on the right.\n#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000010)\n\n// WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN means to render the leading\n// \"+\" for non-negative numbers: \"+0\" and \"+12.3\" instead of \"0\" and \"12.3\".\n#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000020)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to render\n// one-and-a-half as \"1,5\" instead of \"1.5\".\n#define WUFFS_BASE__RENDER_NUMBER_F" +
|
||||
"XX__DECIMAL_SEPARATOR_IS_A_COMMA \\\n ((uint32_t)0x00000100)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ETC means whether to never\n// (EXPONENT_ABSENT, equivalent to printf's \"%f\") or to always\n// (EXPONENT_PRESENT, equivalent to printf's \"%e\") render a floating point\n// number as \"1.23e+05\" instead of \"123000\".\n//\n// Having both bits set is the same has having neither bit set, where the\n// notation used depends on whether the exponent is sufficiently large: \"0.5\"\n// is preferred over \"5e-01\" but \"5e-09\" is preferred over \"0.000000005\".\n#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00000200)\n#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00000400)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION means to render the\n// smallest number of digits so that parsing the resultant string will recover\n// the same double-precision floating point number.\n//\n// For example, double-precision cannot distinguish between 0.3 and\n// 0.2999999999999999888977697537484345957636" +
|
||||
"83319091796875, so when this bit\n// is set, rendering the latter will produce \"0.3\" but rendering\n// 0.3000000000000000444089209850062616169452667236328125 will produce\n// \"0.30000000000000004\".\n#define WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION \\\n ((uint32_t)0x00000800)\n\n" +
|
||||
"// --------\n\n// Options (bitwise or'ed together) for wuffs_base__render_number_xxx\n// functions. The XXX options apply to both integer and floating point. The FXX\n// options apply only to floating point.\n\n#define WUFFS_BASE__RENDER_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT means to render to the right side\n// (higher indexes) of the destination slice, leaving any untouched bytes on\n// the left side (lower indexes). The default is vice versa: rendering on the\n// left with slack on the right.\n#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000100)\n\n// WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN means to render the leading\n// \"+\" for non-negative numbers: \"+0\" and \"+12.3\" instead of \"0\" and \"12.3\".\n#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000200)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to render\n// one-and-a-half as \"1,5\" instead of \"1.5\".\n#define WUFFS_BASE__RENDER_NUMBER_F" +
|
||||
"XX__DECIMAL_SEPARATOR_IS_A_COMMA \\\n ((uint32_t)0x00001000)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ETC means whether to never\n// (EXPONENT_ABSENT, equivalent to printf's \"%f\") or to always\n// (EXPONENT_PRESENT, equivalent to printf's \"%e\") render a floating point\n// number as \"1.23e+05\" instead of \"123000\".\n//\n// Having both bits set is the same has having neither bit set, where the\n// notation used depends on whether the exponent is sufficiently large: \"0.5\"\n// is preferred over \"5e-01\" but \"5e-09\" is preferred over \"0.000000005\".\n#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00002000)\n#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00004000)\n\n// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION means to render the\n// smallest number of digits so that parsing the resultant string will recover\n// the same double-precision floating point number.\n//\n// For example, double-precision cannot distinguish between 0.3 and\n// 0.2999999999999999888977697537484345957636" +
|
||||
"83319091796875, so when this bit\n// is set, rendering the latter will produce \"0.3\" but rendering\n// 0.3000000000000000444089209850062616169452667236328125 will produce\n// \"0.30000000000000004\".\n#define WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION \\\n ((uint32_t)0x00008000)\n\n" +
|
||||
"" +
|
||||
"// ---------------- IEEE 754 Floating Point\n\n// wuffs_base__parse_number_f64 parses the floating point number in s. For\n// example, if s contains the bytes \"1.5\" then it will return the double 1.5.\n//\n// It returns an error if s does not contain a floating point number.\n//\n// It does not necessarily return an error if the conversion is lossy, e.g. if\n// s is \"0.3\", which double-precision floating point cannot represent exactly.\n//\n// Similarly, the returned value may be infinite (and no error returned) even\n// if s was not \"inf\", when the input is nominally finite but sufficiently\n// larger than DBL_MAX, about 1.8e+308.\n//\n// It is similar to the C standard library's strtod function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whi" +
|
||||
"tespace, leading or otherwise.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"00.7\" are invalid.\n// - It is not affected by i18n / l10n settings such as environment variables.\n// - Conversely, it always accepts either ',' or '.' as a decimal separator.\n// In particular, \"3,141,592\" is always invalid but \"3,141\" is always valid\n// (and approximately π). The caller is responsible for e.g. previously\n// rejecting or filtering s if it contains a comma, if that is unacceptable\n// to the caller. For example, JSON numbers always use a dot '.' and never a\n// comma ',', regardless of the LOCALE environment variable.\n// - It does allow arbitrary underscores. For example, \"_3.141_592\" would\n// successfully parse, again approximately π.\n// - It does allow \"inf\", \"+Infinity\" and \"-NAN\", case insensitive, but it\n// does not permit \"nan\" to be followed by an integer mantissa.\n// - It does not allow hexadecimal float" +
|
||||
"ing point numbers.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options);\n\n" +
|
||||
"tespace, leading or otherwise.\n// - It does not allow hexadecimal floating point numbers.\n// - It is not affected by i18n / l10n settings such as environment variables.\n//\n// The options argument can change these, but by default, it:\n// - Allows \"inf\", \"+Infinity\" and \"-NAN\", case insensitive. Similarly,\n// without an explicit opt-out, it would successfully parse \"1e999\" as\n// infinity, even though it overflows double-precision floating point.\n// - Rejects underscores. With an explicit opt-in, \"_3.141_592\" would\n// successfully parse as an approximation to π.\n// - Rejects unnecessary leading zeroes: \"00\", \"0644\" and \"00.7\".\n// - Uses a dot '1.5' instead of a comma '1,5' for the decimal separator.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //\nwuffs_base__parse_number_f64(wuffs_base__slice_" +
|
||||
"u8 s, uint32_t options);\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// wuffs_base__ieee_754_bit_representation__etc converts between a double\n// precision numerical value and its IEEE 754 64-bit representation (1 sign\n// bit, 11 exponent bits, 52 explicit significand bits).\n//\n// For example, it converts between:\n// - +1.0 and 0x3FF0_0000_0000_0000.\n// - +5.5 and 0x4016_0000_0000_0000.\n// - -inf and 0xFFF0_0000_0000_0000.\n//\n// See https://en.wikipedia.org/wiki/Double-precision_floating-point_format\n\nstatic inline uint64_t //\nwuffs_base__ieee_754_bit_representation__from_f64(double f) {\n uint64_t u = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&u, &f, sizeof(uint64_t));\n }\n return u;\n}\n\nstatic inline double //\nwuffs_base__ieee_754_bit_representation__to_f64(uint64_t u) {\n double f = 0;\n if (sizeof(uint64_t) == sizeof(double)) {\n memcpy(&f, &u, sizeof(uint64_t));\n }\n return f;\n}\n\n" +
|
||||
"" +
|
||||
"// ---------------- Integer\n\n// wuffs_base__parse_number_i64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"-123\" then it will return the int64_t -123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow an int64_t.\n//\n// It is similar to wuffs_base__parse_number_u64 but it returns a signed\n// integer, not an unsigned integer. It also allows a leading '+' or '-'.\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_i64 //\nwuffs_base__parse_number_i64(wuffs_base__slice_u8 s, uint32_t options);\n\n// wuffs_base__parse_number_u64 parses the ASCII integer in s. For example, if\n// s contains the bytes \"123\" then it will return the uint64_t 123.\n//\n// It returns an error if s does not contain an integer or if the integer\n// within would overflow a uint64" +
|
||||
"_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not allow unnecessary leading zeroes (\"0\" is valid and its sole\n// zero is necessary). All of \"00\", \"0644\" and \"007\" are invalid.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not" +
|
||||
" affected by i18n / l10n settings such as environment variables.\n// - It does allow arbitrary underscores, except inside the optional 2-byte\n// opening \"0d\" or \"0X\" that denotes base-10 or base-16. For example,\n// \"__0D_1_002\" would successfully parse as \"one thousand and two\".\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options);\n\n" +
|
||||
"_t.\n//\n// It is similar to the C standard library's strtoull function, but:\n// - Errors are returned in-band (in a result type), not out-of-band (errno).\n// - It takes a slice (a pointer and length), not a NUL-terminated C string.\n// - It does not take an optional endptr argument. It does not allow a partial\n// parse: it returns an error unless all of s is consumed.\n// - It does not allow whitespace, leading or otherwise.\n// - It does not allow a leading '+' or '-'.\n// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it\n// always accepts both decimal (e.g \"1234\", \"0d5678\") and hexadecimal (e.g.\n// \"0x9aBC\"). The caller is responsible for prior filtering of e.g. hex\n// numbers if they are unwanted. For example, Wuffs' JSON decoder will only\n// produce a wuffs_base__token for decimal numbers, not hexadecimal.\n// - It is not affected by i18n / l10n settings such as environment variables.\n//\n// The options argument can change these, but by default, it:\n// - Rejects und" +
|
||||
"erscores. With an explicit opt-in, \"__0D_1_002\" would\n// successfully parse as \"one thousand and two\". Underscores are still\n// rejected inside the optional 2-byte opening \"0d\" or \"0X\" that denotes\n// base-10 or base-16.\n// - Rejects unnecessary leading zeroes: \"00\" and \"0644\".\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC wuffs_base__result_u64 //\nwuffs_base__parse_number_u64(wuffs_base__slice_u8 s, uint32_t options);\n\n" +
|
||||
"" +
|
||||
"// --------\n\n#define WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL 20\n#define WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL 21\n\n// wuffs_base__render_number_f64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns 0 (and no bytes are written).\n//\n// For those familiar with C's printf or Go's fmt.Printf functions:\n// - \"%e\" means the WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT option.\n// - \"%f\" means the WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT option.\n// - \"%g\" means neither or both bits are set.\n//\n// The precision argument controls the number of digits rendered, excluding the\n// exponent (the \"e+05\" in \"1.23e+05\"):\n// - for \"%e\" and \"%f\" it is the number of digits after the decimal separator,\n// - for \"%g\" it is the number of significant digits (and trailing zeroes are\n// removed).\n//\n// A precision of 6 gives similar output to printf's defaults.\n//\n// A precision greater than 4095 is equivalent to 4095.\n//\n// The " +
|
||||
"precision argument is ignored when the\n// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION option is set. This is\n// similar to Go's strconv.FormatFloat with a negative (i.e. non-sensical)\n// precision, but there is no corresponding feature in C's printf.\n//\n// Extreme values of x will be rendered as \"NaN\", \"Inf\" (or \"+Inf\" if the\n// WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN option is set) or \"-Inf\".\n//\n// For modular builds that divide the base module into sub-modules, using this\n// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not\n// just WUFFS_CONFIG__MODULE__BASE__CORE.\nWUFFS_BASE__MAYBE_STATIC size_t //\nwuffs_base__render_number_f64(wuffs_base__slice_u8 dst,\n double x,\n uint32_t precision,\n uint32_t options);\n\n// wuffs_base__render_number_i64 writes the decimal encoding of x to dst and\n// returns the number of bytes written. If dst is shorter than the entire\n// encoding, it returns" +
|
||||
|
||||
@@ -3941,6 +3941,32 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice(
|
||||
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS ((uint32_t)0x00000000)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES means to accept
|
||||
// inputs like "00", "0644" and "00.7". By default, they are rejected.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_MULTIPLE_LEADING_ZEROES \
|
||||
((uint32_t)0x00000001)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES means to accept inputs like
|
||||
// "1__2" and "_3.141_592". By default, they are rejected.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_XXX__ALLOW_UNDERSCORES ((uint32_t)0x00000002)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to accept
|
||||
// "1,5" and not "1.5" as one-and-a-half.
|
||||
//
|
||||
// If the caller wants to accept either, it is responsible for canonicalizing
|
||||
// the input before calling wuffs_base__parse_number_fxx. The caller also has
|
||||
// more context on e.g. exactly how to treat something like "$1,234".
|
||||
#define WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA \
|
||||
((uint32_t)0x00000010)
|
||||
|
||||
// WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN means to reject inputs that
|
||||
// would lead to infinite or Not-a-Number floating point values. By default,
|
||||
// they are accepted.
|
||||
//
|
||||
// This affects the literal "inf" as input, but also affects inputs like
|
||||
// "1e999" that would overflow double-precision floating point.
|
||||
#define WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN ((uint32_t)0x00000020)
|
||||
|
||||
// --------
|
||||
|
||||
// Options (bitwise or'ed together) for wuffs_base__render_number_xxx
|
||||
@@ -3953,16 +3979,16 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice(
|
||||
// (higher indexes) of the destination slice, leaving any untouched bytes on
|
||||
// the left side (lower indexes). The default is vice versa: rendering on the
|
||||
// left with slack on the right.
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000010)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__ALIGN_RIGHT ((uint32_t)0x00000100)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN means to render the leading
|
||||
// "+" for non-negative numbers: "+0" and "+12.3" instead of "0" and "12.3".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000020)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_XXX__LEADING_PLUS_SIGN ((uint32_t)0x00000200)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA means to render
|
||||
// one-and-a-half as "1,5" instead of "1.5".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA \
|
||||
((uint32_t)0x00000100)
|
||||
((uint32_t)0x00001000)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ETC means whether to never
|
||||
// (EXPONENT_ABSENT, equivalent to printf's "%f") or to always
|
||||
@@ -3972,8 +3998,8 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice(
|
||||
// Having both bits set is the same has having neither bit set, where the
|
||||
// notation used depends on whether the exponent is sufficiently large: "0.5"
|
||||
// is preferred over "5e-01" but "5e-09" is preferred over "0.000000005".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00000200)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00000400)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT ((uint32_t)0x00002000)
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT ((uint32_t)0x00004000)
|
||||
|
||||
// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION means to render the
|
||||
// smallest number of digits so that parsing the resultant string will recover
|
||||
@@ -3985,7 +4011,7 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice(
|
||||
// 0.3000000000000000444089209850062616169452667236328125 will produce
|
||||
// "0.30000000000000004".
|
||||
#define WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION \
|
||||
((uint32_t)0x00000800)
|
||||
((uint32_t)0x00008000)
|
||||
|
||||
// ---------------- IEEE 754 Floating Point
|
||||
|
||||
@@ -4007,20 +4033,17 @@ wuffs_base__pixel_swizzler::swizzle_interleaved_from_slice(
|
||||
// - It does not take an optional endptr argument. It does not allow a partial
|
||||
// parse: it returns an error unless all of s is consumed.
|
||||
// - It does not allow whitespace, leading or otherwise.
|
||||
// - It does not allow unnecessary leading zeroes ("0" is valid and its sole
|
||||
// zero is necessary). All of "00", "0644" and "00.7" are invalid.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
// - Conversely, it always accepts either ',' or '.' as a decimal separator.
|
||||
// In particular, "3,141,592" is always invalid but "3,141" is always valid
|
||||
// (and approximately π). The caller is responsible for e.g. previously
|
||||
// rejecting or filtering s if it contains a comma, if that is unacceptable
|
||||
// to the caller. For example, JSON numbers always use a dot '.' and never a
|
||||
// comma ',', regardless of the LOCALE environment variable.
|
||||
// - It does allow arbitrary underscores. For example, "_3.141_592" would
|
||||
// successfully parse, again approximately π.
|
||||
// - It does allow "inf", "+Infinity" and "-NAN", case insensitive, but it
|
||||
// does not permit "nan" to be followed by an integer mantissa.
|
||||
// - It does not allow hexadecimal floating point numbers.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
//
|
||||
// The options argument can change these, but by default, it:
|
||||
// - Allows "inf", "+Infinity" and "-NAN", case insensitive. Similarly,
|
||||
// without an explicit opt-out, it would successfully parse "1e999" as
|
||||
// infinity, even though it overflows double-precision floating point.
|
||||
// - Rejects underscores. With an explicit opt-in, "_3.141_592" would
|
||||
// successfully parse as an approximation to π.
|
||||
// - Rejects unnecessary leading zeroes: "00", "0644" and "00.7".
|
||||
// - Uses a dot '1.5' instead of a comma '1,5' for the decimal separator.
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__FLOATCONV sub-module, not
|
||||
@@ -4089,17 +4112,19 @@ wuffs_base__parse_number_i64(wuffs_base__slice_u8 s, uint32_t options);
|
||||
// parse: it returns an error unless all of s is consumed.
|
||||
// - It does not allow whitespace, leading or otherwise.
|
||||
// - It does not allow a leading '+' or '-'.
|
||||
// - It does not allow unnecessary leading zeroes ("0" is valid and its sole
|
||||
// zero is necessary). All of "00", "0644" and "007" are invalid.
|
||||
// - It does not take a base argument (e.g. base 10 vs base 16). Instead, it
|
||||
// always accepts both decimal (e.g "1234", "0d5678") and hexadecimal (e.g.
|
||||
// "0x9aBC"). The caller is responsible for prior filtering of e.g. hex
|
||||
// numbers if they are unwanted. For example, Wuffs' JSON decoder will only
|
||||
// produce a wuffs_base__token for decimal numbers, not hexadecimal.
|
||||
// - It is not affected by i18n / l10n settings such as environment variables.
|
||||
// - It does allow arbitrary underscores, except inside the optional 2-byte
|
||||
// opening "0d" or "0X" that denotes base-10 or base-16. For example,
|
||||
// "__0D_1_002" would successfully parse as "one thousand and two".
|
||||
//
|
||||
// The options argument can change these, but by default, it:
|
||||
// - Rejects underscores. With an explicit opt-in, "__0D_1_002" would
|
||||
// successfully parse as "one thousand and two". Underscores are still
|
||||
// rejected inside the optional 2-byte opening "0d" or "0X" that denotes
|
||||
// base-10 or base-16.
|
||||
// - Rejects unnecessary leading zeroes: "00" and "0644".
|
||||
//
|
||||
// For modular builds that divide the base module into sub-modules, using this
|
||||
// function requires the WUFFS_CONFIG__MODULE__BASE__INTCONV sub-module, not
|
||||
@@ -9990,7 +10015,8 @@ wuffs_base__private_implementation__high_prec_dec__assign(
|
||||
static wuffs_base__status //
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
wuffs_base__private_implementation__high_prec_dec* h,
|
||||
wuffs_base__slice_u8 s) {
|
||||
wuffs_base__slice_u8 s,
|
||||
uint32_t options) {
|
||||
if (!h) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
@@ -10043,7 +10069,11 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
for (;; p++) {
|
||||
if (p >= q) {
|
||||
goto after_all;
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p ==
|
||||
((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
goto after_sep;
|
||||
} else if ((*p == 'E') || (*p == 'e')) {
|
||||
@@ -10069,7 +10099,11 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
// Long-tail non-zeroes set the truncated bit.
|
||||
h->truncated = true;
|
||||
}
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p ==
|
||||
((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
goto after_sep;
|
||||
} else if ((*p == 'E') || (*p == 'e')) {
|
||||
@@ -10080,7 +10114,10 @@ wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
}
|
||||
}
|
||||
|
||||
} else if ((*p == '.') || (*p == ',')) {
|
||||
} else if (*p == ((options &
|
||||
WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
no_digits_before_separator = true;
|
||||
|
||||
@@ -10819,8 +10856,9 @@ wuffs_base__private_implementation__parse_number_f64_eisel(uint64_t man,
|
||||
// --------
|
||||
|
||||
static wuffs_base__result_f64 //
|
||||
wuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s,
|
||||
const char* fallback_status_repr) {
|
||||
wuffs_base__private_implementation__parse_number_f64_special(
|
||||
wuffs_base__slice_u8 s,
|
||||
const char* fallback_status_repr) {
|
||||
do {
|
||||
uint8_t* p = s.ptr;
|
||||
uint8_t* q = s.ptr + s.len;
|
||||
@@ -10920,7 +10958,7 @@ fallback:
|
||||
}
|
||||
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
|
||||
wuffs_base__private_implementation__parse_number_f64__fallback(
|
||||
wuffs_base__private_implementation__high_prec_dec__to_f64(
|
||||
wuffs_base__private_implementation__high_prec_dec* h) {
|
||||
do {
|
||||
// powers converts decimal powers of 10 to binary powers of 2. For example,
|
||||
@@ -11156,7 +11194,10 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
|
||||
// Walk the "d"s after the optional decimal separator ('.' or ','),
|
||||
// updating the man and exp10 variables.
|
||||
int32_t exp10 = 0;
|
||||
if ((*p == '.') || (*p == ',')) {
|
||||
if (*p ==
|
||||
((options & WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA)
|
||||
? ','
|
||||
: '.')) {
|
||||
p++;
|
||||
const uint8_t* first_after_separator_ptr = p;
|
||||
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
|
||||
@@ -11295,11 +11336,13 @@ fallback:
|
||||
do {
|
||||
wuffs_base__private_implementation__high_prec_dec h;
|
||||
wuffs_base__status status =
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(&h, s);
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(&h, s,
|
||||
options);
|
||||
if (status.repr) {
|
||||
return wuffs_base__parse_number_f64_special(s, status.repr);
|
||||
return wuffs_base__private_implementation__parse_number_f64_special(
|
||||
s, status.repr);
|
||||
}
|
||||
return wuffs_base__private_implementation__parse_number_f64__fallback(&h);
|
||||
return wuffs_base__private_implementation__high_prec_dec__to_f64(&h);
|
||||
} while (0);
|
||||
}
|
||||
|
||||
|
||||
+48
-14
@@ -346,11 +346,12 @@ test_wuffs_strconv_hpd_rounded_integer() {
|
||||
int tc;
|
||||
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
|
||||
wuffs_base__private_implementation__high_prec_dec hpd;
|
||||
CHECK_STATUS(
|
||||
"hpd__parse",
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
&hpd, wuffs_base__make_slice_u8((void*)test_cases[tc].str,
|
||||
strlen(test_cases[tc].str))));
|
||||
CHECK_STATUS("hpd__parse",
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
&hpd,
|
||||
wuffs_base__make_slice_u8((void*)test_cases[tc].str,
|
||||
strlen(test_cases[tc].str)),
|
||||
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
|
||||
uint64_t have =
|
||||
wuffs_base__private_implementation__high_prec_dec__rounded_integer(
|
||||
&hpd);
|
||||
@@ -359,6 +360,42 @@ test_wuffs_strconv_hpd_rounded_integer() {
|
||||
have, test_cases[tc].want);
|
||||
}
|
||||
}
|
||||
|
||||
// Test WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA.
|
||||
{
|
||||
int o;
|
||||
for (o = 0; o < 2; o++) {
|
||||
wuffs_base__private_implementation__high_prec_dec hpd;
|
||||
const char* str = "1,75";
|
||||
wuffs_base__status status =
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
&hpd, wuffs_base__make_slice_u8((void*)str, strlen(str)),
|
||||
(o ? WUFFS_BASE__PARSE_NUMBER_FXX__DECIMAL_SEPARATOR_IS_A_COMMA
|
||||
: WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
|
||||
|
||||
if (o == 0) {
|
||||
if (status.repr != wuffs_base__error__bad_argument) {
|
||||
RETURN_FAIL(
|
||||
"DECIMAL_SEPARATOR_IS_A_COMMA off: have \"%s\", want \"%s\"",
|
||||
status.repr, wuffs_base__error__bad_argument);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
CHECK_STATUS("DECIMAL_SEPARATOR_IS_A_COMMA on", status);
|
||||
wuffs_base__result_f64 r =
|
||||
wuffs_base__private_implementation__high_prec_dec__to_f64(&hpd);
|
||||
uint64_t have =
|
||||
wuffs_base__ieee_754_bit_representation__from_f64(r.value);
|
||||
uint64_t want = 0x3FFC000000000000;
|
||||
if (have != want) {
|
||||
RETURN_FAIL("DECIMAL_SEPARATOR_IS_A_COMMA on: have 0x%016" PRIX64
|
||||
", want 0x%016" PRIX64,
|
||||
have, want);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -391,11 +428,12 @@ test_wuffs_strconv_hpd_shift() {
|
||||
int tc;
|
||||
for (tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
|
||||
wuffs_base__private_implementation__high_prec_dec hpd;
|
||||
CHECK_STATUS(
|
||||
"hpd__parse",
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
&hpd, wuffs_base__make_slice_u8((void*)test_cases[tc].str,
|
||||
strlen(test_cases[tc].str))));
|
||||
CHECK_STATUS("hpd__parse",
|
||||
wuffs_base__private_implementation__high_prec_dec__parse(
|
||||
&hpd,
|
||||
wuffs_base__make_slice_u8((void*)test_cases[tc].str,
|
||||
strlen(test_cases[tc].str)),
|
||||
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
|
||||
int32_t shift = test_cases[tc].shift;
|
||||
if (shift > 0) {
|
||||
wuffs_base__private_implementation__high_prec_dec__small_rshift(
|
||||
@@ -673,10 +711,6 @@ test_wuffs_strconv_parse_number_f64() {
|
||||
// If adding new cases, consider updating u64TestCases in
|
||||
// script/print-render-number-f64-tests.go
|
||||
|
||||
// We accept either ',' or '.'.
|
||||
{.want = 0x3FFC000000000000, .str = "1,75"},
|
||||
{.want = 0x3FFC000000000000, .str = "1.75"},
|
||||
|
||||
{.want = fail, .str = " 0"},
|
||||
{.want = fail, .str = ""},
|
||||
{.want = fail, .str = "."},
|
||||
|
||||
Reference in new issue
Block a user