mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
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:
parent
2334cd85a2
commit
6fd8e96d53
Notes:
sideshowbarker
2024-07-17 05:12:13 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/6fd8e96d53 Pull-request: https://github.com/SerenityOS/serenity/pull/15377 Issue: https://github.com/SerenityOS/serenity/issues/14691 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/linusg
8 changed files with 68 additions and 0 deletions
|
@ -95,6 +95,18 @@ template Optional<u16> FlyString::to_uint(TrimWhitespace) const;
|
||||||
template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
|
template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
|
||||||
template Optional<u64> 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
|
bool FlyString::equals_ignoring_case(StringView other) const
|
||||||
{
|
{
|
||||||
return StringUtils::equals_ignoring_case(view(), other);
|
return StringUtils::equals_ignoring_case(view(), other);
|
||||||
|
|
|
@ -77,6 +77,10 @@ public:
|
||||||
Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
|
Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
|
||||||
template<typename T = unsigned>
|
template<typename T = unsigned>
|
||||||
Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
|
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 equals_ignoring_case(StringView) const;
|
||||||
bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||||
|
|
|
@ -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> String::to_uint(TrimWhitespace) const;
|
||||||
template Optional<unsigned long 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
|
bool String::starts_with(StringView str, CaseSensitivity case_sensitivity) const
|
||||||
{
|
{
|
||||||
return StringUtils::starts_with(*this, str, case_sensitivity);
|
return StringUtils::starts_with(*this, str, case_sensitivity);
|
||||||
|
|
|
@ -116,6 +116,10 @@ public:
|
||||||
[[nodiscard]] Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
|
[[nodiscard]] Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
|
||||||
template<typename T = unsigned>
|
template<typename T = unsigned>
|
||||||
[[nodiscard]] Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
|
[[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_lowercase() const;
|
||||||
[[nodiscard]] String to_uppercase() const;
|
[[nodiscard]] String to_uppercase() const;
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
|
# include <AK/FloatingPointStringConversions.h>
|
||||||
# include <AK/String.h>
|
# include <AK/String.h>
|
||||||
#endif
|
#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<u32> convert_to_uint_from_octal(StringView str, TrimWhitespace);
|
||||||
template Optional<u64> 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)
|
bool equals_ignoring_case(StringView a, StringView b)
|
||||||
{
|
{
|
||||||
if (a.length() != b.length())
|
if (a.length() != b.length())
|
||||||
|
|
|
@ -63,6 +63,10 @@ template<typename T = unsigned>
|
||||||
Optional<T> convert_to_uint_from_hex(StringView, TrimWhitespace = TrimWhitespace::Yes);
|
Optional<T> convert_to_uint_from_hex(StringView, TrimWhitespace = TrimWhitespace::Yes);
|
||||||
template<typename T = unsigned>
|
template<typename T = unsigned>
|
||||||
Optional<T> convert_to_uint_from_octal(StringView, TrimWhitespace = TrimWhitespace::Yes);
|
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 equals_ignoring_case(StringView, StringView);
|
||||||
bool ends_with(StringView a, StringView b, CaseSensitivity);
|
bool ends_with(StringView a, StringView b, CaseSensitivity);
|
||||||
bool starts_with(StringView, StringView, CaseSensitivity);
|
bool starts_with(StringView, StringView, CaseSensitivity);
|
||||||
|
|
|
@ -236,6 +236,16 @@ template Optional<long> StringView::to_uint() const;
|
||||||
template Optional<long long> StringView::to_uint() const;
|
template Optional<long long> StringView::to_uint() const;
|
||||||
|
|
||||||
#ifndef KERNEL
|
#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
|
bool StringView::operator==(String const& string) const
|
||||||
{
|
{
|
||||||
return *this == string.view();
|
return *this == string.view();
|
||||||
|
|
|
@ -190,6 +190,10 @@ public:
|
||||||
Optional<T> to_int() const;
|
Optional<T> to_int() const;
|
||||||
template<typename T = unsigned>
|
template<typename T = unsigned>
|
||||||
Optional<T> to_uint() const;
|
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
|
// 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
|
// the given substring view, or after its end, and continuing until the end of this string
|
||||||
|
|
Loading…
Reference in a new issue