LibJS: Add a convenience StringView accessor to PrimitiveString

This commit is contained in:
Timothy Flynn 2023-01-15 09:37:32 -05:00 committed by Linus Groh
parent 0a1874c203
commit 4235c59397
Notes: sideshowbarker 2024-07-17 02:21:14 +09:00
2 changed files with 9 additions and 5 deletions

View File

@ -90,6 +90,12 @@ ThrowCompletionOr<String> PrimitiveString::utf8_string() const
return *m_utf8_string;
}
ThrowCompletionOr<StringView> PrimitiveString::utf8_string_view() const
{
(void)TRY(utf8_string());
return m_utf8_string->bytes_as_string_view();
}
ThrowCompletionOr<DeprecatedString> PrimitiveString::deprecated_string() const
{
TRY(resolve_rope_if_needed());
@ -281,11 +287,8 @@ ThrowCompletionOr<void> PrimitiveString::resolve_rope_if_needed() const
}
// Get the UTF-8 representations for both strings.
auto current_string_as_utf8_string = TRY(current->utf8_string());
auto current_string_as_utf8 = current_string_as_utf8_string.bytes_as_string_view();
auto previous_string_as_utf8_string = TRY(previous->utf8_string());
auto previous_string_as_utf8 = previous_string_as_utf8_string.bytes_as_string_view();
auto current_string_as_utf8 = TRY(current->utf8_string_view());
auto previous_string_as_utf8 = TRY(previous->utf8_string_view());
// NOTE: Now we need to look at the end of the previous string and the start
// of the current string, to see if they should be combined into a surrogate.

View File

@ -37,6 +37,7 @@ public:
u32 hash() const;
ThrowCompletionOr<String> utf8_string() const;
ThrowCompletionOr<StringView> utf8_string_view() const;
bool has_utf8_string() const { return m_utf8_string.has_value(); }
ThrowCompletionOr<DeprecatedString> deprecated_string() const;