C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
zstring_view.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#if __has_include(<compare>)
26#include <compare>
27#endif
28
29#include <string>
30#include <string_view>
31
32namespace essence {
33 template <typename CharT, typename Traits = std::char_traits<CharT>>
35 public:
36 using underlying_type = std::basic_string_view<CharT, Traits>;
37 using traits_type = typename underlying_type::traits_type;
38 using value_type = typename underlying_type::value_type;
39 using pointer = typename underlying_type::pointer;
40 using const_pointer = typename underlying_type::const_pointer;
41 using reference = typename underlying_type::reference;
42 using const_reference = typename underlying_type::const_reference;
43 using const_iterator = typename underlying_type::const_iterator;
44 using iterator = typename underlying_type::iterator;
45 using const_reverse_iterator = typename underlying_type::const_reverse_iterator;
46 using reverse_iterator = typename underlying_type::reverse_iterator;
47 using size_type = typename underlying_type::size_type;
48 using difference_type = typename underlying_type::difference_type;
49
50 static constexpr size_type npos = underlying_type::npos;
51
52 constexpr basic_zstring_view() noexcept = default;
53 constexpr basic_zstring_view(const basic_zstring_view&) noexcept = default;
54 constexpr basic_zstring_view(const CharT* str) : view_{str} {} // NOLINT(*-explicit-constructor)
55 constexpr basic_zstring_view(const CharT* str, size_type size) : view_{str, size} {}
56
57 template <typename Allocator>
58 constexpr basic_zstring_view( // NOLINT(*-explicit-constructor)
59 const std::basic_string<CharT, Traits, Allocator>& str)
60 : view_{str} {}
61
62#ifndef __ANDROID__
63 template <typename Iter, typename End>
64 constexpr basic_zstring_view(Iter first, End last) : view_{first, last} {}
65#endif
66
67 constexpr explicit basic_zstring_view(std::nullptr_t) = delete;
68
69 constexpr operator underlying_type() const noexcept { // NOLINT(*-explicit-constructor)
70 return view_;
71 }
72
73 constexpr const_iterator begin() const noexcept {
74 return view_.begin();
75 }
76
77 constexpr const_iterator cbegin() const noexcept {
78 return view_.cbegin();
79 }
80
81 constexpr const_iterator end() const noexcept {
82 return view_.end();
83 }
84
85 constexpr const_iterator cend() const noexcept {
86 return view_.cend();
87 }
88
89 constexpr const_reverse_iterator rbegin() const noexcept {
90 return view_.rbegin();
91 }
92
93 constexpr const_reverse_iterator crbegin() const noexcept {
94 return view_.crbegin();
95 }
96
97 constexpr const_reverse_iterator rend() const noexcept {
98 return view_.rend();
99 }
100
101 constexpr const_reverse_iterator crend() const noexcept {
102 return view_.crend();
103 }
104
105 constexpr const_reference operator[](size_type index) const {
106 return view_[index];
107 }
108
109 constexpr const_reference at(size_type index) const {
110 return view_.at(index);
111 }
112
113 constexpr const_reference front() const {
114 return view_.front();
115 }
116
117 constexpr const_reference back() const {
118 return view_.back();
119 }
120
121 constexpr const_pointer data() const noexcept {
122 return view_.data();
123 }
124
125 constexpr const_pointer c_str() const noexcept {
126 return view_.data();
127 }
128
129 constexpr size_type size() const noexcept {
130 return view_.size();
131 }
132
133 constexpr size_type length() const noexcept {
134 return view_.length();
135 }
136
137 constexpr size_type max_size() const noexcept {
138 return view_.max_size();
139 }
140
141 [[nodiscard]] constexpr bool empty() const noexcept {
142 return view_.empty();
143 }
144
145 constexpr size_type find(basic_zstring_view view, size_type offset = 0) const noexcept {
146 return view_.find(view, offset);
147 }
148
149 constexpr size_type find(CharT ch, size_type offset = 0) const noexcept {
150 return view_.find(ch, offset);
151 }
152
153 constexpr size_type find(const CharT* str, size_type offset, size_type count) const {
154 return view_.find(str, offset, count);
155 }
156
157 constexpr size_type find(const CharT* str, size_type offset = 0) const {
158 return view_.rfind(str, offset);
159 }
160
161 constexpr size_type rfind(basic_zstring_view view, size_type offset = npos) const noexcept {
162 return view_.rfind(view, offset);
163 }
164
165 constexpr size_type rfind(CharT ch, size_type offset = npos) const noexcept {
166 return view_.rfind(ch, offset);
167 }
168
169 constexpr size_type rfind(const CharT* str, size_type offset, size_type count) const {
170 return view_.rfind(str, offset, count);
171 }
172
173 constexpr size_type rfind(const CharT* str, size_type offset = npos) const {
174 return view_.rfind(str, offset);
175 }
176
177 constexpr size_type find_first_of(basic_zstring_view view, size_type offset = 0) const noexcept {
178 return view_.find_first_of(view, offset);
179 }
180
181 constexpr size_type find_first_of(CharT ch, size_type offset = 0) const noexcept {
182 return view_.find_first_of(ch, offset);
183 }
184
185 constexpr size_type find_first_of(const CharT* str, size_type offset, size_type count) const {
186 return view_.find_first_of(str, offset, count);
187 }
188
189 constexpr size_type find_first_of(const CharT* str, size_type offset = 0) const {
190 return view_.find_first_of(str, offset);
191 }
192
193 constexpr size_type find_last_of(basic_zstring_view view, size_type offset = npos) const noexcept {
194 return view_.find_last_of(view, offset);
195 }
196
197 constexpr size_type find_last_of(CharT ch, size_type offset = npos) const noexcept {
198 return view_.find_last_of(ch, offset);
199 }
200
201 constexpr size_type find_last_of(const CharT* str, size_type offset, size_type count) const {
202 return view_.find_last_of(str, offset, count);
203 }
204
205 constexpr size_type find_last_of(const CharT* str, size_type offset = npos) const {
206 return view_.find_last_of(str, offset);
207 }
208
209 constexpr size_type find_first_not_of(basic_zstring_view view, size_type offset = 0) const noexcept {
210 return view_.find_first_not_of(view, offset);
211 }
212
213 constexpr size_type find_first_not_of(CharT ch, size_type offset = 0) const noexcept {
214 return view_.find_first_not_of(ch, offset);
215 }
216
217 constexpr size_type find_first_not_of(const CharT* str, size_type offset, size_type count) const {
218 return view_.find_first_not_of(str, offset, count);
219 }
220
221 constexpr size_type find_first_not_of(const CharT* str, size_type offset = 0) const {
222 return view_.find_first_not_of(str, offset);
223 }
224
225 constexpr size_type find_last_not_of(basic_zstring_view view, size_type offset = npos) const noexcept {
226 return view_.find_first_not_of(view, offset);
227 }
228
229 constexpr size_type find_last_not_of(CharT ch, size_type offset = npos) const noexcept {
230 return view_.find_first_not_of(ch, offset);
231 }
232
233 constexpr size_type find_last_not_of(const CharT* str, size_type offset, size_type count) const {
234 return view_.find_first_not_of(str, offset, count);
235 }
236
237 constexpr size_type find_last_not_of(const CharT* str, size_type offset = npos) const {
238 return view_.find_first_not_of(str, offset);
239 }
240
241 template <typename OtherTraits>
242 friend constexpr bool operator==(
244 return left.view_ == right.view_;
245 }
246
247#if __has_include(<compare>)
248 template <typename OtherTraits>
249 friend constexpr auto operator<=>(
251 return left.view_ <=> right.view_;
252 }
253#endif
254
255 private:
256 underlying_type view_;
257 };
258
264} // namespace essence
Definition zstring_view.hpp:34
Definition type_list.hpp:61