mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-10 17:42:29 +01:00
Rename String::Parse to String::parse, use from_chars instead
This commit is contained in:
@@ -149,7 +149,7 @@ ShortcutInput::ShortcutInput(std::string_view value)
|
||||
}
|
||||
else
|
||||
{
|
||||
auto number = String::Parse<int32_t>(rem);
|
||||
auto number = String::parse<int32_t>(rem);
|
||||
if (number.has_value())
|
||||
{
|
||||
Kind = InputDeviceKind::JoyButton;
|
||||
@@ -161,7 +161,7 @@ ShortcutInput::ShortcutInput(std::string_view value)
|
||||
else if (String::startsWith(rem, "MOUSE ", true))
|
||||
{
|
||||
rem = rem.substr(6);
|
||||
auto number = String::Parse<int32_t>(rem);
|
||||
auto number = String::parse<int32_t>(rem);
|
||||
if (number)
|
||||
{
|
||||
Kind = InputDeviceKind::Mouse;
|
||||
|
||||
@@ -174,7 +174,7 @@ namespace OpenRCT2::Ui::Windows
|
||||
break;
|
||||
|
||||
case WIDX_RATE:
|
||||
const auto res = String::Parse<int32_t>(text);
|
||||
const auto res = String::parse<int32_t>(text);
|
||||
if (res.has_value())
|
||||
{
|
||||
int32_t rate = res.value();
|
||||
|
||||
@@ -236,7 +236,7 @@ namespace OpenRCT2::Ui::Windows
|
||||
if (widgetIndex != WIDX_PREVIEW)
|
||||
return;
|
||||
|
||||
const auto res = String::Parse<int32_t>(text);
|
||||
const auto res = String::parse<int32_t>(text);
|
||||
if (res.has_value())
|
||||
{
|
||||
uint16_t size;
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace OpenRCT2::Ui::Windows
|
||||
if (widgetIndex != WIDX_PREVIEW)
|
||||
return;
|
||||
|
||||
const auto res = String::Parse<int32_t>(text);
|
||||
const auto res = String::parse<int32_t>(text);
|
||||
if (res.has_value())
|
||||
{
|
||||
int32_t size;
|
||||
|
||||
@@ -141,7 +141,7 @@ namespace OpenRCT2::Ui::Windows
|
||||
if (widgetIndex != WIDX_PREVIEW || text.empty())
|
||||
return;
|
||||
|
||||
const auto res = String::Parse<int32_t>(text);
|
||||
const auto res = String::parse<int32_t>(text);
|
||||
|
||||
if (res.has_value())
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "StringTypes.h"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
@@ -112,33 +113,19 @@ namespace OpenRCT2::String
|
||||
std::string toUpper(std::string_view src);
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> Parse(std::string_view input)
|
||||
std::optional<T> parse(std::string_view input)
|
||||
{
|
||||
if (input.size() == 0)
|
||||
return std::nullopt;
|
||||
|
||||
T result = 0;
|
||||
for (size_t i = 0; i < input.size(); i++)
|
||||
if (input.empty())
|
||||
{
|
||||
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 std::nullopt;
|
||||
}
|
||||
return result;
|
||||
T result;
|
||||
auto [ptr, ec] = std::from_chars(input.data(), input.data() + input.size(), result);
|
||||
if (ec == std::errc() && ptr == input.data() + input.size())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user