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
|
|
|
|
|
2021-06-18 19:21:27 +03:00
|
|
|
#include "AK/StringUtils.h"
|
2020-03-22 12:12:55 +03:00
|
|
|
#include <AK/String.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class FlyString {
|
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
FlyString() = default;
|
2022-04-01 20:58:27 +03:00
|
|
|
FlyString(FlyString const& other)
|
2020-05-05 11:08:14 +03:00
|
|
|
: m_impl(other.impl())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
FlyString(FlyString&& other)
|
|
|
|
: m_impl(move(other.m_impl))
|
|
|
|
{
|
|
|
|
}
|
2022-04-01 20:58:27 +03:00
|
|
|
FlyString(String const&);
|
2021-11-11 02:55:02 +03:00
|
|
|
FlyString(StringView);
|
2022-04-01 20:58:27 +03:00
|
|
|
FlyString(char const* string)
|
2021-06-03 02:35:01 +03:00
|
|
|
: FlyString(static_cast<String>(string))
|
|
|
|
{
|
|
|
|
}
|
2020-03-22 12:12:55 +03:00
|
|
|
|
2021-06-13 13:00:01 +03:00
|
|
|
static FlyString from_fly_impl(NonnullRefPtr<StringImpl> impl)
|
|
|
|
{
|
|
|
|
VERIFY(impl->is_fly());
|
|
|
|
FlyString string;
|
|
|
|
string.m_impl = move(impl);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
FlyString& operator=(FlyString const& other)
|
2020-05-05 11:08:14 +03:00
|
|
|
{
|
|
|
|
m_impl = other.m_impl;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlyString& operator=(FlyString&& other)
|
|
|
|
{
|
|
|
|
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; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(FlyString const& other) const { return m_impl == other.m_impl; }
|
2020-03-22 12:12:55 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(String 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
|
|
|
|
2020-03-22 21:07:02 +03:00
|
|
|
FlyString to_lowercase() const;
|
|
|
|
|
2020-12-10 16:17:30 +03:00
|
|
|
template<typename T = int>
|
2021-06-18 19:21:27 +03:00
|
|
|
Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
|
2020-12-10 16:17:30 +03:00
|
|
|
template<typename T = unsigned>
|
2021-06-18 19:21:27 +03:00
|
|
|
Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
|
2022-10-11 01:48:45 +03:00
|
|
|
#ifndef KERNEL
|
|
|
|
Optional<double> to_double(TrimWhitespace = TrimWhitespace::Yes) const;
|
|
|
|
Optional<float> to_float(TrimWhitespace = TrimWhitespace::Yes) const;
|
|
|
|
#endif
|
2020-03-22 12:12:55 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
bool equals_ignoring_case(StringView) const;
|
|
|
|
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:
|
|
|
|
RefPtr<StringImpl> m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<FlyString> : public GenericTraits<FlyString> {
|
2022-04-01 20:58:27 +03:00
|
|
|
static unsigned hash(FlyString 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
|
2020-03-22 12:12:55 +03:00
|
|
|
using AK::FlyString;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|