AK: Add to_{double, float} convenience functions to all string types

These are guarded with #ifndef KERNEL, since doubles (and floats) are
not allowed in KERNEL mode.
In StringUtils there is convert_to_floating_point which does have a
template parameter incase you have a templated type.
This commit is contained in:
davidot 2022-10-11 00:48:45 +02:00 committed by Linus Groh
parent 2334cd85a2
commit 6fd8e96d53
Notes: sideshowbarker 2024-07-17 05:12:13 +09:00
8 changed files with 68 additions and 0 deletions

View File

@ -95,6 +95,18 @@ template Optional<u16> FlyString::to_uint(TrimWhitespace) const;
template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
template Optional<u64> FlyString::to_uint(TrimWhitespace) const;
#ifndef KERNEL
Optional<double> FlyString::to_double(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<double>(view(), trim_whitespace);
}
Optional<float> FlyString::to_float(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<float>(view(), trim_whitespace);
}
#endif
bool FlyString::equals_ignoring_case(StringView other) const
{
return StringUtils::equals_ignoring_case(view(), other);

View File

@ -77,6 +77,10 @@ public:
Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
template<typename T = unsigned>
Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
#ifndef KERNEL
Optional<double> to_double(TrimWhitespace = TrimWhitespace::Yes) const;
Optional<float> to_float(TrimWhitespace = TrimWhitespace::Yes) const;
#endif
bool equals_ignoring_case(StringView) const;
bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;

View File

@ -182,6 +182,18 @@ template Optional<u32> String::to_uint(TrimWhitespace) const;
template Optional<unsigned long> String::to_uint(TrimWhitespace) const;
template Optional<unsigned long long> String::to_uint(TrimWhitespace) const;
#ifndef KERNEL
Optional<double> String::to_double(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<double>(*this, trim_whitespace);
}
Optional<float> String::to_float(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<float>(*this, trim_whitespace);
}
#endif
bool String::starts_with(StringView str, CaseSensitivity case_sensitivity) const
{
return StringUtils::starts_with(*this, str, case_sensitivity);

View File

@ -116,6 +116,10 @@ public:
[[nodiscard]] Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
template<typename T = unsigned>
[[nodiscard]] Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
#ifndef KERNEL
[[nodiscard]] Optional<double> to_double(TrimWhitespace = TrimWhitespace::Yes) const;
[[nodiscard]] Optional<float> to_float(TrimWhitespace = TrimWhitespace::Yes) const;
#endif
[[nodiscard]] String to_lowercase() const;
[[nodiscard]] String to_uppercase() const;

View File

@ -15,6 +15,7 @@
#include <AK/Vector.h>
#ifndef KERNEL
# include <AK/FloatingPointStringConversions.h>
# include <AK/String.h>
#endif
@ -232,6 +233,23 @@ template Optional<u16> convert_to_uint_from_octal(StringView str, TrimWhitespace
template Optional<u32> convert_to_uint_from_octal(StringView str, TrimWhitespace);
template Optional<u64> convert_to_uint_from_octal(StringView str, TrimWhitespace);
#ifndef KERNEL
template<typename T>
Optional<T> convert_to_floating_point(StringView str, TrimWhitespace trim_whitespace)
{
static_assert(IsSame<T, double> || IsSame<T, float>);
auto string = trim_whitespace == TrimWhitespace::Yes
? str.trim_whitespace()
: str;
char const* start = string.characters_without_null_termination();
return parse_floating_point_completely<T>(start, start + str.length());
}
template Optional<double> convert_to_floating_point(StringView str, TrimWhitespace);
template Optional<float> convert_to_floating_point(StringView str, TrimWhitespace);
#endif
bool equals_ignoring_case(StringView a, StringView b)
{
if (a.length() != b.length())

View File

@ -63,6 +63,10 @@ template<typename T = unsigned>
Optional<T> convert_to_uint_from_hex(StringView, TrimWhitespace = TrimWhitespace::Yes);
template<typename T = unsigned>
Optional<T> convert_to_uint_from_octal(StringView, TrimWhitespace = TrimWhitespace::Yes);
#ifndef KERNEL
template<typename T>
Optional<T> convert_to_floating_point(StringView, TrimWhitespace = TrimWhitespace::Yes);
#endif
bool equals_ignoring_case(StringView, StringView);
bool ends_with(StringView a, StringView b, CaseSensitivity);
bool starts_with(StringView, StringView, CaseSensitivity);

View File

@ -236,6 +236,16 @@ template Optional<long> StringView::to_uint() const;
template Optional<long long> StringView::to_uint() const;
#ifndef KERNEL
Optional<double> StringView::to_double(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<double>(*this, trim_whitespace);
}
Optional<float> StringView::to_float(TrimWhitespace trim_whitespace) const
{
return StringUtils::convert_to_floating_point<float>(*this, trim_whitespace);
}
bool StringView::operator==(String const& string) const
{
return *this == string.view();

View File

@ -190,6 +190,10 @@ public:
Optional<T> to_int() const;
template<typename T = unsigned>
Optional<T> to_uint() const;
#ifndef KERNEL
Optional<double> to_double(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const;
Optional<float> to_float(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const;
#endif
// Create a new substring view of this string view, starting either at the beginning of
// the given substring view, or after its end, and continuing until the end of this string