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