Skip to content

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

Merged
jalvesz merged 1 commit into
fortran-lang:masterfrom
jalvesz:str2num
Jul 18, 2026
Merged

fix (str2num) : handle leading '+' sign in to_num parsers#1198
jalvesz merged 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 review from a team and jvdp1 July 13, 2026 10:50

@jvdp1 jvdp1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. thank you @jalvesz

@jalvesz
jalvesz merged commit a7e294f into fortran-lang:master Jul 18, 2026
77 of 78 checks passed
@jalvesz
jalvesz deleted the str2num branch July 20, 2026 07:16
Mahmood-Sinan pushed a commit to Mahmood-Sinan/stdlib that referenced this pull request Aug 12, 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.

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

3 participants