2021-08-09 08:58:31 -04:00
|
|
|
/*
|
2023-01-06 11:00:24 -05:00
|
|
|
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
2021-08-09 08:58:31 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2021-08-09 08:58:31 -04:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/Types.h>
|
2023-01-06 11:00:24 -05:00
|
|
|
#include <AK/Utf16View.h>
|
2021-08-09 08:58:31 -04:00
|
|
|
#include <AK/Vector.h>
|
2023-01-07 13:59:10 -05:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-08-09 08:58:31 -04:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
class Utf16StringImpl : public RefCounted<Utf16StringImpl> {
|
|
|
|
public:
|
|
|
|
~Utf16StringImpl() = default;
|
|
|
|
|
2023-08-08 18:54:20 +02:00
|
|
|
[[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create();
|
|
|
|
[[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(Utf16Data);
|
|
|
|
[[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(StringView);
|
|
|
|
[[nodiscard]] static NonnullRefPtr<Utf16StringImpl> create(Utf16View const&);
|
2021-08-09 08:58:31 -04:00
|
|
|
|
2023-01-06 11:00:24 -05:00
|
|
|
Utf16Data const& string() const;
|
2021-08-09 08:58:31 -04:00
|
|
|
Utf16View view() const;
|
|
|
|
|
2024-10-24 23:02:10 +02:00
|
|
|
[[nodiscard]] u32 hash() const
|
|
|
|
{
|
|
|
|
if (!m_has_hash) {
|
|
|
|
m_hash = compute_hash();
|
|
|
|
m_has_hash = true;
|
|
|
|
}
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
[[nodiscard]] bool operator==(Utf16StringImpl const& other) const { return string() == other.string(); }
|
|
|
|
|
2021-08-09 08:58:31 -04:00
|
|
|
private:
|
|
|
|
Utf16StringImpl() = default;
|
2023-01-06 11:00:24 -05:00
|
|
|
explicit Utf16StringImpl(Utf16Data string);
|
2021-08-09 08:58:31 -04:00
|
|
|
|
2024-10-24 23:02:10 +02:00
|
|
|
[[nodiscard]] u32 compute_hash() const;
|
|
|
|
|
|
|
|
mutable bool m_has_hash { false };
|
|
|
|
mutable u32 m_hash { 0 };
|
2023-01-06 11:00:24 -05:00
|
|
|
Utf16Data m_string;
|
2021-08-09 08:58:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Utf16String {
|
|
|
|
public:
|
2023-08-08 18:54:20 +02:00
|
|
|
[[nodiscard]] static Utf16String create();
|
|
|
|
[[nodiscard]] static Utf16String create(Utf16Data);
|
|
|
|
[[nodiscard]] static Utf16String create(StringView);
|
|
|
|
[[nodiscard]] static Utf16String create(Utf16View const&);
|
2021-08-09 08:58:31 -04:00
|
|
|
|
2023-01-06 11:00:24 -05:00
|
|
|
Utf16Data const& string() const;
|
2021-08-09 08:58:31 -04:00
|
|
|
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;
|
|
|
|
|
2023-08-08 18:54:20 +02:00
|
|
|
[[nodiscard]] String to_utf8() const;
|
2023-12-16 17:49:34 +03:30
|
|
|
[[nodiscard]] ByteString to_byte_string() const;
|
2021-08-09 08:58:31 -04:00
|
|
|
u16 code_unit_at(size_t index) const;
|
|
|
|
|
|
|
|
size_t length_in_code_units() const;
|
|
|
|
bool is_empty() const;
|
|
|
|
|
2024-10-24 23:02:10 +02:00
|
|
|
[[nodiscard]] u32 hash() const { return m_string->hash(); }
|
|
|
|
[[nodiscard]] bool operator==(Utf16String const& other) const
|
|
|
|
{
|
|
|
|
if (m_string == other.m_string)
|
|
|
|
return true;
|
|
|
|
return *m_string == *other.m_string;
|
|
|
|
}
|
|
|
|
|
2021-08-09 08:58:31 -04:00
|
|
|
private:
|
2023-01-07 12:24:05 -05:00
|
|
|
explicit Utf16String(NonnullRefPtr<Detail::Utf16StringImpl>);
|
|
|
|
|
2021-08-09 08:58:31 -04:00
|
|
|
NonnullRefPtr<Detail::Utf16StringImpl> m_string;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2024-10-24 23:02:10 +02:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<JS::Utf16String> : public DefaultTraits<JS::Utf16String> {
|
|
|
|
static unsigned hash(JS::Utf16String const& s) { return s.hash(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|