mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 13:57:35 +00:00
LibJS: Add a simple reference-counted UTF-16 string
To help alleviate memory usage when creating and copying large strings, create a simple wrapper around a Vector<u16> to reference count UTF-16 strings.
This commit is contained in:
parent
3322efd4cd
commit
02e7dceb96
4 changed files with 183 additions and 0 deletions
|
@ -157,6 +157,7 @@ set(SOURCES
|
||||||
Runtime/TypedArray.cpp
|
Runtime/TypedArray.cpp
|
||||||
Runtime/TypedArrayConstructor.cpp
|
Runtime/TypedArrayConstructor.cpp
|
||||||
Runtime/TypedArrayPrototype.cpp
|
Runtime/TypedArrayPrototype.cpp
|
||||||
|
Runtime/Utf16String.cpp
|
||||||
Runtime/Value.cpp
|
Runtime/Value.cpp
|
||||||
Runtime/VM.cpp
|
Runtime/VM.cpp
|
||||||
Runtime/WeakContainer.cpp
|
Runtime/WeakContainer.cpp
|
||||||
|
|
|
@ -169,6 +169,7 @@ class Statement;
|
||||||
class StringOrSymbol;
|
class StringOrSymbol;
|
||||||
class Symbol;
|
class Symbol;
|
||||||
class Token;
|
class Token;
|
||||||
|
class Utf16String;
|
||||||
class VM;
|
class VM;
|
||||||
class Value;
|
class Value;
|
||||||
class WeakContainer;
|
class WeakContainer;
|
||||||
|
|
120
Userland/Libraries/LibJS/Runtime/Utf16String.cpp
Normal file
120
Userland/Libraries/LibJS/Runtime/Utf16String.cpp
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/StringView.h>
|
||||||
|
#include <AK/Utf16View.h>
|
||||||
|
#include <LibJS/Runtime/Utf16String.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
namespace Detail {
|
||||||
|
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> the_empty_utf16_string()
|
||||||
|
{
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> empty_string = Utf16StringImpl::create();
|
||||||
|
return empty_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16StringImpl::Utf16StringImpl(Vector<u16> string)
|
||||||
|
: m_string(move(string))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create()
|
||||||
|
{
|
||||||
|
return adopt_ref(*new Utf16StringImpl());
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Vector<u16> string)
|
||||||
|
{
|
||||||
|
return adopt_ref(*new Utf16StringImpl(move(string)));
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(StringView const& string)
|
||||||
|
{
|
||||||
|
return create(AK::utf8_to_utf16(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Utf16View const& view)
|
||||||
|
{
|
||||||
|
Vector<u16> string;
|
||||||
|
string.ensure_capacity(view.length_in_code_units());
|
||||||
|
string.append(view.data(), view.length_in_code_units());
|
||||||
|
return create(move(string));
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<u16> const& Utf16StringImpl::string() const
|
||||||
|
{
|
||||||
|
return m_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16View Utf16StringImpl::view() const
|
||||||
|
{
|
||||||
|
return Utf16View { m_string };
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16String::Utf16String()
|
||||||
|
: m_string(Detail::the_empty_utf16_string())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16String::Utf16String(Vector<u16> string)
|
||||||
|
: m_string(Detail::Utf16StringImpl::create(move(string)))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16String::Utf16String(StringView const& string)
|
||||||
|
: m_string(Detail::Utf16StringImpl::create(move(string)))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16String::Utf16String(Utf16View const& string)
|
||||||
|
: m_string(Detail::Utf16StringImpl::create(move(string)))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<u16> const& Utf16String::string() const
|
||||||
|
{
|
||||||
|
return m_string->string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16View Utf16String::view() const
|
||||||
|
{
|
||||||
|
return m_string->view();
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16View Utf16String::substring_view(size_t code_unit_offset, size_t code_unit_length) const
|
||||||
|
{
|
||||||
|
return view().substring_view(code_unit_offset, code_unit_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf16View Utf16String::substring_view(size_t code_unit_offset) const
|
||||||
|
{
|
||||||
|
return view().substring_view(code_unit_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
String Utf16String::to_utf8() const
|
||||||
|
{
|
||||||
|
return view().to_utf8(Utf16View::AllowInvalidCodeUnits::Yes);
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 Utf16String::code_unit_at(size_t index) const
|
||||||
|
{
|
||||||
|
return view().code_unit_at(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Utf16String::length_in_code_units() const
|
||||||
|
{
|
||||||
|
return view().length_in_code_units();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Utf16String::is_empty() const
|
||||||
|
{
|
||||||
|
return view().is_empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
61
Userland/Libraries/LibJS/Runtime/Utf16String.h
Normal file
61
Userland/Libraries/LibJS/Runtime/Utf16String.h
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/NonnullRefPtr.h>
|
||||||
|
#include <AK/RefCounted.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/Types.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
namespace Detail {
|
||||||
|
|
||||||
|
class Utf16StringImpl : public RefCounted<Utf16StringImpl> {
|
||||||
|
public:
|
||||||
|
~Utf16StringImpl() = default;
|
||||||
|
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> create();
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> create(Vector<u16>);
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> create(StringView const&);
|
||||||
|
static NonnullRefPtr<Utf16StringImpl> create(Utf16View const&);
|
||||||
|
|
||||||
|
Vector<u16> const& string() const;
|
||||||
|
Utf16View view() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Utf16StringImpl() = default;
|
||||||
|
explicit Utf16StringImpl(Vector<u16> string);
|
||||||
|
|
||||||
|
Vector<u16> m_string;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Utf16String {
|
||||||
|
public:
|
||||||
|
Utf16String();
|
||||||
|
explicit Utf16String(Vector<u16>);
|
||||||
|
explicit Utf16String(StringView const&);
|
||||||
|
explicit Utf16String(Utf16View const&);
|
||||||
|
|
||||||
|
Vector<u16> const& string() const;
|
||||||
|
Utf16View view() const;
|
||||||
|
Utf16View substring_view(size_t code_unit_offset, size_t code_unit_length) const;
|
||||||
|
Utf16View substring_view(size_t code_unit_offset) const;
|
||||||
|
|
||||||
|
String to_utf8() const;
|
||||||
|
u16 code_unit_at(size_t index) const;
|
||||||
|
|
||||||
|
size_t length_in_code_units() const;
|
||||||
|
bool is_empty() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
NonnullRefPtr<Detail::Utf16StringImpl> m_string;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue