From a24c49c18c347770c467586614555e24d5c60e74 Mon Sep 17 00:00:00 2001 From: Fausto Tommasi Date: Fri, 27 Jan 2023 06:43:50 -0600 Subject: [PATCH] LibRegex: Add to_string method for RegexStringView --- Userland/Libraries/LibRegex/RegexMatch.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Userland/Libraries/LibRegex/RegexMatch.h b/Userland/Libraries/LibRegex/RegexMatch.h index 923d550300..4f6882c244 100644 --- a/Userland/Libraries/LibRegex/RegexMatch.h +++ b/Userland/Libraries/LibRegex/RegexMatch.h @@ -8,6 +8,7 @@ #include "Forward.h" #include "RegexOptions.h" +#include #include #include @@ -162,6 +163,11 @@ public: { } + RegexStringView(String const& string) + : m_view(string.bytes_as_string_view()) + { + } + RegexStringView(StringView const view) : m_view(view) { @@ -394,6 +400,19 @@ public: }); } + ErrorOr to_string() const + { + return m_view.visit( + [](StringView view) { return String::from_utf8(view); }, + [](Utf16View view) { return view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes); }, + [](auto& view) -> ErrorOr { + StringBuilder builder; + for (auto it = view.begin(); it != view.end(); ++it) + TRY(builder.try_append_code_point(*it)); + return builder.to_string(); + }); + } + // Note: index must always be the code unit offset to return. u32 operator[](size_t index) const {