mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +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
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