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 };