From cdf84a3e36db5cb58bc36933a5905a47bf33fba2 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 23 Dec 2023 11:35:03 +1300 Subject: [PATCH] AK: Implement StringView::to_number from String::to_number Do exactly what String does, then use StringView's implementation as String's new one. This should allow us to call to_number on a StringView. --- AK/String.h | 9 +-------- AK/StringView.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/AK/String.h b/AK/String.h index f122160fb69..b37ec0b9bfa 100644 --- a/AK/String.h +++ b/AK/String.h @@ -181,14 +181,7 @@ public: template Optional to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const { -#ifndef KERNEL - if constexpr (IsFloatingPoint) - return StringUtils::convert_to_floating_point(bytes_as_string_view(), trim_whitespace); -#endif - if constexpr (IsSigned) - return StringUtils::convert_to_int(bytes_as_string_view(), trim_whitespace); - else - return StringUtils::convert_to_uint(bytes_as_string_view(), trim_whitespace); + return bytes_as_string_view().to_number(trim_whitespace); } static ErrorOr vformatted(StringView fmtstr, TypeErasedFormatParams&); diff --git a/AK/StringView.h b/AK/StringView.h index ff9cc8068d8..da8db21e7ee 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -353,6 +353,19 @@ public: }()); } + template + Optional to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const + { +#ifndef KERNEL + if constexpr (IsFloatingPoint) + return StringUtils::convert_to_floating_point(*this, trim_whitespace); +#endif + if constexpr (IsSigned) + return StringUtils::convert_to_int(*this, trim_whitespace); + else + return StringUtils::convert_to_uint(*this, trim_whitespace); + } + private: friend class ByteString; char const* m_characters { nullptr };