Skip to content

Conversation

phlptp
Copy link
Collaborator

@phlptp phlptp commented Dec 31, 2023

This PR add support for all TOML integer types.
This includes

{"+99", 99},
    { "99",99 },
    { "0xDEADBEEF",0xDEADBEEF },
    { "0xdeadbeef",0xDEADBEEF },
    { "0xdead_beef",0xDEADBEEF },
    { "0xdead'beef",0xDEADBEEF },
    { "0o01234567",001234567 },
    { "0o755",0755 },
    { "0755",0755 },
    { "995862_262",995862262 },
    { "995862262",995862262 },
    { "+995862275",+995862275 },
    { "995'862'275",995862275 },
    {"0b11010110",0xD6},
    {"0b1101'0110",0xD6},
    {"1_2_3_4_5",12345},

So support for separators (Toml and C++ standards), binary(new), hex(worked previously), and octal(new)

Copy link

codecov bot commented Dec 31, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (dc137f0) 100.00% compared to head (8428b19) 100.00%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #968   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines         4506      4555   +49     
=========================================
+ Hits          4506      4555   +49     

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

@phlptp phlptp requested a review from henryiii January 1, 2024 22:25
@phlptp
Copy link
Collaborator Author

phlptp commented Jan 1, 2024

Todo in next PR, Transform operation with escaped strings so it can be done in the CLI processing, and documentation update

@phlptp phlptp merged commit 9110160 into CLIUtils:main Jan 2, 2024
@phlptp phlptp deleted the toml_integer_support branch January 3, 2024 18:20
phlptp added a commit that referenced this pull request May 25, 2025
**Short description**
Application fails with `CLI::ConversionError` in some locales if the
application locale is set.

**Root cause**
CLI11 uses `std::stringstream` to format default value and
`strtol`/`strtod` to parse them back. The first method adds
locale-specific thousand separators but the second method parses the
number only up to the first separator. This causes an exception when the
same value is converted to string representation and then parsed back.

Examples: in _en_US_ locale 1234.56 is "1,234.56" and in de_DE locale
1234.56 is "1.234,56".

**Reproduction**
```c++
#include <string>
#include <CLI/CLI.hpp>

int main(int argc, const char **argv)
{
    std::locale::global(std::locale(""));
    CLI::App app{"Bug report app"};
    long foo;

    app.add_option("FOO", foo, "Foo option")
        ->envname("FOO")
        ->default_val(1234567);
    CLI11_PARSE(app, argc, argv);

    return 0;
}
```
Output:
```
$ g++ -Wall -Wextra -I CLI11/include/ cli11-bug-locale-string.cpp -o cli11-bug-locale-string
$ for locale in C en_US de_DE fr_FR de_CH; do echo "locale $locale"; LC_NUMERIC=$locale.UTF-8 ./cli11-bug-locale-string && echo OK; echo; done
locale C
OK

locale en_US
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1,234,567") produces an error : Could not convert: FOO = 1,234,567
Aborted (core dumped)

locale de_DE
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1.234.567") produces an error : Could not convert: FOO = 1.234.567
Aborted (core dumped)

locale fr_FR
terminate called after throwing an instance of 'CLI::ConversionError'
what():  The value FOO is not an allowed value for given default value("1 234 567") produces an error : Could not convert: FOO = 1 234 567
Aborted (core dumped)

locale de_CH
OK
```

**Fix description**
The fix strips group separators from the input string. This extends a
bit the logic introduced in change #968 for `_` and `'` separators.
There are no unit tests for the change because it requires to have all
mentioned locales to be installed in the system.

---------

Co-authored-by: Philip Top <[email protected]>
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.

1 participant