mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-22 18:31:59 -05:00
Roll our own implementation of parse
This commit is contained in:
parent
fa1a374a04
commit
2b3e7b98b8
1 changed files with 23 additions and 6 deletions
|
@ -11,7 +11,6 @@
|
|||
|
||||
#include "../common.h"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
|
@ -122,13 +121,31 @@ namespace String
|
|||
|
||||
template<typename T> std::optional<T> 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<T>((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
|
||||
|
||||
|
|
Loading…
Reference in a new issue