mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-22 07:13:07 +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
|
else
|
||||||
{
|
{
|
||||||
auto number = String::Parse<int32_t>(rem);
|
auto number = String::parse<int32_t>(rem);
|
||||||
if (number.has_value())
|
if (number.has_value())
|
||||||
{
|
{
|
||||||
Kind = InputDeviceKind::JoyButton;
|
Kind = InputDeviceKind::JoyButton;
|
||||||
@@ -161,7 +161,7 @@ ShortcutInput::ShortcutInput(std::string_view value)
|
|||||||
else if (String::startsWith(rem, "MOUSE ", true))
|
else if (String::startsWith(rem, "MOUSE ", true))
|
||||||
{
|
{
|
||||||
rem = rem.substr(6);
|
rem = rem.substr(6);
|
||||||
auto number = String::Parse<int32_t>(rem);
|
auto number = String::parse<int32_t>(rem);
|
||||||
if (number)
|
if (number)
|
||||||
{
|
{
|
||||||
Kind = InputDeviceKind::Mouse;
|
Kind = InputDeviceKind::Mouse;
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ namespace OpenRCT2::Ui::Windows
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case WIDX_RATE:
|
case WIDX_RATE:
|
||||||
const auto res = String::Parse<int32_t>(text);
|
const auto res = String::parse<int32_t>(text);
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
{
|
||||||
int32_t rate = res.value();
|
int32_t rate = res.value();
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ namespace OpenRCT2::Ui::Windows
|
|||||||
if (widgetIndex != WIDX_PREVIEW)
|
if (widgetIndex != WIDX_PREVIEW)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto res = String::Parse<int32_t>(text);
|
const auto res = String::parse<int32_t>(text);
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
{
|
||||||
uint16_t size;
|
uint16_t size;
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ namespace OpenRCT2::Ui::Windows
|
|||||||
if (widgetIndex != WIDX_PREVIEW)
|
if (widgetIndex != WIDX_PREVIEW)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto res = String::Parse<int32_t>(text);
|
const auto res = String::parse<int32_t>(text);
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
{
|
||||||
int32_t size;
|
int32_t size;
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ namespace OpenRCT2::Ui::Windows
|
|||||||
if (widgetIndex != WIDX_PREVIEW || text.empty())
|
if (widgetIndex != WIDX_PREVIEW || text.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const auto res = String::Parse<int32_t>(text);
|
const auto res = String::parse<int32_t>(text);
|
||||||
|
|
||||||
if (res.has_value())
|
if (res.has_value())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "StringTypes.h"
|
#include "StringTypes.h"
|
||||||
|
|
||||||
|
#include <charconv>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -112,34 +113,20 @@ namespace OpenRCT2::String
|
|||||||
std::string toUpper(std::string_view src);
|
std::string toUpper(std::string_view src);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T> Parse(std::string_view input)
|
std::optional<T> parse(std::string_view input)
|
||||||
{
|
{
|
||||||
if (input.size() == 0)
|
if (input.empty())
|
||||||
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;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
}
|
T result;
|
||||||
else
|
auto [ptr, ec] = std::from_chars(input.data(), input.data() + input.size(), result);
|
||||||
|
if (ec == std::errc() && ptr == input.data() + input.size())
|
||||||
{
|
{
|
||||||
// Bad character
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns string representation of a hexadecimal input, such as SHA256 hash
|
* Returns string representation of a hexadecimal input, such as SHA256 hash
|
||||||
|
|||||||
Reference in New Issue
Block a user