2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef GIMBAL_STRING_VIEW_HPP
19#define GIMBAL_STRING_VIEW_HPP
22#include "../containers/gimbal_generics.hpp"
36std::optional<T> string_view_to_value(
const StringView& view) {
41
42
43
44
45
46
54 const char& getElement_(std::size_t idx)
const {
return data()[idx]; }
55 std::size_t getElementCount_()
const noexcept {
return length(); }
57 auto spaceShip_(
int result)
const noexcept {
59 return std::strong_ordering::less;
61 return std::strong_ordering::greater;
63 return std::strong_ordering::equal;
66 constexpr StringView()
noexcept:
69 constexpr StringView(
const char* pStr, size_t size=0)
noexcept:
73 static_cast<GblStringView*>(
this)->length = std::char_traits<
char>::length(pStr);
76 static_cast<GblStringView*>(
this)->length = size;
81 constexpr StringView(
const StringView& rhs)
noexcept:
87 constexpr StringView(std::string_view view)
noexcept:
90 constexpr const char* data()
const noexcept {
return pData; }
91 constexpr std::size_t length()
const noexcept {
return static_cast<
const GblStringView*>(
this)->length; }
92 constexpr bool isNullTerminated()
const noexcept {
return nullTerminated; }
94 int compareIgnoreCase(
const char* pString, std::size_t len=0)
const noexcept {
95 return GblStringView_compareIgnoreCase(*
this, pString, len);
98 bool equalsIgnoreCase(
const char* pString, std::size_t len=0)
const noexcept {
99 return GblStringView_equalsIgnoreCase(*
this, pString, len);
103 void copy(
void* pDst, std::size_t offset, std::size_t bytes)
const noexcept {
104 GblStringView_copy(*
this, pDst, offset, bytes);
107 bool empty()
const noexcept {
108 return GblStringView_empty(*
this);
111 bool isBlank()
const noexcept {
112 return GblStringView_blank(*
this);
115 constexpr char operator[](std::size_t index)
const {
116 if(index >= length())
117 throw std::out_of_range(
"Invalid StringView index!");
118 if (std::is_constant_evaluated()) {
119 return data()[index];
121 return GblStringView_at(*
this, index);
127 throw std::out_of_range(
"Cannot get first char of empty StringView!");
129 return GblStringView_first(*
this);
134 throw std::out_of_range(
"Cannot get last char of empty StringView!");
136 return GblStringView_last(*
this);
139 StringView removePrefix(std::size_t len)
const {
141 throw std::out_of_range(
"Cannot remove prefix length > StringView.length!");
142 return GblStringView_removePrefix(*
this, len);
146 StringView removeSuffix(std::size_t len)
const {
148 throw std::out_of_range(
"Cannot remove suffix length > StringView.length!");
149 return GblStringView_removeSuffix(*
this, len);
154 return GblStringView_chomp(*
this);
157 StringView substr(std::size_t offset, std::size_t length)
const noexcept {
158 return GblStringView_substr(*
this, offset, length);
161 bool contains(
const char* pStr, std::size_t len=0)
const noexcept {
162 return GblStringView_contains(*
this, pStr, len);
165 bool containsIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
166 return GblStringView_containsIgnoreCase(*
this, pStr, len);
169 std::size_t count(
const char* pStr, std::size_t len=0)
const noexcept {
170 return GblStringView_count(*
this, pStr, len);
173 std::size_t countIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
174 return GblStringView_countIgnoreCase(*
this, pStr, len);
177 std::size_t find(
const char* pStr, std::size_t len=0)
const noexcept {
178 return GblStringView_find(*
this, pStr, len);
181 std::size_t findIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
182 return GblStringView_findIgnoreCase(*
this, pStr, len);
185 std::size_t rfind(
const char* pStr, std::size_t len=0)
const noexcept {
186 return GblStringView_rfind(*
this, pStr, len);
189 std::size_t rfindIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
190 return GblStringView_rfindIgnoreCase(*
this, pStr, len);
193 bool startsWith(
const char* pStr, std::size_t len=0)
const noexcept {
194 return GblStringView_startsWith(*
this, pStr, len);
197 bool startsWithIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
198 return GblStringView_startsWithIgnoreCase(*
this, pStr, len);
201 bool endsWith(
const char* pStr, std::size_t len=0)
const noexcept {
202 return GblStringView_endsWith(*
this, pStr, len);
205 bool endsWithIgnoreCase(
const char* pStr, std::size_t len=0)
const noexcept {
206 return GblStringView_endsWithIgnoreCase(*
this, pStr, len);
209 std::size_t findFirstOf(
const char* pChars, std::size_t len=0, std::size_t offset=0)
const noexcept {
210 return GblStringView_findFirstOf(*
this, pChars, len, offset);
213 std::size_t findLastOf(
const char* pChars, std::size_t len=0, std::size_t offset=npos)
const noexcept {
214 return GblStringView_findLastOf(*
this, pChars, len, offset);
217 std::size_t findFirstNotOf(
const char* pChars, std::size_t len=0, std::size_t offset=0)
const noexcept {
218 return GblStringView_findFirstNotOf(*
this, pChars, len, offset);
221 std::size_t findLastNotOf(
const char* pChars, std::size_t len=0, std::size_t offset=npos)
const noexcept {
222 return GblStringView_findLastNotOf(*
this, pChars, len, offset);
225 std::string toStdString()
const {
227 str.resize(length());
228 GblStringView_toCString(*
this, str.data(), str.capacity());
232 Quark quark()
const noexcept {
233 return GblStringView_quark(*
this);
236 Quark tryQuark()
const noexcept {
237 return GblStringView_tryQuark(*
this);
240 const char* intern()
const noexcept {
241 return GblStringView_intern(*
this);
244 bool toNil()
const noexcept {
return GblStringView_toNil(*
this); }
247 auto toValue()
const noexcept{
return string_view_to_value<T>(*
this); }
249 friend std::ostream& operator<<(std::ostream &os,
StringView view) {
250 os << std::string_view(view.data(), view.length());
254 bool operator==(
const char* pStr)
const noexcept {
255 return GblStringView_equals(*
this, pStr);
284#define GBL_STRING_VIEW_TO_VALUE_(type, postfix)
286 inline std::optional<type> string_view_to_value<type>(const StringView& view) {
288 type value = GblStringView_##postfix(view, &ok);
289 return ok? std::optional{value} : std::nullopt;
305#undef GBL_STRING_VIEW_TO_VALUE_
307consteval
inline StringView operator
""_strv(
const char* pString, std::size_t size)
noexcept {
308 return StringView(pString, size);
315 std::size_t operator()(
const gbl::
StringView& view)
const noexcept {
316 return GblStringView_hash(view);
OO C++ wrapper object around GblQuark.
#define GBL_STRING_VIEW_NPOS
Constant value used to signify the last position or an invalid position in a GblStringView.
#define GBL_STRING_VIEW_TO_VALUE_(type, postfix)
Immutable substring type.
size_t nullTerminated
Reserved bit for maintaining whether the string is NULL terminated or not.
const char * pData
Start address of the string being viewed.
OO C++ binding object around GblStringView.