other replacements for string

This commit is contained in:
Erik Frojdh
2020-03-19 12:39:42 +01:00
parent dee0aea378
commit 9a6f521f6a
4 changed files with 28 additions and 7 deletions

View File

@ -628,6 +628,12 @@ inline int StringTo(const std::string &s) {
return std::stoi(s, nullptr, base);
}
template <>
inline int64_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stol(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

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