Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/StringUtils.h>
11
12namespace AK {
13
14class DeprecatedFlyString {
15public:
16 DeprecatedFlyString() = default;
17 DeprecatedFlyString(DeprecatedFlyString const& other)
18 : m_impl(other.impl())
19 {
20 }
21 DeprecatedFlyString(DeprecatedFlyString&& other)
22 : m_impl(move(other.m_impl))
23 {
24 }
25 DeprecatedFlyString(DeprecatedString const&);
26 DeprecatedFlyString(StringView);
27 DeprecatedFlyString(char const* string)
28 : DeprecatedFlyString(static_cast<DeprecatedString>(string))
29 {
30 }
31
32 static DeprecatedFlyString from_fly_impl(NonnullRefPtr<StringImpl const> impl)
33 {
34 VERIFY(impl->is_fly());
35 DeprecatedFlyString string;
36 string.m_impl = move(impl);
37 return string;
38 }
39
40 DeprecatedFlyString& operator=(DeprecatedFlyString const& other)
41 {
42 m_impl = other.m_impl;
43 return *this;
44 }
45
46 DeprecatedFlyString& operator=(DeprecatedFlyString&& other)
47 {
48 m_impl = move(other.m_impl);
49 return *this;
50 }
51
52 bool is_empty() const { return !m_impl || !m_impl->length(); }
53 bool is_null() const { return !m_impl; }
54
55 bool operator==(DeprecatedFlyString const& other) const { return m_impl == other.m_impl; }
56
57 bool operator==(DeprecatedString const&) const;
58
59 bool operator==(StringView) const;
60
61 bool operator==(char const*) const;
62
63 StringImpl const* impl() const { return m_impl; }
64 char const* characters() const { return m_impl ? m_impl->characters() : nullptr; }
65 size_t length() const { return m_impl ? m_impl->length() : 0; }
66
67 ALWAYS_INLINE u32 hash() const { return m_impl ? m_impl->existing_hash() : 0; }
68 ALWAYS_INLINE StringView view() const { return m_impl ? m_impl->view() : StringView {}; }
69
70 DeprecatedFlyString to_lowercase() const;
71
72 template<typename T = int>
73 Optional<T> to_int(TrimWhitespace = TrimWhitespace::Yes) const;
74 template<typename T = unsigned>
75 Optional<T> to_uint(TrimWhitespace = TrimWhitespace::Yes) const;
76#ifndef KERNEL
77 Optional<double> to_double(TrimWhitespace = TrimWhitespace::Yes) const;
78 Optional<float> to_float(TrimWhitespace = TrimWhitespace::Yes) const;
79#endif
80
81 bool equals_ignoring_ascii_case(StringView) const;
82 bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
83 bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
84
85 static void did_destroy_impl(Badge<StringImpl>, StringImpl&);
86
87 template<typename... Ts>
88 [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts... strings) const
89 {
90 return (... || this->operator==(forward<Ts>(strings)));
91 }
92
93private:
94 RefPtr<StringImpl const> m_impl;
95};
96
97template<>
98struct Traits<DeprecatedFlyString> : public GenericTraits<DeprecatedFlyString> {
99 static unsigned hash(DeprecatedFlyString const& s) { return s.hash(); }
100};
101
102}
103
104#if USING_AK_GLOBALLY
105using AK::DeprecatedFlyString;
106#endif