Fix schemagen integer parser rejecting zero with negative exponents #57

Merged
andrew merged 1 commits from weaselbot/weaseljson:weaselbot/issue-56 into main 2026-07-20 18:00:37 +00:00
2 changed files with 11 additions and 1 deletions
Showing only changes of commit cdff634057 - Show all commits
+10
View File
@@ -592,6 +592,13 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
("123.0", "WeaselJson_OK", 123),
("9e18", "WeaselJson_OK", 9000000000000000000),
("10e18", "WeaselJson_REJECT", 0),
# Issue #56: zero written with a negative exponent must be accepted.
("0e-1", "WeaselJson_OK", 0),
("0e-2", "WeaselJson_OK", 0),
("0.0e-2", "WeaselJson_OK", 0),
("-0e-2", "WeaselJson_OK", 0),
("0e-20", "WeaselJson_OK", 0),
("0.000e-5", "WeaselJson_OK", 0),
]
cases_src = self._build_cases_array("root", cases)
harness = textwrap.dedent(
@@ -634,6 +641,9 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
('{"age":2.0}', "WeaselJson_OK", 2),
('{"age":0.0001e4}', "WeaselJson_OK", 1),
('{"age":0.001}', "WeaselJson_REJECT", 0),
# Issue #56: zero written with a negative exponent must be accepted.
('{"age":0e-2}', "WeaselJson_OK", 0),
('{"age":-0e-20}', "WeaselJson_OK", 0),
(
'{"age":-9223372036854775808.0}',
"WeaselJson_OK",
+1 -1
View File
@@ -1058,10 +1058,10 @@ private:
++trim;
}}
int64_t finalExp = exp - fracDigits + trim;
if (finalExp < 0) return false;
size_t leadingZeros = 0;
while (leadingZeros < digits.size() && digits[leadingZeros] == '0') ++leadingZeros;
if (leadingZeros == digits.size()) {{ out = 0; return true; }}
if (finalExp < 0) return false;
if (leadingZeros > 0) digits.erase(0, leadingZeros);
constexpr uint64_t kMaxNeg = 9223372036854775808ULL;