Skip to content

fix: handle leading '+' sign in to_num string-to-number conversion#12

Merged
jalvesz merged 1 commit into
str2numfrom
copilot/fix-library-functions-issue-1197
Jul 13, 2026
Merged

fix: handle leading '+' sign in to_num string-to-number conversion#12
jalvesz merged 1 commit into
str2numfrom
copilot/fix-library-functions-issue-1197

Conversation

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown

to_num() silently returned 0 for any numeric string with a leading + (e.g. "+1", "+1.234"). The integer and all four real parsers (to_sp_base, to_dp_base, to_xdp_base, to_qp_base) only consumed a leading -; a + was left unadvanced, the digit loop found a non-digit immediately, and the parser succeeded with value 0.

k = to_num("+1", k)   ! returned 0 (wrong)
k = to_num("-1", k)   ! returned -1 (correct)

Changes

  • src/strings/stdlib_str2num.fypp — in all five to_*_base parsers, extended the leading-sign check from an if (minus only) to if / else if that also consumes a leading + and advances p. Updated the comment from "Verify leading negative" to "Verify leading sign".
  • test/string/test_string_to_number.fypp — added regression cases for integers ("+1", "+42", " +99", "+0005") and reals ("+1", "+1.234") across all type variants.

@jalvesz
jalvesz marked this pull request as ready for review July 13, 2026 08:42
@jalvesz
jalvesz changed the base branch from master to str2num July 13, 2026 10:09
@jalvesz
jalvesz merged commit 26fc53c into str2num Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants