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-01-09 00:23:00 +00:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.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 {
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
struct DeprecatedFlyStringImplTraits : public Traits<StringImpl*> {
|
2022-04-01 17:58:27 +00:00
|
|
|
static unsigned hash(StringImpl const* s) { return s ? s->hash() : 0; }
|
|
|
|
static bool equals(StringImpl const* a, StringImpl const* b)
|
2020-03-22 09:12:55 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(a);
|
|
|
|
VERIFY(b);
|
2020-10-06 15:16:39 +00:00
|
|
|
return *a == *b;
|
2020-03-22 09:12:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
static Singleton<HashTable<StringImpl*, DeprecatedFlyStringImplTraits>> s_table;
|
2020-08-25 01:35:19 +00:00
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
static HashTable<StringImpl*, 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-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString::DeprecatedFlyString(DeprecatedString const& string)
|
2020-03-22 09:12:55 +00:00
|
|
|
{
|
|
|
|
if (string.is_null())
|
|
|
|
return;
|
2020-05-05 08:08:14 +00:00
|
|
|
if (string.impl()->is_fly()) {
|
|
|
|
m_impl = string.impl();
|
|
|
|
return;
|
|
|
|
}
|
2020-03-22 09:12:55 +00: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 19:42:32 +00:00
|
|
|
VERIFY((*it)->is_fly());
|
2020-03-22 09:12:55 +00:00
|
|
|
m_impl = *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString::DeprecatedFlyString(StringView string)
|
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) {
|
|
|
|
return string == candidate;
|
|
|
|
});
|
|
|
|
if (it == fly_impls().end()) {
|
2022-12-06 01:12:49 +00:00
|
|
|
auto new_string = string.to_deprecated_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());
|
|
|
|
m_impl = *it;
|
|
|
|
}
|
2020-03-22 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T>
|
2023-01-09 00:23:00 +00:00
|
|
|
Optional<T> DeprecatedFlyString::to_int(TrimWhitespace trim_whitespace) const
|
2020-03-22 09:12:55 +00:00
|
|
|
{
|
2021-06-18 16:21:27 +00:00
|
|
|
return StringUtils::convert_to_int<T>(view(), trim_whitespace);
|
2020-03-22 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
template Optional<i8> DeprecatedFlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i16> DeprecatedFlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i32> DeprecatedFlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i64> DeprecatedFlyString::to_int(TrimWhitespace) const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2023-01-09 00:23:00 +00:00
|
|
|
Optional<T> DeprecatedFlyString::to_uint(TrimWhitespace trim_whitespace) const
|
2020-12-10 13:17:30 +00:00
|
|
|
{
|
2021-06-18 16:21:27 +00:00
|
|
|
return StringUtils::convert_to_uint<T>(view(), trim_whitespace);
|
2020-12-10 13:17:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
template Optional<u8> DeprecatedFlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u16> DeprecatedFlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u32> DeprecatedFlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u64> DeprecatedFlyString::to_uint(TrimWhitespace) const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
2022-10-10 22:48:45 +00:00
|
|
|
#ifndef KERNEL
|
2023-01-09 00:23:00 +00:00
|
|
|
Optional<double> DeprecatedFlyString::to_double(TrimWhitespace trim_whitespace) const
|
2022-10-10 22:48:45 +00:00
|
|
|
{
|
|
|
|
return StringUtils::convert_to_floating_point<double>(view(), trim_whitespace);
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
Optional<float> DeprecatedFlyString::to_float(TrimWhitespace trim_whitespace) const
|
2022-10-10 22:48:45 +00:00
|
|
|
{
|
|
|
|
return StringUtils::convert_to_floating_point<float>(view(), trim_whitespace);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
bool DeprecatedFlyString::equals_ignoring_case(StringView other) const
|
2020-03-22 12:07:45 +00:00
|
|
|
{
|
|
|
|
return StringUtils::equals_ignoring_case(view(), other);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString(*m_impl).to_lowercase();
|
2020-03-22 18:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
bool DeprecatedFlyString::operator==(DeprecatedString 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
|
|
|
}
|