mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 00:11:14 +01:00
[number] Fix two more edge cases (#6529)
* [number] Minor rename * [number] Make zero mantissa with large exponent to return 0, not nan * [number] More percise keeping track of fraction
This commit is contained in:
1 parent
07bc8effbb
commit
7fde83a7e0
4 files changed
+70
-12
No files matched your search
@@ -105,7 +105,7 @@ struct dict_opset_t : opset_t<number_t>
|
||||
if (unlikely (nibble == RESERVED)) break;
|
||||
else if (nibble == END)
|
||||
{
|
||||
size_t length = parser.finish ();
|
||||
size_t length = parser.commit ();
|
||||
if (unlikely (length == 0 || length != count))
|
||||
break;
|
||||
return parser.get_value ();
|
||||
|
||||
+14
-10
@@ -103,11 +103,10 @@ struct hb_number_parser_t
|
||||
|
||||
/* Signal end of input. Rolls back a dangling exponent. Returns the
|
||||
* accepted length, or 0 if no valid number was parsed. */
|
||||
size_t finish ()
|
||||
HB_NODISCARD size_t commit ()
|
||||
{
|
||||
if (state == ExpStart || state == ExpSign) rollback_exponent ();
|
||||
return (state == Sign || state == AfterDotFromStart ||
|
||||
state == ExpStart || state == ExpSign) ? 0 : pos;
|
||||
return (state == Sign || state == AfterDotFromStart) ? 0 : pos;
|
||||
}
|
||||
|
||||
/* Valid after finish() returned true. */
|
||||
@@ -117,9 +116,13 @@ struct hb_number_parser_t
|
||||
if (!fraction.is_empty ()) v += fraction.get_value ();
|
||||
if (neg) v *= -1.;
|
||||
|
||||
/* Zero mantissa: any exponent gives zero, and this avoids
|
||||
* 0 * HUGE_VAL = nan for "0e441" where _pow10(441) is HUGE_VAL
|
||||
* but exp_overflow didn't fire. Preserves -0.0 for "-0e<...>". */
|
||||
if (v == 0) return v;
|
||||
|
||||
if (unlikely (exp_overflow))
|
||||
{
|
||||
if (v == 0) return v; /* preserves -0.0 for "-0e<overflow>" */
|
||||
if (exp_neg) return neg ? -.0 : .0;
|
||||
else return neg ? -HUGE_VAL : HUGE_VAL;
|
||||
}
|
||||
@@ -144,14 +147,14 @@ struct hb_number_parser_t
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr uint64_t MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */
|
||||
static constexpr uint64_t MAX_FRACT = 10000000000000000000ull; // 10^19
|
||||
struct /* fraction */
|
||||
{
|
||||
void add (int digit)
|
||||
{
|
||||
if (likely (fraction <= MAX_FRACT / 10))
|
||||
{
|
||||
fraction = fraction * 10. + digit;
|
||||
fraction = fraction * 10 + digit;
|
||||
++count;
|
||||
}
|
||||
else if (drop < 0) drop = digit;
|
||||
@@ -160,9 +163,10 @@ private:
|
||||
|
||||
double get_value () const
|
||||
{
|
||||
double result = fraction;
|
||||
/* Round-to-nearest-even using the recorded tail */
|
||||
if (drop > 5 || (drop == 5 && ((((uint64_t) fraction) & 1) || sticky)))
|
||||
/* (double) fraction rounds to nearest; division by an exact
|
||||
* power of ten rounds once more. Combined error < 1 ULP. */
|
||||
double result = (double) fraction;
|
||||
if (drop > 5 || (drop == 5 && (sticky || (fraction & 1))))
|
||||
result += 1;
|
||||
return result / _pow10 (count);
|
||||
}
|
||||
@@ -171,7 +175,7 @@ private:
|
||||
|
||||
private:
|
||||
unsigned count = 0;
|
||||
double fraction = 0;
|
||||
uint64_t fraction = 0;
|
||||
int drop = -1; /* drop is the first digit we couldn't accumulate */
|
||||
bool sticky = false; /* sticky is set when any later digit was nonzero. */
|
||||
} fraction;
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ hb_parse_double (const char **pp, const char *end, double *pv, bool whole_buffer
|
||||
hb_number_parser_t parser;
|
||||
while (p < end && parser.consume (*p)) p++;
|
||||
|
||||
size_t length = parser.finish ();
|
||||
size_t length = parser.commit ();
|
||||
if (unlikely (length == 0)) return false;
|
||||
const char *pend = num_start + length;
|
||||
if (unlikely (whole_buffer && pend != end)) return false;
|
||||
|
||||
@@ -1905,6 +1905,60 @@ test_parse_double (void)
|
||||
hb_always_assert (pv == strtod (str, nullptr));
|
||||
hb_always_assert (pp == end);
|
||||
}
|
||||
/* Zero mantissa, exponent in (308, MAX_EXP]: _pow10 returns HUGE_VAL
|
||||
* but exp_overflow doesn't fire, so 0 * inf must short-circuit to 0
|
||||
* rather than producing nan. */
|
||||
{
|
||||
const char str[] = "0e441";
|
||||
const char *pp = str;
|
||||
const char *end = str + ARRAY_LENGTH (str) - 1;
|
||||
double pv;
|
||||
hb_always_assert (hb_parse_double (&pp, end, &pv));
|
||||
hb_always_assert (pv == 0.0 && !std::signbit (pv));
|
||||
hb_always_assert (pv == strtod (str, nullptr));
|
||||
}
|
||||
{
|
||||
const char str[] = "-0e441";
|
||||
const char *pp = str;
|
||||
const char *end = str + ARRAY_LENGTH (str) - 1;
|
||||
double pv;
|
||||
hb_always_assert (hb_parse_double (&pp, end, &pv));
|
||||
hb_always_assert (pv == 0.0 && std::signbit (pv));
|
||||
hb_always_assert (pv == strtod (str, nullptr));
|
||||
hb_always_assert (std::signbit (pv) == std::signbit (strtod (str, nullptr)));
|
||||
}
|
||||
/* Same bug, alternate zero-mantissa forms. */
|
||||
{
|
||||
const char str[] = ".0e441";
|
||||
const char *pp = str;
|
||||
const char *end = str + ARRAY_LENGTH (str) - 1;
|
||||
double pv;
|
||||
hb_always_assert (hb_parse_double (&pp, end, &pv));
|
||||
hb_always_assert (pv == 0.0 && !std::signbit (pv));
|
||||
hb_always_assert (pv == strtod (str, nullptr));
|
||||
}
|
||||
{
|
||||
const char str[] = "0.e441";
|
||||
const char *pp = str;
|
||||
const char *end = str + ARRAY_LENGTH (str) - 1;
|
||||
double pv;
|
||||
hb_always_assert (hb_parse_double (&pp, end, &pv));
|
||||
hb_always_assert (pv == 0.0 && !std::signbit (pv));
|
||||
hb_always_assert (pv == strtod (str, nullptr));
|
||||
}
|
||||
/* 18 fractional digits: accumulator keeps 15, so we can be ~3 ULP
|
||||
* off from strtod. Known limit; would tighten to ~1 ULP with a
|
||||
* uint64_t accumulator. */
|
||||
{
|
||||
const char str[] = ".543588148486817512e";
|
||||
const char *pp = str;
|
||||
const char *end = str + ARRAY_LENGTH (str) - 1;
|
||||
double pv;
|
||||
hb_always_assert (hb_parse_double (&pp, end, &pv));
|
||||
hb_always_assert (pv > 0.543588148486817 && pv < 0.543588148486818);
|
||||
hb_always_assert (almost_equal (pv, strtod (str, nullptr)));
|
||||
hb_always_assert (pp == str + 19); // trailing 'e' not consumed
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
Reference in new issue
Block a user