2020-03-22 09:12:55 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-03-22 09:12:55 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-22 09:12:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2023-01-02 14:15:14 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2020-03-22 09:12:55 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
class DeprecatedFlyString {
|
2020-03-22 09:12:55 +00:00
|
|
|
public:
|
2024-01-24 20:15:48 +00:00
|
|
|
DeprecatedFlyString()
|
|
|
|
: m_impl(StringImpl::the_empty_stringimpl())
|
|
|
|
{
|
|
|
|
}
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString(DeprecatedFlyString const& other)
|
2020-05-05 08:08:14 +00:00
|
|
|
: m_impl(other.impl())
|
|
|
|
{
|
|
|
|
}
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString(DeprecatedFlyString&& other)
|
2020-05-05 08:08:14 +00:00
|
|
|
: m_impl(move(other.m_impl))
|
|
|
|
{
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
DeprecatedFlyString(ByteString const&);
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString(StringView);
|
|
|
|
DeprecatedFlyString(char const* string)
|
2023-12-16 14:19:34 +00:00
|
|
|
: DeprecatedFlyString(static_cast<ByteString>(string))
|
2021-06-02 23:35:01 +00:00
|
|
|
{
|
|
|
|
}
|
2020-03-22 09:12:55 +00:00
|
|
|
|
2023-02-19 21:59:26 +00:00
|
|
|
static DeprecatedFlyString from_fly_impl(NonnullRefPtr<StringImpl const> impl)
|
2021-06-13 10:00:01 +00:00
|
|
|
{
|
|
|
|
VERIFY(impl->is_fly());
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString string;
|
2021-06-13 10:00:01 +00:00
|
|
|
string.m_impl = move(impl);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString& operator=(DeprecatedFlyString const& other)
|
2020-05-05 08:08:14 +00:00
|
|
|
{
|
|
|
|
m_impl = other.m_impl;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString& operator=(DeprecatedFlyString&& other)
|
2020-05-05 08:08:14 +00:00
|
|
|
{
|
|
|
|
m_impl = move(other.m_impl);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-01-24 20:15:48 +00:00
|
|
|
bool is_empty() const { return !m_impl->length(); }
|
2020-03-24 13:03:42 +00:00
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
bool operator==(DeprecatedFlyString const& other) const { return m_impl == other.m_impl; }
|
2020-03-22 09:12:55 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool operator==(ByteString const&) const;
|
2020-03-28 08:11:00 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool operator==(StringView) const;
|
2020-03-28 08:11:00 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(char const*) const;
|
2020-03-28 08:11:00 +00:00
|
|
|
|
2024-01-24 20:15:48 +00:00
|
|
|
NonnullRefPtr<StringImpl const> impl() const { return m_impl; }
|
|
|
|
char const* characters() const { return m_impl->characters(); }
|
|
|
|
size_t length() const { return m_impl->length(); }
|
2020-03-22 09:12:55 +00:00
|
|
|
|
2024-01-24 20:15:48 +00:00
|
|
|
ALWAYS_INLINE u32 hash() const { return m_impl->existing_hash(); }
|
|
|
|
ALWAYS_INLINE StringView view() const { return m_impl->view(); }
|
2020-03-22 09:12:55 +00:00
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString to_lowercase() const;
|
2020-03-22 18:07:02 +00:00
|
|
|
|
2023-12-23 01:09:17 +00:00
|
|
|
template<Arithmetic T>
|
|
|
|
Optional<T> to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const
|
|
|
|
{
|
|
|
|
return view().to_number<T>(trim_whitespace);
|
|
|
|
}
|
|
|
|
|
2023-03-10 07:48:54 +00:00
|
|
|
bool equals_ignoring_ascii_case(StringView) const;
|
2021-11-10 23:55:02 +00:00
|
|
|
bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-03-22 12:07:45 +00:00
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
static void did_destroy_impl(Badge<StringImpl>, StringImpl&);
|
|
|
|
|
2021-06-04 09:46:29 +00:00
|
|
|
template<typename... Ts>
|
2024-10-26 22:37:14 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
2020-05-25 10:36:41 +00:00
|
|
|
{
|
2021-06-04 09:46:29 +00:00
|
|
|
return (... || this->operator==(forward<Ts>(strings)));
|
2020-05-25 10:36:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:12:55 +00:00
|
|
|
private:
|
2024-01-24 20:15:48 +00:00
|
|
|
NonnullRefPtr<StringImpl const> m_impl;
|
2020-03-22 09:12:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<DeprecatedFlyString> : public DefaultTraits<DeprecatedFlyString> {
|
2023-01-09 00:23:00 +00:00
|
|
|
static unsigned hash(DeprecatedFlyString const& s) { return s.hash(); }
|
2020-03-22 09:12:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-01-09 00:23:00 +00:00
|
|
|
using AK::DeprecatedFlyString;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|