1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUD_STRING_VIEW_HPP
#define FUD_STRING_VIEW_HPP
#include "fud_assert.hpp"
#include "fud_config.hpp"
#include "fud_status.hpp"
#include "fud_utf8.hpp"
#include <string_view>
namespace fud {
class String;
struct StringView {
template <size_t N>
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
consteval StringView(const utf8 (&input)[N]) : m_length{N - 1}, m_data{input}
{
static_assert(N > 0);
}
constexpr StringView() noexcept = default;
constexpr StringView(const StringView& rhs) noexcept = default;
constexpr StringView(StringView&& rhs) noexcept = default;
constexpr ~StringView() noexcept = default;
constexpr StringView& operator=(const StringView& rhs) = default;
constexpr StringView& operator=(StringView&& rhs) = default;
constexpr StringView(size_t strLen, const utf8* strData) : m_length(strLen), m_data{strData}
{
}
template <size_t N>
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
StringView(const char (&input)[N]) :
m_length{N - 1},
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
m_data{reinterpret_cast<const utf8*>(input)}
{
static_assert(N > 0);
}
StringView(size_t strLen, const char* strData) :
m_length(strLen),
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
m_data{reinterpret_cast<const utf8*>(strData)}
{
}
StringView(std::string_view rhs) noexcept : StringView(rhs.length(), rhs.data())
{
}
explicit StringView(const String& fudString) noexcept;
template <size_t N>
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
constexpr static StringView makeFromCString(const char (&input)[N])
{
static_assert(N > 0);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return StringView{N - 1, reinterpret_cast<const utf8*>(input)};
}
[[nodiscard]] std::string_view as_string_view() const {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return std::string_view{reinterpret_cast<const char*>(m_data), m_length};
}
[[nodiscard]] constexpr Span<const utf8> asSpan() const
{
return Span<const utf8>{m_data, m_length};
}
[[nodiscard]] constexpr size_t length() const
{
return m_length;
}
[[nodiscard]] constexpr const utf8* data() const
{
return m_data;
}
[[nodiscard]] const char* c_str() const
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<const char*>(m_data);
}
constexpr const utf8& operator[](size_t index) const
{
if constexpr (fudBoundsChecking) {
fudAssert(m_data != nullptr);
fudAssert(index < m_length);
}
return m_data[index];
}
[[nodiscard]] constexpr const utf8* begin() const noexcept
{
return m_data;
}
[[nodiscard]] constexpr const utf8* end() const noexcept
{
return m_data + m_length;
}
[[nodiscard]] bool nullTerminated() const;
[[nodiscard]] bool utf8Valid() const;
Result<size_t, FudStatus> skipWhitespace();
Result<size_t, FudStatus> trimWhitespace();
[[nodiscard]] bool advance();
void advanceUnsafe();
[[nodiscard]] bool advance(size_t size);
void advanceUnsafe(size_t size);
size_t m_length{0};
const utf8* m_data{nullptr};
// auto operator<=>(const StringView& rhs) const noexcept = default;
auto operator<=>(const StringView& rhs) const noexcept;
bool operator==(const StringView& rhs) const noexcept;
Result<std::strong_ordering, FudStatus> compare(const StringView& rhs);
};
} // namespace fud
#endif
|