Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all capabilities for toml integers and floating point numbers #968

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion include/CLI/TypeTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

// [CLI11:public_includes:set]
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <exception>
Expand Down Expand Up @@ -877,6 +878,33 @@ bool integral_conversion(const std::string &input, T &output) noexcept {
output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
return (static_cast<std::int64_t>(output) == output_sll);
}
// remove separators
if(input.find_first_of("_'") != std::string::npos) {
std::string nstring = input;
nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
return integral_conversion(nstring, output);
}
if(input.compare(0, 2, "0o") == 0) {
val = nullptr;
errno = 0;
output_ll = std::strtoull(input.c_str() + 2, &val, 8);
if(errno == ERANGE) {
return false;
}
output = static_cast<T>(output_ll);
return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
}
if(input.compare(0, 2, "0b") == 0) {
val = nullptr;
errno = 0;
output_ll = std::strtoull(input.c_str() + 2, &val, 2);
if(errno == ERANGE) {
return false;
}
output = static_cast<T>(output_ll);
return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
}
return false;
}

Expand All @@ -901,6 +929,33 @@ bool integral_conversion(const std::string &input, T &output) noexcept {
output = static_cast<T>(1);
return true;
}
// remove separators
if(input.find_first_of("_'") != std::string::npos) {
std::string nstring = input;
nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
return integral_conversion(nstring, output);
}
if(input.compare(0, 2, "0o") == 0) {
val = nullptr;
errno = 0;
output_ll = std::strtoll(input.c_str() + 2, &val, 8);
if(errno == ERANGE) {
return false;
}
output = static_cast<T>(output_ll);
return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
}
if(input.compare(0, 2, "0b") == 0) {
val = nullptr;
errno = 0;
output_ll = std::strtoll(input.c_str() + 2, &val, 2);
if(errno == ERANGE) {
return false;
}
output = static_cast<T>(output_ll);
return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
}
return false;
}

Expand Down Expand Up @@ -998,7 +1053,17 @@ bool lexical_cast(const std::string &input, T &output) {
char *val = nullptr;
auto output_ld = std::strtold(input.c_str(), &val);
output = static_cast<T>(output_ld);
return val == (input.c_str() + input.size());
if(val == (input.c_str() + input.size())) {
return true;
}
// remove separators
if(input.find_first_of("_'") != std::string::npos) {
std::string nstring = input;
nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
return lexical_cast(nstring, output);
}
return false;
}

/// complex
Expand Down
1 change: 1 addition & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <map>

TEST_CASE_METHOD(TApp, "OneFlagShort", "[app]") {
app.add_flag("-c,--count");
Expand Down
141 changes: 141 additions & 0 deletions tests/OptionTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "app_helper.hpp"

#include <algorithm>
#include <atomic>
#include <cmath>
#include <complex>
Expand Down Expand Up @@ -218,6 +219,146 @@ TEST_CASE_METHOD(TApp, "atomic_int_option", "[optiontype]") {
CHECK(0 == i);
}

static const std::map<std::string, double> testValuesDouble{
{"3.14159", 3.14159},
{"-3.14159", -3.14159},
{"+1.0", 1.0},
{"-0.01", -0.01},
{"5e22", 5e22},
{"-2E-2", -2e-2},
{"5e+22", 5e22},
{"1e06", 1e6},
{"6.626e-34", 6.626e-34},
{"6.626e+34", 6.626e34},
{"-6.626e-34", -6.626e-34},
{"224_617.445_991", 224617.445991},
{"224'617.445'991", 224617.445991},
{"inf", std::numeric_limits<double>::infinity()},
{"+inf", std::numeric_limits<double>::infinity()},
{"-inf", -std::numeric_limits<double>::infinity()},
{"nan", std::numeric_limits<double>::signaling_NaN()},
{"+nan", std::numeric_limits<double>::signaling_NaN()},
{"-nan", -std::numeric_limits<double>::signaling_NaN()},

};

TEST_CASE_METHOD(TApp, "floatingConversions", "[optiontype]") {

auto test_data = GENERATE(from_range(testValuesDouble));

double val{0};
app.add_option("--val", val);

args = {"--val", test_data.first};

run();
if(std::isnan(test_data.second)) {
CHECK(std::isnan(val));
} else {

CHECK_THAT(val, Catch::WithinRel(test_data.second, 1e-11));
}
}

static const std::map<std::string, std::int64_t> testValuesInt{
{"+99", 99},
{"99", 99},
{"-99", -99},
{"0xDEADBEEF", 0xDEADBEEF},
{"0xdeadbeef", 0xDEADBEEF},
{"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},
};

TEST_CASE_METHOD(TApp, "intConversions", "[optiontype]") {

auto test_data = GENERATE(from_range(testValuesInt));

std::int64_t val{0};
app.add_option("--val", val);

args = {"--val", test_data.first};

run();

CHECK(val == test_data.second);
}

TEST_CASE_METHOD(TApp, "intConversionsErange", "[optiontype]") {

std::int64_t val{0};
app.add_option("--val", val);

args = {"--val", "0o11545241241415151512312415123125667"};

CHECK_THROWS_AS(run(), CLI::ParseError);

args = {"--val", "0b1011000001101011001100110011111000101010101011111111111111111111111001010111011100"};

CHECK_THROWS_AS(run(), CLI::ParseError);
}

static const std::map<std::string, std::uint64_t> testValuesUInt{
{"+99", 99},
{"99", 99},
{"0xDEADBEEF", 0xDEADBEEF},
{"0xdeadbeef", 0xDEADBEEF},
{"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},
};

TEST_CASE_METHOD(TApp, "uintConversions", "[optiontype]") {

auto test_data = GENERATE(from_range(testValuesUInt));

std::uint64_t val{0};
app.add_option("--val", val);

args = {"--val", test_data.first};

run();

CHECK(val == test_data.second);
}

TEST_CASE_METHOD(TApp, "uintConversionsErange", "[optiontype]") {

std::uint64_t val{0};
app.add_option("--val", val);

args = {"--val", "0o11545241241415151512312415123125667"};

CHECK_THROWS_AS(run(), CLI::ParseError);

args = {"--val", "0b1011000001101011001100110011111000101010101011111111111111111111111001010111011100"};

CHECK_THROWS_AS(run(), CLI::ParseError);
}

TEST_CASE_METHOD(TApp, "CharOption", "[optiontype]") {
char c1{'t'};
app.add_option("-c", c1);
Expand Down