2020-03-22 05:12:55 -04:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-03-22 05:12:55 -04:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-22 05:12:55 -04:00
|
|
|
*/
|
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
#include <AK/ByteString.h>
|
2023-01-08 19:23:00 -05:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2020-03-22 05:12:55 -04:00
|
|
|
#include <AK/HashTable.h>
|
2020-06-12 15:07:52 -04:00
|
|
|
#include <AK/Optional.h>
|
2020-08-24 21:35:19 -04:00
|
|
|
#include <AK/Singleton.h>
|
2020-03-22 05:12:55 -04:00
|
|
|
#include <AK/StringUtils.h>
|
2020-03-23 08:45:10 -04:00
|
|
|
#include <AK/StringView.h>
|
2020-03-22 05:12:55 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2024-01-24 15:15:48 -05:00
|
|
|
struct DeprecatedFlyStringImplTraits : public Traits<StringImpl const*> {
|
|
|
|
static unsigned hash(StringImpl const* s) { return s->hash(); }
|
2022-04-01 13:58:27 -04:00
|
|
|
static bool equals(StringImpl const* a, StringImpl const* b)
|
2020-03-22 05:12:55 -04:00
|
|
|
{
|
2020-10-06 11:16:39 -04:00
|
|
|
return *a == *b;
|
2020-03-22 05:12:55 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-19 16:59:26 -05:00
|
|
|
static Singleton<HashTable<StringImpl const*, DeprecatedFlyStringImplTraits>> s_table;
|
2020-08-24 21:35:19 -04:00
|
|
|
|
2023-02-19 16:59:26 -05:00
|
|
|
static HashTable<StringImpl const*, DeprecatedFlyStringImplTraits>& fly_impls()
|
2020-03-22 05:12:55 -04:00
|
|
|
{
|
2020-08-24 21:35:19 -04:00
|
|
|
return *s_table;
|
2020-03-22 05:12:55 -04:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
void DeprecatedFlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
|
2020-03-22 05:12:55 -04:00
|
|
|
{
|
|
|
|
fly_impls().remove(&impl);
|
|
|
|
}
|
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
DeprecatedFlyString::DeprecatedFlyString(ByteString const& string)
|
2024-01-24 15:15:48 -05:00
|
|
|
: m_impl(string.impl())
|
2020-03-22 05:12:55 -04:00
|
|
|
{
|
2024-01-24 15:15:48 -05:00
|
|
|
if (string.impl()->is_fly())
|
2020-05-05 04:08:14 -04:00
|
|
|
return;
|
2024-01-24 15:15:48 -05:00
|
|
|
|
|
|
|
auto it = fly_impls().find(string.impl());
|
2020-03-22 05:12:55 -04:00
|
|
|
if (it == fly_impls().end()) {
|
2024-01-24 15:15:48 -05:00
|
|
|
fly_impls().set(string.impl());
|
2020-03-22 05:12:55 -04:00
|
|
|
string.impl()->set_fly({}, true);
|
|
|
|
m_impl = string.impl();
|
|
|
|
} else {
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY((*it)->is_fly());
|
2024-01-24 15:15:48 -05:00
|
|
|
m_impl = **it;
|
2020-03-22 05:12:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString::DeprecatedFlyString(StringView string)
|
2024-01-24 15:15:48 -05:00
|
|
|
: m_impl(StringImpl::the_empty_stringimpl())
|
2020-03-22 05:12:55 -04:00
|
|
|
{
|
2021-05-15 03:50:51 -04:00
|
|
|
if (string.is_null())
|
|
|
|
return;
|
|
|
|
auto it = fly_impls().find(string.hash(), [&](auto& candidate) {
|
2023-10-10 07:30:58 -04:00
|
|
|
return string == *candidate;
|
2021-05-15 03:50:51 -04:00
|
|
|
});
|
|
|
|
if (it == fly_impls().end()) {
|
2023-12-16 09:19:34 -05:00
|
|
|
auto new_string = string.to_byte_string();
|
2021-05-15 03:50:51 -04:00
|
|
|
fly_impls().set(new_string.impl());
|
|
|
|
new_string.impl()->set_fly({}, true);
|
|
|
|
m_impl = new_string.impl();
|
|
|
|
} else {
|
|
|
|
VERIFY((*it)->is_fly());
|
2024-01-24 15:15:48 -05:00
|
|
|
m_impl = **it;
|
2021-05-15 03:50:51 -04:00
|
|
|
}
|
2020-03-22 05:12:55 -04:00
|
|
|
}
|
|
|
|
|
2023-03-10 02:48:54 -05:00
|
|
|
bool DeprecatedFlyString::equals_ignoring_ascii_case(StringView other) const
|
2020-03-22 08:07:45 -04:00
|
|
|
{
|
2023-03-10 02:48:54 -05:00
|
|
|
return StringUtils::equals_ignoring_ascii_case(view(), other);
|
2020-03-22 08:07:45 -04:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
bool DeprecatedFlyString::starts_with(StringView str, CaseSensitivity case_sensitivity) const
|
2020-07-18 12:59:38 -04:00
|
|
|
{
|
|
|
|
return StringUtils::starts_with(view(), str, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
bool DeprecatedFlyString::ends_with(StringView str, CaseSensitivity case_sensitivity) const
|
2020-05-26 06:21:34 -04:00
|
|
|
{
|
|
|
|
return StringUtils::ends_with(view(), str, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString DeprecatedFlyString::to_lowercase() const
|
2020-03-22 14:07:02 -04:00
|
|
|
{
|
2023-12-16 09:19:34 -05:00
|
|
|
return ByteString(*m_impl).to_lowercase();
|
2020-03-22 14:07:02 -04:00
|
|
|
}
|
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
bool DeprecatedFlyString::operator==(ByteString const& other) const
|
2020-03-28 04:11:00 -04:00
|
|
|
{
|
2022-01-29 09:57:31 -05:00
|
|
|
return m_impl == other.impl() || view() == other.view();
|
2020-03-28 04:11:00 -04:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
bool DeprecatedFlyString::operator==(StringView string) const
|
2020-03-28 04:11:00 -04:00
|
|
|
{
|
2022-01-29 09:57:31 -05:00
|
|
|
return view() == string;
|
2020-03-28 04:11:00 -04:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
bool DeprecatedFlyString::operator==(char const* string) const
|
2020-03-28 04:11:00 -04:00
|
|
|
{
|
2022-01-29 09:57:31 -05:00
|
|
|
return view() == string;
|
2020-03-28 04:11:00 -04:00
|
|
|
}
|
|
|
|
|
2020-03-22 05:12:55 -04:00
|
|
|
}
|