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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
#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/String.h>
|
|
|
|
#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 {
|
|
|
|
|
2021-02-25 20:10:47 +00:00
|
|
|
struct FlyStringImplTraits : 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-07 19:34:11 +00:00
|
|
|
static Singleton<HashTable<StringImpl*, FlyStringImplTraits>> s_table;
|
2020-08-25 01:35:19 +00:00
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
static HashTable<StringImpl*, FlyStringImplTraits>& fly_impls()
|
|
|
|
{
|
2020-08-25 01:35:19 +00:00
|
|
|
return *s_table;
|
2020-03-22 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
|
|
|
|
{
|
|
|
|
fly_impls().remove(&impl);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
FlyString::FlyString(String 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
FlyString::FlyString(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()) {
|
|
|
|
auto new_string = string.to_string();
|
|
|
|
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>
|
2021-06-18 16:21:27 +00:00
|
|
|
Optional<T> FlyString::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
|
|
|
}
|
|
|
|
|
2021-06-18 16:21:27 +00:00
|
|
|
template Optional<i8> FlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i16> FlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i32> FlyString::to_int(TrimWhitespace) const;
|
|
|
|
template Optional<i64> FlyString::to_int(TrimWhitespace) const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2021-06-18 16:21:27 +00:00
|
|
|
Optional<T> FlyString::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
|
|
|
}
|
|
|
|
|
2021-06-18 16:21:27 +00:00
|
|
|
template Optional<u8> FlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u16> FlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u32> FlyString::to_uint(TrimWhitespace) const;
|
|
|
|
template Optional<u64> FlyString::to_uint(TrimWhitespace) const;
|
2020-12-10 13:17:30 +00:00
|
|
|
|
2022-10-10 22:48:45 +00:00
|
|
|
#ifndef KERNEL
|
|
|
|
Optional<double> FlyString::to_double(TrimWhitespace trim_whitespace) const
|
|
|
|
{
|
|
|
|
return StringUtils::convert_to_floating_point<double>(view(), trim_whitespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<float> FlyString::to_float(TrimWhitespace trim_whitespace) const
|
|
|
|
{
|
|
|
|
return StringUtils::convert_to_floating_point<float>(view(), trim_whitespace);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool FlyString::equals_ignoring_case(StringView other) const
|
2020-03-22 12:07:45 +00:00
|
|
|
{
|
|
|
|
return StringUtils::equals_ignoring_case(view(), other);
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool FlyString::starts_with(StringView str, CaseSensitivity case_sensitivity) const
|
2020-07-18 16:59:38 +00:00
|
|
|
{
|
|
|
|
return StringUtils::starts_with(view(), str, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool FlyString::ends_with(StringView str, CaseSensitivity case_sensitivity) const
|
2020-05-26 10:21:34 +00:00
|
|
|
{
|
|
|
|
return StringUtils::ends_with(view(), str, case_sensitivity);
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:07:02 +00:00
|
|
|
FlyString FlyString::to_lowercase() const
|
|
|
|
{
|
|
|
|
return String(*m_impl).to_lowercase();
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool FlyString::operator==(String 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
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool FlyString::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
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool FlyString::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
|
|
|
}
|