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