AK: Implement UTF-16 string-to-number conversions

This commit is contained in:
Timothy Flynn 2025-06-27 13:56:17 -04:00 committed by Tim Flynn
commit d40e3af697
Notes: github-actions[bot] 2025-07-18 16:47:05 +00:00
6 changed files with 163 additions and 45 deletions

View file

@ -7,6 +7,7 @@
#include <AK/StringConversions.h>
#include <AK/StringView.h>
#include <AK/Utf16View.h>
#include <fast_float/fast_float.h>
@ -29,27 +30,22 @@ namespace AK {
__ENUMERATE_TYPE(float) \
__ENUMERATE_TYPE(double)
template<Arithmetic T>
Optional<ParseFirstNumberResult<T>> parse_first_number(StringView string, TrimWhitespace trim_whitespace, int base)
template<typename CharType, Arithmetic ValueType>
static constexpr Optional<ParseFirstNumberResult<ValueType>> from_chars(CharType const* string, size_t length, int base)
{
if (trim_whitespace == TrimWhitespace::Yes)
string = StringUtils::trim_whitespace(string, TrimMode::Both);
ValueType value { 0 };
auto const* begin = string.characters_without_null_termination();
auto const* end = begin + string.length();
T value { 0 };
fast_float::parse_options_t<char> options;
fast_float::parse_options_t<CharType> options;
options.base = base;
options.format |= fast_float::chars_format::no_infnan;
if constexpr (IsSigned<T> || IsFloatingPoint<T>) {
if constexpr (IsSigned<ValueType> || IsFloatingPoint<ValueType>) {
options.format |= fast_float::chars_format::allow_leading_plus;
}
auto result = fast_float::from_chars_advanced(begin, end, value, options);
auto result = fast_float::from_chars_advanced(string, string + length, value, options);
if constexpr (IsFloatingPoint<T>) {
if constexpr (IsFloatingPoint<ValueType>) {
if (result.ec == std::errc::result_out_of_range && (__builtin_isinf(value) || value == 0))
result.ec = {};
}
@ -57,7 +53,26 @@ Optional<ParseFirstNumberResult<T>> parse_first_number(StringView string, TrimWh
if (result.ec != std::errc {})
return {};
return ParseFirstNumberResult { value, static_cast<size_t>(result.ptr - begin) };
return ParseFirstNumberResult { value, static_cast<size_t>(result.ptr - string) };
}
template<Arithmetic T>
Optional<ParseFirstNumberResult<T>> parse_first_number(StringView string, TrimWhitespace trim_whitespace, int base)
{
if (trim_whitespace == TrimWhitespace::Yes)
string = StringUtils::trim_whitespace(string, TrimMode::Both);
return from_chars<char, T>(string.characters_without_null_termination(), string.length(), base);
}
template<Arithmetic T>
Optional<ParseFirstNumberResult<T>> parse_first_number(Utf16View const& string, TrimWhitespace trim_whitespace, int base)
{
if (string.has_ascii_storage())
return parse_first_number<T>(string.bytes(), trim_whitespace, base);
auto trimmed_string = trim_whitespace == TrimWhitespace::Yes ? string.trim_whitespace() : string;
return from_chars<char16_t, T>(trimmed_string.utf16_span().data(), trimmed_string.length_in_code_units(), base);
}
#define __ENUMERATE_TYPE(type) \
@ -65,6 +80,11 @@ Optional<ParseFirstNumberResult<T>> parse_first_number(StringView string, TrimWh
ENUMERATE_ARITHMETIC_TYPES
#undef __ENUMERATE_TYPE
#define __ENUMERATE_TYPE(type) \
template Optional<ParseFirstNumberResult<type>> parse_first_number(Utf16View const&, TrimWhitespace, int);
ENUMERATE_ARITHMETIC_TYPES
#undef __ENUMERATE_TYPE
template<Arithmetic T>
Optional<T> parse_number(StringView string, TrimWhitespace trim_whitespace, int base)
{
@ -81,20 +101,54 @@ Optional<T> parse_number(StringView string, TrimWhitespace trim_whitespace, int
return result->value;
}
template<Arithmetic T>
Optional<T> parse_number(Utf16View const& string, TrimWhitespace trim_whitespace, int base)
{
if (string.has_ascii_storage())
return parse_number<T>(string.bytes(), trim_whitespace, base);
auto trimmed_string = trim_whitespace == TrimWhitespace::Yes ? string.trim_whitespace() : string;
auto result = parse_first_number<T>(trimmed_string, TrimWhitespace::No, base);
if (!result.has_value())
return {};
if (result->characters_parsed != trimmed_string.length_in_code_units())
return {};
return result->value;
}
#define __ENUMERATE_TYPE(type) \
template Optional<type> parse_number(StringView, TrimWhitespace, int);
ENUMERATE_ARITHMETIC_TYPES
#undef __ENUMERATE_TYPE
#define __ENUMERATE_TYPE(type) \
template Optional<type> parse_number(Utf16View const&, TrimWhitespace, int);
ENUMERATE_ARITHMETIC_TYPES
#undef __ENUMERATE_TYPE
template<Integral T>
Optional<T> parse_hexadecimal_number(StringView string, TrimWhitespace trim_whitespace)
{
return parse_number<T>(string, trim_whitespace, 16);
}
template<Integral T>
Optional<T> parse_hexadecimal_number(Utf16View const& string, TrimWhitespace trim_whitespace)
{
return parse_number<T>(string, trim_whitespace, 16);
}
#define __ENUMERATE_TYPE(type) \
template Optional<type> parse_hexadecimal_number(StringView, TrimWhitespace);
ENUMERATE_INTEGRAL_TYPES
#undef __ENUMERATE_TYPE
#define __ENUMERATE_TYPE(type) \
template Optional<type> parse_hexadecimal_number(Utf16View const&, TrimWhitespace);
ENUMERATE_INTEGRAL_TYPES
#undef __ENUMERATE_TYPE
}