Skip to content

Commit

Permalink
Do not skip leading whitespace in jvp_strtod
Browse files Browse the repository at this point in the history
`jvp_strtod` skips leading whitespace, but its decnum counterpart
`decNumberFromString` (called within `jv_number_with_literal`) does not.
Those two are called interchangeably, so it leads to inconsistent
behavior depending on whether the decnum feature is enabled.
Additionally, `classify`, used in the token scanner, only considers
[ \t\n\r] to be whitespace, but `jvp_strtod` consumes the larger set
[ \t\n\v\f\r], so those extra characters are considered literals.

Changing this deviates from the behavior of `strdod` from <stdlib.h> and
is technically a breaking API change, since it is a public symbol.
  • Loading branch information
thaliaarchi committed Nov 8, 2024
1 parent 60b1ac1 commit f1bfe95
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
14 changes: 3 additions & 11 deletions src/jv_dtoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -2368,29 +2368,21 @@ jvp_strtod

sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
dval(&rv) = 0.;
for(s = s00;;s++) switch(*s) {
switch(*(s = s00)) {
case '-':
sign = 1;
/* no break */
JQ_FALLTHROUGH;
case '+':
if (*++s)
goto break2;
break;
/* no break */
JQ_FALLTHROUGH;
case 0:
goto ret0;
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
case ' ':
continue;
default:
goto break2;
break;
}
break2:
if (*s == '0') {
#ifndef NO_HEX_FP /*{*/
switch(s[1]) {
Expand Down
4 changes: 2 additions & 2 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2084,8 +2084,8 @@ null
2

.[] |= try tonumber
["1", "2a", "3", " 4 ", "5.67", ".89", "-876", "+5.43", 21]
[1, 3, 5.67, 0.89, -876, 5.43, 21]
["1", "2a", "3", " 4", "5 ", "6.7", ".89", "-876", "+5.43", 21]
[1, 3, 6.7, 0.89, -876, 5.43, 21]

# Also 1859, but from 2073
any(keys[]|tostring?;true)
Expand Down

0 comments on commit f1bfe95

Please sign in to comment.