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