catch exception in BSPF::stoi (fixes #958)

This commit is contained in:
Thomas Jentzsch 2023-02-27 19:43:23 +01:00
parent 7676e9b4c8
commit 93ea39d615
1 changed files with 7 additions and 4 deletions

View File

@ -197,11 +197,14 @@ namespace BSPF
template<int BASE = 10> template<int BASE = 10>
inline int stoi(string_view s, const int defaultValue = 0) inline int stoi(string_view s, const int defaultValue = 0)
{ {
try {
int i{}; int i{};
s = s.substr(s.find_first_not_of(" ")); s = s.substr(s.find_first_not_of(" "));
auto result = std::from_chars(s.data(), s.data() + s.size(), i, BASE); auto result = std::from_chars(s.data(), s.data() + s.size(), i, BASE);
return result.ec == std::errc() ? i : defaultValue; return result.ec == std::errc() ? i : defaultValue;
} }
catch(...) { return defaultValue; }
}
// Compare two strings (case insensitive) // Compare two strings (case insensitive)
// Return negative, zero, positive result for <,==,> respectively // Return negative, zero, positive result for <,==,> respectively