[number] Deduplicate the logic around fraction (#6352)

* [number] Don't mutate input pointer on failure

Two last tests in test_parse_double used to fail but now don't.

* [number] Deduplicate the logic around fraction

Forming a new abstraction, _hb_fraction, just to consume the fraction part.
This commit is contained in:
ebraminio authored and GitHub committed 2026-10-10 12:05:00 -06:00
1 parent fd0c6c9a69
commit 8aa60f12a9
3 files changed
+268 -77

No files matched your search

+57 -70
View File
@@ -28,7 +28,7 @@
#include "hb.hh"
// Works only for n < 512
// Handles 0..308; anything larger overflows double and returns HUGE_VAL.
static inline double
_pow10 (unsigned exponent)
{
@@ -54,6 +54,38 @@ _pow10 (unsigned exponent)
return result;
}
struct _hb_fraction
{
void add (int digit)
{
if (likely (fraction <= MAX_FRACT / 10))
{
fraction = fraction * 10. + digit;
++count;
}
else if (drop < 0) drop = digit;
else if (digit != 0) sticky = true;
}
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)))
result += 1;
return result / _pow10 (count);
}
bool is_empty () const { return !count; }
private:
unsigned count = 0;
double 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.
static constexpr uint64_t MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */
};
// A variant of strtod that also gets end of buffer in its second argument.
//
// Grammar (with backtracking on partial exponents):
@@ -70,16 +102,6 @@ _pow10 (unsigned exponent)
static inline double
hb_strtod (const char *p, const char **end_ptr /* IN/OUT */)
{
double value = 0;
double frac = 0;
double frac_count = 0;
unsigned exp = 0;
bool neg = false, exp_neg = false, exp_overflow = false;
int frac_drop = -1;
bool frac_sticky = false;
const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */
const unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */
const char *p_original = p;
const char *pe = *end_ptr;
@@ -88,88 +110,60 @@ hb_strtod (const char *p, const char **end_ptr /* IN/OUT */)
p++;
// Sign
bool neg = false;
if (p < pe && (*p == '+' || *p == '-'))
{
neg = (*p == '-');
p++;
}
// Mantissa.
const char *mantissa_end = nullptr;
double value = 0;
if (p < pe && '0' <= *p && *p <= '9')
// Integer and fraction
{
// Integer part
do
const char *int_start = p;
while (p < pe && '0' <= *p && *p <= '9')
{
value = value * 10. + (*p - '0');
p++;
} while (p < pe && '0' <= *p && *p <= '9');
mantissa_end = p;
}
bool has_int = p > int_start;
// Optional fractional part; a trailing '.' with no digits is accepted
_hb_fraction frac;
if (p < pe && *p == '.')
{
p++;
while (p < pe && '0' <= *p && *p <= '9')
{
if (likely (frac <= MAX_FRACT / 10))
{
frac = frac * 10. + (*p - '0');
++frac_count;
}
// Record the tail we couldn't fit for round-to-nearest below
else if (frac_drop < 0) frac_drop = *p - '0';
else if (*p != '0') frac_sticky = true;
frac.add (*p - '0');
p++;
}
mantissa_end = p;
}
}
else if (p < pe && *p == '.')
{
// Fractional-only form: '.' must be followed by at least one digit
p++;
if (p < pe && '0' <= *p && *p <= '9')
{
do
{
if (likely (frac <= MAX_FRACT / 10))
{
frac = frac * 10. + (*p - '0');
++frac_count;
}
else if (frac_drop < 0) frac_drop = *p - '0';
else if (*p != '0') frac_sticky = true;
p++;
} while (p < pe && '0' <= *p && *p <= '9');
mantissa_end = p;
}
else
if (!frac.is_empty ()) value += frac.get_value ();
else if (!has_int)
{
// Nothing could be parsed
*end_ptr = p_original;
return 0.0;
return .0;
}
}
else
{
// No digits: no subject sequence
*end_ptr = p_original;
return 0.0;
}
// Exponent. Backtracking is implicit: end_ptr follows mantissa_end,
// which is only updated when the exponent is well-formed.
// Exponent
unsigned exp = 0;
bool exp_neg = false, exp_overflow = false;
if (p < pe && (*p == 'e' || *p == 'E'))
{
constexpr unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */
const char *before_exponent = p;
p++;
if (p < pe && (*p == '+' || *p == '-'))
{
exp_neg = (*p == '-');
p++;
}
if (p < pe && '0' <= *p && *p <= '9')
{
do
{
if (likely (exp * 10 + (*p - '0') <= MAX_EXP))
@@ -178,24 +172,17 @@ hb_strtod (const char *p, const char **end_ptr /* IN/OUT */)
exp_overflow = true;
p++;
} while (p < pe && '0' <= *p && *p <= '9');
mantissa_end = p;
}
// else: exponent malformed; end_ptr stays at end of mantissa
else
p = before_exponent; // rewind
}
*end_ptr = p;
*end_ptr = mantissa_end;
// Apply round-to-nearest-even using the dropped tail recorded above
if (frac_drop > 5 ||
(frac_drop == 5 && (frac_sticky || (((uint64_t) frac) & 1))))
frac += 1;
if (frac_count) value += frac / _pow10 ((unsigned) frac_count);
if (neg) value *= -1.;
if (unlikely (exp_overflow))
{
if (value == 0) return value;
if (exp_neg) return neg ? -0.0 : 0.0;
if (exp_neg) return neg ? -.0 : .0;
else return neg ? -DBL_MAX : DBL_MAX;
}
+7 -6
View File
@@ -37,9 +37,10 @@ hb_parse_double (const char **pp, const char *end, double *pv, bool whole_buffer
const char *pend = end;
double value = hb_strtod (*pp, &pend);
if (unlikely (*pp == pend)) return false;
if (unlikely (whole_buffer && end != pend)) return false;
*pv = value;
*pp = pend;
return !whole_buffer || end == pend;
return true;
}
/* Returns the digit value of c in the given base (2..36), or -1 if c
@@ -104,11 +105,11 @@ _parse_number (const char **pp, const char *end, T *pv,
uint64_t v = 0;
p = _hb_scan_digits (p, pend, base, &v, &overflow, &any);
if (!any || overflow) return false;
if (unlikely (!any || overflow)) return false;
T out;
if (!post (v, neg, &out)) return false;
if (whole_buffer && p != pend) return false;
if (unlikely (!post (v, neg, &out))) return false;
if (unlikely (whole_buffer && p != pend)) return false;
*pv = out;
*pp = p;
@@ -122,7 +123,7 @@ hb_parse_int (const char **pp, const char *end, int *pv, bool whole_buffer)
[] (uint64_t v, bool neg, int *out) {
/* INT_MAX = 2^31-1, INT_MIN magnitude = 2^31 */
uint64_t limit = neg ? (uint64_t) INT_MAX + 1 : (uint64_t) INT_MAX;
if (v > limit) return false;
if (unlikely (v > limit)) return false;
*out = (int) (neg ? -(int64_t) v : (int64_t) v);
return true;
});
@@ -134,7 +135,7 @@ hb_parse_uint (const char **pp, const char *end, unsigned *pv,
{
return _parse_number (pp, end, pv, whole_buffer, base,
[] (uint64_t v, bool neg, unsigned *out) {
if (v > UINT_MAX) return false;
if (unlikely (v > UINT_MAX)) return false;
/* Negate in unsigned so -1 wraps to UINT_MAX, matching strtoul
* truncated to unsigned. */
unsigned u = (unsigned) v;
+204 -1
View File
@@ -326,6 +326,38 @@ test_parse_int ()
hb_always_assert (pp == str);
hb_always_assert (pv == 42);
}
/* Whitespace between sign and digits is not allowed */
{
const char str[] = "- 123";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
int pv = 99;
hb_always_assert (!hb_parse_int (&pp, end, &pv));
hb_always_assert (pp == str);
hb_always_assert (pv == 99);
}
/* Plus zero, for symmetry with the -0 case */
{
const char str[] = "+0";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
int pv = 99;
hb_always_assert (hb_parse_int (&pp, end, &pv));
hb_always_assert (pv == 0);
hb_always_assert (pp == end);
}
/* Sign alone plus trailing whitespace still fails */
{
const char str[] = "- ";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
int pv = 99;
hb_always_assert (!hb_parse_int (&pp, end, &pv));
hb_always_assert (pv == 99);
}
}
static void
@@ -417,7 +449,7 @@ test_parse_uint ()
unsigned int pv = 99;
hb_always_assert (!hb_parse_uint (&pp, end, &pv, true));
hb_always_assert (pp == str);
// hb_always_assert (pv == 99); this shouldn't fail but it does with the current implementation
hb_always_assert (pv == 99);
}
/* Leading whitespace */
@@ -813,6 +845,82 @@ test_parse_uint ()
hb_always_assert (pp == str);
hb_always_assert (pv == 42);
}
/* Negative values whose magnitude exceeds UINT_MAX must fail, not wrap */
{
const char str[] = "-4294967296";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv = 99;
hb_always_assert (!hb_parse_uint (&pp, end, &pv));
hb_always_assert (pp == str);
hb_always_assert (pv == 99);
}
/* INT_MIN magnitude as unsigned: wraps to 0x80000000 */
{
const char str[] = "-2147483648";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv;
hb_always_assert (hb_parse_uint (&pp, end, &pv));
hb_always_assert (pv == 2147483648u);
hb_always_assert (pp == end);
}
/* Mixed case in base 36 */
{
const char str[] = "Az";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv;
hb_always_assert (hb_parse_uint (&pp, end, &pv, true, 36));
hb_always_assert (pv == 10 * 36 + 35);
hb_always_assert (pp == end);
}
/* Whitespace between sign and digits is not allowed */
{
const char str[] = "- 1";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv = 99;
hb_always_assert (!hb_parse_uint (&pp, end, &pv));
hb_always_assert (pv == 99);
}
/* "0x" prefix is case-insensitive but only for base 16 */
{
const char str[] = "0X10";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv;
hb_always_assert (hb_parse_uint (&pp, end, &pv, true, 16));
hb_always_assert (pv == 16);
hb_always_assert (pp == end);
}
/* A second "0x" is not consumed as a prefix */
{
const char str[] = "0x0x10";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv;
hb_always_assert (hb_parse_uint (&pp, end, &pv, false, 16));
hb_always_assert (pv == 0);
hb_always_assert (pp == str + 3);
}
/* Null byte stops the scan (end passed at NUL) */
{
const char str[] = "12\0F";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
unsigned int pv;
hb_always_assert (hb_parse_uint (&pp, end, &pv));
hb_always_assert (pv == 12);
hb_always_assert (pp == str + 2);
}
}
/* Tolerate 1 ULP difference. Needed because 32-bit x86 (x87) computes
@@ -1415,6 +1523,101 @@ test_parse_double (void)
hb_always_assert (pv == 100000.0);
hb_always_assert (pp == end);
}
/* Exponent with leading zeros */
{
const char str[] = "1e005";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv, true));
hb_always_assert (pv == 100000.0);
hb_always_assert (pp == end);
}
{
const char str[] = "1e-005";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv, true));
hb_always_assert (almost_equal (pv, 1e-5));
hb_always_assert (pp == end);
}
/* Bare -0 (no decimal) preserves the sign, matching strtod */
{
const char str[] = "-0";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv, true));
hb_always_assert (pv == 0.0 && std::signbit (pv));
hb_always_assert (pp == end);
}
/* Long mantissa followed by an exponent */
{
const char str[] = "12345678901234567890e-10";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv;
hb_always_assert (hb_parse_double (&pp, end, &pv, true));
hb_always_assert (pv > 1.234e9 && pv < 1.235e9);
hb_always_assert (pp == end);
}
/* whole_buffer failure must not touch *pv */
{
const char str[] = "123abc";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 42.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv, true));
hb_always_assert (pp == str);
hb_always_assert (pv == 42.0);
}
/* Same for no-match failure */
{
const char str[] = "abc";
const char *pp = str;
const char *end = str + ARRAY_LENGTH (str) - 1;
double pv = 42.0;
hb_always_assert (!hb_parse_double (&pp, end, &pv));
hb_always_assert (pp == str);
hb_always_assert (pv == 42.0);
}
/* Exponent followed by trailing content */
{
const char str[] = "1e5abc";
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 == 100000.0);
hb_always_assert (pp == str + 3);
}
/* Same with a sign in the exponent */
{
const char str[] = "1e+5x";
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 == 100000.0);
hb_always_assert (pp == str + 4);
}
/* Malformed exponent followed by content: should stop at the 'e' */
{
const char str[] = "1ex";
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 == 1.0);
hb_always_assert (pp == str + 1);
}
}
int