Skip to content

fix (str2num) : handle leading '+' sign in to_num parsers#1198

Open
jalvesz wants to merge 1 commit into
fortran-lang:masterfrom
jalvesz:str2num
Open

fix (str2num) : handle leading '+' sign in to_num parsers#1198
jalvesz wants to merge 1 commit into
fortran-lang:masterfrom
jalvesz:str2num

Conversation

@jalvesz

@jalvesz jalvesz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Fix #1197 by CoPilot:

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.

…12)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.62%. Comparing base (478ef58) to head (26fc53c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1198      +/-   ##
==========================================
+ Coverage   68.60%   68.62%   +0.01%     
==========================================
  Files         409      409              
  Lines       13784    13784              
  Branches     1559     1559              
==========================================
+ Hits         9457     9459       +2     
+ Misses       4327     4325       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jalvesz jalvesz requested a review from jvdp1 July 13, 2026 10:50
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.

function to_num() does not convert "+1" correctly

2 participants