chrono_parse
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
34#if MGUTILITY_CPLUSPLUS >= 201703L
35#include <string_view>
36#endif
37
38namespace mgutility {
39
40#if MGUTILITY_CPLUSPLUS < 201703L
41
42namespace detail {
50constexpr auto strlen_constexpr(const char *str, size_t sz = 0) noexcept
51 -> size_t {
52 return str[sz] == '\0' ? sz : strlen_constexpr(str, ++sz);
53}
54} // namespace detail
55
61template <typename Char = char> class basic_string_view {
62public:
66 constexpr inline basic_string_view() noexcept : data_(""), size_(0) {}
67
73 constexpr inline basic_string_view(const Char *str) noexcept
74 : data_(str), size_(detail::strlen_constexpr(str)) {}
75
81 constexpr inline basic_string_view(
82 const std::basic_string<Char> &str) noexcept
83 : data_(str.c_str()), size_(str.size()) {}
84
91 constexpr inline basic_string_view(const Char *str, size_t len) noexcept
92 : data_(str), size_(len) {}
93
99 constexpr inline basic_string_view(const basic_string_view &other)
100 : data_(other.data_), size_(other.size_) {}
101
107 constexpr inline basic_string_view(basic_string_view &&other) noexcept
108 : data_(std::move(other.data_)), size_(std::move(other.size_)) {}
109
116 MGUTILITY_CNSTXPR inline basic_string_view<Char> &
117 operator=(const basic_string_view &other) noexcept {
118 data_ = other.data_;
119 size_ = other.size_;
120 return *this;
121 }
122
129 MGUTILITY_CNSTXPR inline basic_string_view<Char> &
130 operator=(basic_string_view &&other) noexcept {
131 data_ = std::move(other.data_);
132 size_ = std::move(other.size_);
133 return *this;
134 }
135
142 constexpr inline const Char operator[](size_t index) const noexcept {
143 return data_[index];
144 }
145
151 constexpr inline const Char *begin() const noexcept { return data_; }
152
158 constexpr inline const Char *end() const noexcept { return (data_ + size_); }
159
165 constexpr inline bool empty() const noexcept { return size_ < 1; }
166
172 constexpr inline size_t size() const noexcept { return size_; }
173
179 constexpr inline const Char *data() const noexcept { return data_; }
180
188 constexpr inline basic_string_view<Char>
189 substr(size_t begin, size_t len = 0U) const noexcept {
190 return basic_string_view<Char>(data_ + begin,
191 len == 0U ? size_ - begin : len);
192 }
193
201 constexpr inline size_t rfind(Char c, size_t pos = npos) const noexcept {
202 return (pos == npos ? pos = size_ : pos = pos), c == data_[pos] ? pos
203 : pos == 0U
204 ? npos
205 : rfind(c, --pos);
206 }
207
215 constexpr inline size_t find(Char c, size_t pos = 0) const noexcept {
216 return c == data_[pos] ? pos : pos < size_ ? find(c, ++pos) : npos;
217 }
218
226 constexpr friend inline bool
228 basic_string_view<Char> rhs) noexcept {
229 return (lhs.size_ == rhs.size_) &&
230 std::strncmp(lhs.data_, rhs.data_, lhs.size_) == 0;
231 }
232
240 constexpr friend inline bool operator==(basic_string_view<Char> lhs,
241 const Char *rhs) noexcept {
242 return (lhs.size_ == detail::strlen_constexpr(rhs)) &&
243 std::strncmp(lhs.data_, rhs, lhs.size_) == 0;
244 }
245
253 constexpr friend inline bool
255 basic_string_view<Char> rhs) noexcept {
256 return !(lhs == rhs);
257 }
258
266 constexpr friend inline bool operator!=(basic_string_view<Char> lhs,
267 const Char *rhs) noexcept {
268 return !(lhs == rhs);
269 }
270
276 inline operator std::string() { return std::string(data_, size_); }
277
283 inline operator std::string() const { return std::string(data_, size_); }
284
292 friend inline std::ostream &operator<<(std::ostream &os,
294 for (auto c : sv) {
295 os << c;
296 }
297 return os;
298 }
299
300 static constexpr auto npos = -1;
301
302private:
303 size_t size_;
304 const Char *data_;
305};
306
307using string_view = basic_string_view<char>;
308
309#else
310
311using string_view = std::string_view;
312
313#endif
314
315} // namespace mgutility
316
317#endif // STRING_STRING_VIEW_HPP
A basic string view class template.
Definition string_view.hpp:61
constexpr basic_string_view(const Char *str) noexcept
Constructs a basic_string_view from a C-string.
Definition string_view.hpp:73
constexpr const Char * end() const noexcept
Returns an iterator to the end of the string.
Definition string_view.hpp:158
constexpr friend bool operator!=(basic_string_view< Char > lhs, basic_string_view< Char > rhs) noexcept
Inequality operator.
Definition string_view.hpp:254
constexpr basic_string_view(const basic_string_view &other)
Copy constructor.
Definition string_view.hpp:99
constexpr size_t size() const noexcept
Returns the size of the string.
Definition string_view.hpp:172
constexpr bool empty() const noexcept
Checks if the string is empty.
Definition string_view.hpp:165
friend std::ostream & operator<<(std::ostream &os, const basic_string_view< Char > &sv)
Stream insertion operator.
Definition string_view.hpp:292
constexpr basic_string_view(basic_string_view &&other) noexcept
Move constructor.
Definition string_view.hpp:107
constexpr friend bool operator==(basic_string_view< Char > lhs, const Char *rhs) noexcept
Equality operator.
Definition string_view.hpp:240
constexpr friend bool operator==(basic_string_view< Char > lhs, basic_string_view< Char > rhs) noexcept
Equality operator.
Definition string_view.hpp:227
constexpr size_t find(Char c, size_t pos=0) const noexcept
Finds the first occurrence of a character.
Definition string_view.hpp:215
MGUTILITY_CNSTXPR basic_string_view< Char > & operator=(const basic_string_view &other) noexcept
Copy assignment operator.
Definition string_view.hpp:117
constexpr const Char operator[](size_t index) const noexcept
Accesses the character at the given index.
Definition string_view.hpp:142
constexpr const Char * begin() const noexcept
Returns an iterator to the beginning of the string.
Definition string_view.hpp:151
constexpr basic_string_view() noexcept
Default constructor.
Definition string_view.hpp:66
constexpr size_t rfind(Char c, size_t pos=npos) const noexcept
Finds the last occurrence of a character.
Definition string_view.hpp:201
constexpr const Char * data() const noexcept
Returns a pointer to the underlying data.
Definition string_view.hpp:179
constexpr basic_string_view(const std::basic_string< Char > &str) noexcept
Constructs a basic_string_view from a std::string.
Definition string_view.hpp:81
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:91
constexpr friend bool operator!=(basic_string_view< Char > lhs, const Char *rhs) noexcept
Inequality operator.
Definition string_view.hpp:266
MGUTILITY_CNSTXPR basic_string_view< Char > & operator=(basic_string_view &&other) noexcept
Move assignment operator.
Definition string_view.hpp:130
constexpr basic_string_view< Char > substr(size_t begin, size_t len=0U) const noexcept
Returns a substring view.
Definition string_view.hpp:189
Defines macros for compiler and standard support detection.