[Lua] Resolve stoi Exception (#2736)

* [Lua] Resolve stoi Exception

* Change fallback for wp, not really needed for safety

* Change to Strings::ToInt
This commit is contained in:
Chris Miles
2023-02-06 17:22:01 -06:00
committed by GitHub
parent 8031bf0bcb
commit 6a9228ed6e
3 changed files with 102 additions and 94 deletions
+7
View File
@@ -763,3 +763,10 @@ std::string Strings::Random(size_t length)
std::generate_n(str.begin(), length, randchar);
return str;
}
// a wrapper for stoi which will return a fallback if the string
// fails to cast to a number
int Strings::ToInt(const std::string &s, int fallback)
{
return Strings::IsNumber(s) ? std::stoi(s) : fallback;
}
+1
View File
@@ -86,6 +86,7 @@ class Strings {
public:
static bool Contains(std::vector<std::string> container, std::string element);
static bool Contains(const std::string& subject, const std::string& search);
static int ToInt(const std::string &s, int fallback = 0);
static bool IsNumber(const std::string &s);
static bool IsFloat(const std::string &s);
static const std::string ToLower(std::string s);