/src/serenity/Userland/Libraries/LibJS/Runtime/Utf16String.h
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 | | #pragma once |
8 | | |
9 | | #include <AK/ByteString.h> |
10 | | #include <AK/NonnullRefPtr.h> |
11 | | #include <AK/RefCounted.h> |
12 | | #include <AK/Types.h> |
13 | | #include <AK/Utf16View.h> |
14 | | #include <AK/Vector.h> |
15 | | #include <LibJS/Runtime/Completion.h> |
16 | | |
17 | | namespace JS { |
18 | | namespace Detail { |
19 | | |
20 | | class Utf16StringImpl : public RefCounted<Utf16StringImpl> { |
21 | | public: |
22 | 0 | ~Utf16StringImpl() = default; |
23 | | |
24 | | [[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(); |
25 | | [[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(Utf16Data); |
26 | | [[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(StringView); |
27 | | [[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(Utf16View const&); |
28 | | |
29 | | Utf16Data const& string() const; |
30 | | Utf16View view() const; |
31 | | |
32 | | [[nodiscard]] u32 hash() const |
33 | 0 | { |
34 | 0 | if (!m_has_hash) { |
35 | 0 | m_hash = compute_hash(); |
36 | 0 | m_has_hash = true; |
37 | 0 | } |
38 | 0 | return m_hash; |
39 | 0 | } |
40 | 0 | [[nodiscard]] bool operator==(Utf16StringImpl const& other) const { return string() == other.string(); } |
41 | | |
42 | | private: |
43 | 0 | Utf16StringImpl() = default; |
44 | | explicit Utf16StringImpl(Utf16Data string); |
45 | | |
46 | | [[nodiscard]] u32 compute_hash() const; |
47 | | |
48 | | mutable bool m_has_hash { false }; |
49 | | mutable u32 m_hash { 0 }; |
50 | | Utf16Data m_string; |
51 | | }; |
52 | | |
53 | | } |
54 | | |
55 | | class Utf16String { |
56 | | public: |
57 | | [[nodiscard]] static Utf16String create(); |
58 | | [[nodiscard]] static Utf16String create(Utf16Data); |
59 | | [[nodiscard]] static Utf16String create(StringView); |
60 | | [[nodiscard]] static Utf16String create(Utf16View const&); |
61 | | |
62 | | Utf16Data const& string() const; |
63 | | Utf16View view() const; |
64 | | Utf16View substring_view(size_t code_unit_offset, size_t code_unit_length) const; |
65 | | Utf16View substring_view(size_t code_unit_offset) const; |
66 | | |
67 | | [[nodiscard]] String to_utf8() const; |
68 | | [[nodiscard]] ByteString to_byte_string() const; |
69 | | u16 code_unit_at(size_t index) const; |
70 | | |
71 | | size_t length_in_code_units() const; |
72 | | bool is_empty() const; |
73 | | |
74 | 0 | [[nodiscard]] u32 hash() const { return m_string->hash(); } |
75 | | [[nodiscard]] bool operator==(Utf16String const& other) const |
76 | 0 | { |
77 | 0 | if (m_string == other.m_string) |
78 | 0 | return true; |
79 | 0 | return *m_string == *other.m_string; |
80 | 0 | } |
81 | | |
82 | | private: |
83 | | explicit Utf16String(NonnullRefPtr<Detail::Utf16StringImpl>); |
84 | | |
85 | | NonnullRefPtr<Detail::Utf16StringImpl> m_string; |
86 | | }; |
87 | | |
88 | | } |
89 | | |
90 | | namespace AK { |
91 | | |
92 | | template<> |
93 | | struct Traits<JS::Utf16String> : public DefaultTraits<JS::Utf16String> { |
94 | 0 | static unsigned hash(JS::Utf16String const& s) { return s.hash(); } |
95 | | }; |
96 | | |
97 | | } |