/src/serenity/Userland/Libraries/LibJS/Runtime/Utf16String.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/StringView.h> |
8 | | #include <LibJS/Runtime/Utf16String.h> |
9 | | #include <LibJS/Runtime/VM.h> |
10 | | |
11 | | namespace JS { |
12 | | namespace Detail { |
13 | | |
14 | | static NonnullRefPtr<Utf16StringImpl> the_empty_utf16_string() |
15 | 0 | { |
16 | 0 | static NonnullRefPtr<Utf16StringImpl> empty_string = Utf16StringImpl::create(); |
17 | 0 | return empty_string; |
18 | 0 | } |
19 | | |
20 | | Utf16StringImpl::Utf16StringImpl(Utf16Data string) |
21 | 0 | : m_string(move(string)) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create() |
26 | 0 | { |
27 | 0 | return adopt_ref(*new Utf16StringImpl); |
28 | 0 | } |
29 | | |
30 | | NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Utf16Data string) |
31 | 0 | { |
32 | 0 | return adopt_ref(*new Utf16StringImpl(move(string))); |
33 | 0 | } |
34 | | |
35 | | NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(StringView string) |
36 | 0 | { |
37 | 0 | return create(MUST(utf8_to_utf16(string))); |
38 | 0 | } |
39 | | |
40 | | NonnullRefPtr<Utf16StringImpl> Utf16StringImpl::create(Utf16View const& view) |
41 | 0 | { |
42 | 0 | Utf16Data string; |
43 | 0 | string.ensure_capacity(view.length_in_code_units()); |
44 | 0 | string.unchecked_append(view.data(), view.length_in_code_units()); |
45 | 0 | return create(move(string)); |
46 | 0 | } |
47 | | |
48 | | Utf16Data const& Utf16StringImpl::string() const |
49 | 0 | { |
50 | 0 | return m_string; |
51 | 0 | } |
52 | | |
53 | | Utf16View Utf16StringImpl::view() const |
54 | 0 | { |
55 | 0 | return Utf16View { m_string }; |
56 | 0 | } |
57 | | |
58 | | u32 Utf16StringImpl::compute_hash() const |
59 | 0 | { |
60 | 0 | if (m_string.is_empty()) |
61 | 0 | return 0; |
62 | 0 | return string_hash((char const*)m_string.data(), m_string.size() * sizeof(u16)); |
63 | 0 | } |
64 | | |
65 | | } |
66 | | |
67 | | Utf16String Utf16String::create() |
68 | 0 | { |
69 | 0 | return Utf16String { Detail::the_empty_utf16_string() }; |
70 | 0 | } |
71 | | |
72 | | Utf16String Utf16String::create(Utf16Data string) |
73 | 0 | { |
74 | 0 | return Utf16String { Detail::Utf16StringImpl::create(move(string)) }; |
75 | 0 | } |
76 | | |
77 | | Utf16String Utf16String::create(StringView string) |
78 | 0 | { |
79 | 0 | return Utf16String { Detail::Utf16StringImpl::create(string) }; |
80 | 0 | } |
81 | | |
82 | | Utf16String Utf16String::create(Utf16View const& string) |
83 | 0 | { |
84 | 0 | return Utf16String { Detail::Utf16StringImpl::create(string) }; |
85 | 0 | } |
86 | | |
87 | | Utf16String::Utf16String(NonnullRefPtr<Detail::Utf16StringImpl> string) |
88 | 0 | : m_string(move(string)) |
89 | 0 | { |
90 | 0 | } |
91 | | |
92 | | Utf16Data const& Utf16String::string() const |
93 | 0 | { |
94 | 0 | return m_string->string(); |
95 | 0 | } |
96 | | |
97 | | Utf16View Utf16String::view() const |
98 | 0 | { |
99 | 0 | return m_string->view(); |
100 | 0 | } |
101 | | |
102 | | Utf16View Utf16String::substring_view(size_t code_unit_offset, size_t code_unit_length) const |
103 | 0 | { |
104 | 0 | return view().substring_view(code_unit_offset, code_unit_length); |
105 | 0 | } |
106 | | |
107 | | Utf16View Utf16String::substring_view(size_t code_unit_offset) const |
108 | 0 | { |
109 | 0 | return view().substring_view(code_unit_offset); |
110 | 0 | } |
111 | | |
112 | | String Utf16String::to_utf8() const |
113 | 0 | { |
114 | 0 | return MUST(view().to_utf8(Utf16View::AllowInvalidCodeUnits::Yes)); |
115 | 0 | } |
116 | | |
117 | | ByteString Utf16String::to_byte_string() const |
118 | 0 | { |
119 | 0 | return MUST(view().to_byte_string(Utf16View::AllowInvalidCodeUnits::Yes)); |
120 | 0 | } |
121 | | |
122 | | u16 Utf16String::code_unit_at(size_t index) const |
123 | 0 | { |
124 | 0 | return view().code_unit_at(index); |
125 | 0 | } |
126 | | |
127 | | size_t Utf16String::length_in_code_units() const |
128 | 0 | { |
129 | 0 | return view().length_in_code_units(); |
130 | 0 | } |
131 | | |
132 | | bool Utf16String::is_empty() const |
133 | 0 | { |
134 | 0 | return view().is_empty(); |
135 | 0 | } |
136 | | |
137 | | } |