diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 4753de89..bc6ccc26 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -99,7 +99,8 @@ enum class performance_hints : uint64_t { // parsed with a few bitwise operations, without branches that depend on // their shape. Do not use it when the numbers have predictable shapes (e.g., // mostly 0, 1.0 and 0.75 in a regular pattern): the usual parser is then - // faster. Other inputs are parsed as usual, only a bit more slowly. The + // faster. Inputs that cannot start a number (e.g., "auto") are rejected at + // once; other inputs are parsed as usual, only a bit more slowly. The // results do not change. Only for char inputs and the float and double // types; the JSON and JavaScript formats ignore it. short_numbers = 1 << 2, diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 17febfae..acb099bf 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -385,10 +385,34 @@ from_chars_short(UC const *first, UC const *last, T &value, chars_format fmt, return false; } fmt = adjust_for_feature_macros(fmt); - // JSON and JavaScript have stricter rules (e.g., no "01" or "5."). + // JSON and JavaScript have stricter rules (e.g., no "01" or "5."). The + // short parser cannot tell a decimal point that is a digit (e.g., V8 passes + // '0' so that parseInt stops after the integer part) from the digit itself. if (!uint64_t(fmt & chars_format::fixed) || uint64_t(fmt & (basic_json_fmt | basic_javascript_fmt)) || - (last - first) > 8 || first == last) { + is_integer(decimal_point) || first == last) { + return false; + } + // A number starts with a digit, a sign or the decimal point. Other inputs, + // such as "auto", are rejected here as the usual parser would reject them: + // falling back would cost them a full parse, and such inputs can be common + // (e.g., in a JavaScript engine, most strings converted to numbers are not + // numbers). We test before the length so that long words (e.g., "undefined") + // take the same branch. + UC const c = *first; + if (!is_integer(c) && c != UC('-') && c != decimal_point && + !(uint64_t(fmt & chars_format::allow_leading_plus) && c == UC('+'))) { + // Leading spaces, infinity and NaN are left to the usual parser. + bool const maybe_infnan = !uint64_t(fmt & chars_format::no_infnan) && + ((c | 0x20) == 'i' || (c | 0x20) == 'n'); + if (uint64_t(fmt & chars_format::skip_white_space) || maybe_infnan) { + return false; + } + answer.ec = std::errc::invalid_argument; + answer.ptr = first; + return true; + } + if ((last - first) > 8) { return false; } uint64_t mantissa; diff --git a/tests/performance_hints_test.cpp b/tests/performance_hints_test.cpp index 1800281d..d127f198 100644 --- a/tests/performance_hints_test.cpp +++ b/tests/performance_hints_test.cpp @@ -30,6 +30,12 @@ chars_format const formats[] = { chars_format::general, chars_format::general | chars_format::allow_leading_plus, chars_format::general | chars_format::skip_white_space, + chars_format::general | chars_format::no_infnan, + chars_format::general | chars_format::no_infnan | + chars_format::allow_leading_plus, + chars_format::general | chars_format::no_infnan | + chars_format::skip_white_space, + chars_format::fixed | chars_format::no_infnan, chars_format::scientific, chars_format::fixed, chars_format::json, @@ -73,12 +79,19 @@ bool same_result(std::basic_string const &s, chars_format fmt) { if (!same(s, r1, expected, r2, actual, "from_chars", fmt)) { return false; } - fast_float::parse_options_t const options(fmt, UC(',')); - expected = T(-12345); - actual = T(-12345); - r1 = fast_float::from_chars_advanced(first, last, expected, options); - r2 = fast_float::from_chars_advanced(first, last, actual, options); - return same(s, r1, expected, r2, actual, "from_chars_advanced", fmt); + // A comma as decimal point, and a digit: V8 passes '0' so that parseInt + // stops after the integer part. + for (UC const decimal_point : {UC(','), UC('0'), UC('5')}) { + fast_float::parse_options_t const options(fmt, decimal_point); + expected = T(-12345); + actual = T(-12345); + r1 = fast_float::from_chars_advanced(first, last, expected, options); + r2 = fast_float::from_chars_advanced(first, last, actual, options); + if (!same(s, r1, expected, r2, actual, "from_chars_advanced", fmt)) { + return false; + } + } + return true; } // The parser's own result also tells which error it found, and where. @@ -313,14 +326,18 @@ int main() { // around 8 characters "12345678", "123456789", "-1234567", "-12345678", "+1234567", "1234.567", "1234.5678", "-123.456", ".1234567", "1234567.", "0000000.", "-0000000", - "99999999", "9.999999", "1.2.3", "12..3", "1,234", "12 34"}; + "99999999", "9.999999", "1.2.3", "12..3", "1,234", "12 34", + // not numbers + "auto", "none", "px", "x", "a1", "e5", "E5", "true", "undefined", + "inherit", "Infinity", "infinity", "INF", "inf", "NaN", "nan", "nan(1)", + "i", "n", "Nope", "infx", " 1", "\t1", "_1", "(1)", "#1"}; for (std::string const &s : cases) { if (!check_all(s)) { return EXIT_FAILURE; } } // Short random strings over an alphabet rich in signs, digits and dots. - char const alphabet[] = "0123456789000.-+e ,xn"; + char const alphabet[] = "0123456789000.-+e ,xniaf"; std::mt19937_64 gen(1234); for (size_t i = 0; i < 100000; i++) { size_t const length = 1 + size_t(gen() % 10);