enum_name
Loading...
Searching...
No Matches
string_view.hpp
1/*
2MIT License
3
4Copyright (c) 2024 mguludag
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef STRING_STRING_VIEW_HPP
26#define STRING_STRING_VIEW_HPP
27
28#include <cstring>
29#include <ostream>
30#include <string>
31
33
34namespace mgutility {
35
36#if MGUTILITY_CPLUSPLUS < 201703L
37
38namespace detail {
46constexpr auto strlen_constexpr(const char *str, size_t sz = 0) noexcept
47 -> size_t {
48 return str[sz] == '\0' ? sz : strlen_constexpr(str, ++sz);
49}
50} // namespace detail
51
57template <typename Char = char> class basic_string_view {
58public:
62 constexpr inline basic_string_view() noexcept : data_(""), size_(0) {}
63
69 constexpr inline basic_string_view(const Char *str) noexcept
70 : data_(str), size_(detail::strlen_constexpr(str)) {}
71
77 constexpr inline basic_string_view(
78 const std::basic_string<Char> &str) noexcept
79 : data_(str.c_str()), size_(str.size()) {}
80
87 constexpr inline basic_string_view(const Char *str, size_t len) noexcept
88 : data_(str), size_(len) {}
89
95 constexpr inline basic_string_view(const basic_string_view &other)
96 : data_(other.data_), size_(other.size_) {}
97
104 : data_(std::move(other.data_)), size_(std::move(other.size_)) {}
105
114 data_ = other.data_;
115 size_ = other.size_;
116 return *this;
117 }
118
127 data_ = std::move(other.data_);
128 size_ = std::move(other.size_);
129 return *this;
130 }
131
138 constexpr inline const Char operator[](size_t index) const noexcept {
139 return data_[index];
140 }
141
147 constexpr inline const Char *begin() const noexcept { return data_; }
148
154 constexpr inline const Char *end() const noexcept { return (data_ + size_); }
155
161 constexpr inline bool empty() const noexcept { return size_ < 1; }
162
168 constexpr inline size_t size() const noexcept { return size_; }
169
175 constexpr inline const Char *data() const noexcept { return data_; }
176
184 constexpr inline basic_string_view<Char>
185 substr(size_t begin, size_t len = 0U) const noexcept {
186 return basic_string_view<Char>(data_ + begin,
187 len == 0U ? size_ - begin : len);
188 }
189
197 constexpr inline size_t rfind(Char c, size_t pos = npos) const noexcept {
198 return (pos == npos ? pos = size_ : pos = pos), c == data_[pos] ? pos
199 : pos == 0U
200 ? npos
201 : rfind(c, --pos);
202 }
203
211 constexpr inline size_t find(Char c, size_t pos = 0) const noexcept {
212 return c == data_[pos] ? pos : pos < size_ ? find(c, ++pos) : npos;
213 }
214
222 constexpr friend inline bool
224 basic_string_view<Char> rhs) noexcept {
225 return (lhs.size_ == rhs.size_) &&
226 std::strncmp(lhs.data_, rhs.data_, lhs.size_) == 0;
227 }
228
236 constexpr friend inline bool operator==(basic_string_view<Char> lhs,
237 const Char *rhs) noexcept {
238 return (lhs.size_ == detail::strlen_constexpr(rhs)) &&
239 std::strncmp(lhs.data_, rhs, lhs.size_) == 0;
240 }
241
249 constexpr friend inline bool
251 basic_string_view<Char> rhs) noexcept {
252 return !(lhs == rhs);
253 }
254
262 constexpr friend inline bool operator!=(basic_string_view<Char> lhs,
263 const Char *rhs) noexcept {
264 return !(lhs == rhs);
265 }
266
272 inline operator std::string() { return std::string(data_, size_); }
273
279 inline operator std::string() const { return std::string(data_, size_); }
280
288 friend inline std::ostream &operator<<(std::ostream &os,
290 for (auto c : sv) {
291 os << c;
292 }
293 return os;
294 }
295
296 static constexpr auto npos = -1;
297
298private:
299 size_t size_;
300 const Char *data_;
301};
302
303using string_view = basic_string_view<char>;
304
305#else
306#include <string_view>
307
308using string_view = std::string_view;
309
310#endif
311
312} // namespace mgutility
313
314#endif // STRING_STRING_VIEW_HPP
A basic string view class template.
Definition string_view.hpp:57
constexpr basic_string_view(const Char *str) noexcept
Constructs a basic_string_view from a C-string.
Definition string_view.hpp:69
constexpr const Char * end() const noexcept
Returns an iterator to the end of the string.
Definition string_view.hpp:154
constexpr friend bool operator!=(basic_string_view< Char > lhs, basic_string_view< Char > rhs) noexcept
Inequality operator.
Definition string_view.hpp:250
constexpr basic_string_view(const basic_string_view &other)
Copy constructor.
Definition string_view.hpp:95
constexpr size_t size() const noexcept
Returns the size of the string.
Definition string_view.hpp:168
constexpr bool empty() const noexcept
Checks if the string is empty.
Definition string_view.hpp:161
friend std::ostream & operator<<(std::ostream &os, const basic_string_view< Char > &sv)
Stream insertion operator.
Definition string_view.hpp:288
constexpr basic_string_view(basic_string_view &&other) noexcept
Move constructor.
Definition string_view.hpp:103
constexpr friend bool operator==(basic_string_view< Char > lhs, const Char *rhs) noexcept
Equality operator.
Definition string_view.hpp:236
constexpr friend bool operator==(basic_string_view< Char > lhs, basic_string_view< Char > rhs) noexcept
Equality operator.
Definition string_view.hpp:223
constexpr size_t find(Char c, size_t pos=0) const noexcept
Finds the first occurrence of a character.
Definition string_view.hpp:211
MGUTILITY_CNSTXPR basic_string_view< Char > & operator=(const basic_string_view &other) noexcept
Copy assignment operator.
Definition string_view.hpp:113
constexpr const Char operator[](size_t index) const noexcept
Accesses the character at the given index.
Definition string_view.hpp:138
constexpr const Char * begin() const noexcept
Returns an iterator to the beginning of the string.
Definition string_view.hpp:147
constexpr basic_string_view() noexcept
Default constructor.
Definition string_view.hpp:62
constexpr size_t rfind(Char c, size_t pos=npos) const noexcept
Finds the last occurrence of a character.
Definition string_view.hpp:197
constexpr const Char * data() const noexcept
Returns a pointer to the underlying data.
Definition string_view.hpp:175
constexpr basic_string_view(const std::basic_string< Char > &str) noexcept
Constructs a basic_string_view from a std::string.
Definition string_view.hpp:77
constexpr basic_string_view(const Char *str, size_t len) noexcept
Constructs a basic_string_view from a C-string and length.
Definition string_view.hpp:87
constexpr friend bool operator!=(basic_string_view< Char > lhs, const Char *rhs) noexcept
Inequality operator.
Definition string_view.hpp:262
MGUTILITY_CNSTXPR basic_string_view< Char > & operator=(basic_string_view &&other) noexcept
Move assignment operator.
Definition string_view.hpp:126
constexpr basic_string_view< Char > substr(size_t begin, size_t len=0U) const noexcept
Returns a substring view.
Definition string_view.hpp:185
Defines macros for compiler and standard support detection.
Checks for MSVC compiler version.
Definition enum_for_each.hpp:34
Definition meta.hpp:161