ladybird/Userland/Libraries/LibJS/Runtime/PrimitiveString.h
Andreas Kling a3e4535f34 LibJS: Resolve rope strings directly to UTF-16 when preferable
When someone calls PrimitiveString::utf16_string() on a rope string,
we know for sure that the client wants a UTF-16 string and may not
be interested in a UTF-8 version at all.

To avoid round-tripping through UTF-8 in this scenario, callers can
now inform resolve_rope_if_needed() about their preferred encoding,
should rope resolution take place. The UTF-16 case is actually a lot
simpler than the UTF-8 case, since we can simply ask for UTF-16 data
for each fiber of the rope, and then concatenate all the fibers.

Since LibJS always uses UTF-16 for regular expression matching, this
avoids round-tripping through UTF-8 whenever the input to a regex test
is already UTF-16. :^)
2023-07-13 20:53:54 +02:00

78 lines
2.6 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Utf16String.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class PrimitiveString final : public Cell {
JS_CELL(PrimitiveString, Cell);
public:
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, Utf16String);
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, String);
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, FlyString const&);
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, DeprecatedString);
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, DeprecatedFlyString const&);
[[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, PrimitiveString&, PrimitiveString&);
static ThrowCompletionOr<NonnullGCPtr<PrimitiveString>> create(VM&, StringView);
virtual ~PrimitiveString();
PrimitiveString(PrimitiveString const&) = delete;
PrimitiveString& operator=(PrimitiveString const&) = delete;
bool is_empty() 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;
bool has_deprecated_string() const { return m_deprecated_string.has_value(); }
ThrowCompletionOr<Utf16String> utf16_string() const;
ThrowCompletionOr<Utf16View> utf16_string_view() const;
bool has_utf16_string() const { return m_utf16_string.has_value(); }
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
private:
explicit PrimitiveString(PrimitiveString&, PrimitiveString&);
explicit PrimitiveString(String);
explicit PrimitiveString(DeprecatedString);
explicit PrimitiveString(Utf16String);
virtual void visit_edges(Cell::Visitor&) override;
enum class EncodingPreference {
UTF8,
UTF16,
};
ThrowCompletionOr<void> resolve_rope_if_needed(EncodingPreference) const;
mutable bool m_is_rope { false };
mutable GCPtr<PrimitiveString> m_lhs;
mutable GCPtr<PrimitiveString> m_rhs;
mutable Optional<String> m_utf8_string;
mutable Optional<DeprecatedString> m_deprecated_string;
mutable Optional<Utf16String> m_utf16_string;
};
}