Skip to content

Make numeric parsing locale-independent#170

Merged
Taywee merged 1 commit into
Taywee:masterfrom
metsw24-max:locale-independent-number-parsing
May 21, 2026
Merged

Make numeric parsing locale-independent#170
Taywee merged 1 commit into
Taywee:masterfrom
metsw24-max:locale-independent-number-parsing

Conversation

@metsw24-max
Copy link
Copy Markdown
Contributor

This patch fixes locale-dependent numeric parsing in both ValueReader and CompletionFlag.

Previously, numeric parsing used std::istringstream without explicitly setting a locale, causing parsing behavior to depend on the process-global locale inherited from std::locale::global().

That could lead to inconsistent or incorrect parsing behavior, for example:

  • --rate 3.14 parsing as 3 with .14 treated as trailing input in locales using , as the decimal separator
  • --rate 3,14 being incorrectly accepted as 3.14
  • completion cword parsing rejecting otherwise valid digit-only inputs under locales with grouping validation rules

The fix explicitly imbues std::locale::classic() on the affected std::istringstream instances so parsing remains deterministic and locale-independent regardless of external std::locale::global() changes elsewhere in the process.

Modified locations:

  • args.hxx:1499
  • args.hxx:3826

Also adds a regression test using a custom numpunct facet to verify locale-independent parsing behavior without relying on system-installed locales.

Verified behavior:

  • 3.14 parses correctly
  • 2.5 parses correctly
  • 3,14 is rejected

Full test suite passes successfully.

@Taywee
Copy link
Copy Markdown
Owner

Taywee commented May 21, 2026

Looks mostly fine. I'm very slightly on the fence, because this is a backwards-incompatible change, changing parsing behavior for people who are already using locales that have different number parsing. Those are probably few and far between and the behavior is probably more surprising than helpful. It's probably better to force a user to use a custom reader if they really want locale-sensitive parsing.

@Taywee Taywee merged commit 8a254e8 into Taywee:master May 21, 2026
7 checks passed
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