assume dec unless 0x

This commit is contained in:
Erik Frojdh
2020-03-19 11:09:20 +01:00
parent 40c5ccfe37
commit 46e9b450c0
4 changed files with 57 additions and 37 deletions

View File

@ -622,6 +622,12 @@ inline uint64_t StringTo(const std::string &s) {
return std::stoull(s, nullptr, base);
}
template <>
inline int StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoi(s, nullptr, base);
}
/** For types with a .str() method use this for conversion */
template <typename T>
typename std::enable_if<has_str<T>::value, std::string>::type

View File

@ -165,3 +165,17 @@ TEST_CASE("uint64 from string"){
}
TEST_CASE("int from string"){
REQUIRE(StringTo<int>("-1") == -1);
REQUIRE(StringTo<int>("-0x1") == -0x1);
REQUIRE(StringTo<int>("-0x1") == -1);
REQUIRE(StringTo<int>("0") == 0);
REQUIRE(StringTo<int>("5") == 5);
REQUIRE(StringTo<int>("16") == 16);
REQUIRE(StringTo<int>("20") == 20);
REQUIRE(StringTo<int>("0x14") == 20);
REQUIRE(StringTo<int>("0x15") == 21);
REQUIRE(StringTo<int>("0xffffff") == 0xffffff);
}