1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:27:45 +00:00

LibJS: Allow PrimitiveString to be created with a UTF-16 string

PrimitiveString may currently only be created with a UTF-8 string, and
it transcodes on the fly when a UTF-16 string is needed. Allow creating
a PrimitiveString from a UTF-16 string to avoid unnecessary transcoding
when the caller only wants UTF-16.
This commit is contained in:
Timothy Flynn 2021-08-01 19:02:19 -04:00 committed by Andreas Kling
parent 4c2cc419f9
commit b6ff7f4fcc
2 changed files with 62 additions and 13 deletions

View file

@ -15,9 +15,13 @@ namespace JS {
class PrimitiveString final : public Cell {
public:
explicit PrimitiveString(String);
explicit PrimitiveString(Vector<u16>);
virtual ~PrimitiveString();
String const& string() const { return m_string; }
PrimitiveString(PrimitiveString const&) = delete;
PrimitiveString& operator=(PrimitiveString const&) = delete;
String const& string() const;
Vector<u16> const& utf16_string() const;
Utf16View utf16_string_view() const;
@ -25,13 +29,19 @@ public:
private:
virtual const char* class_name() const override { return "PrimitiveString"; }
String m_string;
mutable String m_utf8_string;
mutable bool m_has_utf8_string { false };
mutable Vector<u16> m_utf16_string;
mutable bool m_has_utf16_string { false };
};
PrimitiveString* js_string(Heap&, Utf16View const&);
PrimitiveString* js_string(VM&, Utf16View const&);
PrimitiveString* js_string(Heap&, Vector<u16>);
PrimitiveString* js_string(VM&, Vector<u16>);
PrimitiveString* js_string(Heap&, String);
PrimitiveString* js_string(VM&, String);