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