diff options
author | davidot <davidot@serenityos.org> | 2022-10-11 00:48:45 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-10-23 15:48:45 +0200 |
commit | 6fd8e96d537e8175f0ca06789a6a19913f561455 (patch) | |
tree | 9a26604021db8e7133f9ad99874c6b4e01bd4e50 /AK | |
parent | 2334cd85a26f6244c7af198f118a6d7e9c437b44 (diff) | |
download | serenity-6fd8e96d537e8175f0ca06789a6a19913f561455.zip |
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.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/FlyString.cpp | 12 | ||||
-rw-r--r-- | AK/FlyString.h | 4 | ||||
-rw-r--r-- | AK/String.cpp | 12 | ||||
-rw-r--r-- | AK/String.h | 4 | ||||
-rw-r--r-- | AK/StringUtils.cpp | 18 | ||||
-rw-r--r-- | AK/StringUtils.h | 4 | ||||
-rw-r--r-- | AK/StringView.cpp | 10 | ||||
-rw-r--r-- | AK/StringView.h | 4 |
8 files changed, 68 insertions, 0 deletions
diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 7e4465876f..1c30af7418 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -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); diff --git a/AK/FlyString.h b/AK/FlyString.h index c81eb65e48..3cc12db868 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -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; diff --git a/AK/String.cpp b/AK/String.cpp index 86d0be5f20..cd318efbd5 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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); diff --git a/AK/String.h b/AK/String.h index 29682bc5ef..89dcff15d6 100644 --- a/AK/String.h +++ b/AK/String.h @@ -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; diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 42887cce05..28d4a6b044 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -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()) diff --git a/AK/StringUtils.h b/AK/StringUtils.h index aa9b2af73d..10b9952516 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -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); diff --git a/AK/StringView.cpp b/AK/StringView.cpp index a987696a08..278b833bfd 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -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(); diff --git a/AK/StringView.h b/AK/StringView.h index 4b8b974aa4..bb1b664184 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -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 |