diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 8d2ab02462..0849571353 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -11,7 +11,6 @@ #include "../common.h" -#include #include #include #include @@ -122,13 +121,31 @@ namespace String template std::optional Parse(std::string_view input) { - T out; - const std::from_chars_result result = std::from_chars(input.data(), input.data() + input.size(), out); - if (result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) - { + if (input.size() == 0) return std::nullopt; + + T result = 0; + for (size_t i = 0; i < input.size(); i++) + { + auto chr = input[i]; + if (chr >= '0' && chr <= '9') + { + auto digit = chr - '0'; + auto last = result; + result = static_cast((result * 10) + digit); + if (result <= last) + { + // Overflow, number too large for type + return std::nullopt; + } + } + else + { + // Bad character + return std::nullopt; + } } - return out; + return result; } } // namespace String