mirror of
https://github.com/flightlessmango/MangoHud.git
synced 2024-11-11 19:10:55 +00:00
Replace std::stoi with something that doesn't throw
This commit is contained in:
parent
e1fb2fbf88
commit
d9a09670d8
@ -78,29 +78,17 @@ static std::string itox(T i) {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static bool try_stoi(int& val, const std::string& str, std::size_t* pos = 0, int base = 10)
|
||||
static bool try_stoi(int& val, const std::string& str)
|
||||
{
|
||||
try {
|
||||
val = std::stoi(str, pos, base);
|
||||
if (sscanf(str.c_str(), "%d", &val) == 1)
|
||||
return true;
|
||||
} catch (std::invalid_argument& e) {
|
||||
#ifndef NDEBUG
|
||||
std::cerr << __func__ << ": invalid argument: '" << str << "'" << std::endl;
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool try_stoull(unsigned long long& val, const std::string& str, std::size_t* pos = 0, int base = 10)
|
||||
static bool try_stoull(unsigned long long& val, const std::string& str)
|
||||
{
|
||||
try {
|
||||
val = std::stoull(str, pos, base);
|
||||
if (sscanf(str.c_str(), "%llu", &val) == 1)
|
||||
return true;
|
||||
} catch (std::invalid_argument& e) {
|
||||
#ifndef NDEBUG
|
||||
std::cerr << __func__ << ": invalid argument: '" << str << "'" << std::endl;
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user