1 #ifndef _C4_SUBSTR_HPP_
2 #define _C4_SUBSTR_HPP_
10 #include "c4/config.hpp"
11 #include "c4/error.hpp"
12 #include "c4/substr_fwd.hpp"
15 # pragma clang diagnostic push
16 # pragma clang diagnostic ignored "-Wold-style-cast"
17 #elif defined(__GNUC__)
18 # pragma GCC diagnostic push
19 # pragma GCC diagnostic ignored "-Wtype-limits"
20 # pragma GCC diagnostic ignored "-Wuseless-cast"
21 # pragma GCC diagnostic ignored "-Wold-style-cast"
37 static inline void _do_reverse(C *C4_RESTRICT first, C *C4_RESTRICT last)
56 #define C4_REQUIRE_RW(ret_type) \
57 template <typename U=C> \
58 typename std::enable_if< ! std::is_const<U>::value, ret_type>::type
86 using CC =
typename std::add_const<C>::type;
87 using NCC_ =
typename std::remove_const<C>::type;
98 enum :
size_t {
npos = (size_t)-1,
NONE = (
size_t)-1 };
102 C4_ALWAYS_INLINE
operator typename std::enable_if<!std::is_const<U>::value,
ro_substr const&>::type () const noexcept
122 C4_ALWAYS_INLINE
basic_substring& operator= (std::nullptr_t) noexcept { str =
nullptr; len = 0;
return *
this; }
124 C4_ALWAYS_INLINE
void clear() noexcept { str =
nullptr; len = 0; }
137 C4_ALWAYS_INLINE constexpr
basic_substring(C (&s_)[N]) noexcept : str(s_), len(N-1) {}
140 C4_ALWAYS_INLINE
basic_substring(C *s_,
size_t len_) noexcept : str(s_), len(len_) { C4_ASSERT(str || !len_); }
144 C4_ALWAYS_INLINE
basic_substring(C *beg_, C *end_) noexcept : str(beg_), len(
static_cast<size_t>(end_ - beg_)) { C4_ASSERT(end_ >= beg_); }
151 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
152 C4_ALWAYS_INLINE
basic_substring(U s_) noexcept : str(s_), len(s_ ? strlen(s_) : 0) {}
158 C4_ALWAYS_INLINE
void assign(C (&s_)[N]) noexcept { str = (s_); len = (N-1); }
161 C4_ALWAYS_INLINE
void assign(C *s_,
size_t len_) noexcept { str = s_; len = len_; C4_ASSERT(str || !len_); }
165 C4_ALWAYS_INLINE
void assign(C *beg_, C *end_) noexcept { C4_ASSERT(end_ >= beg_); str = (beg_); len =
static_cast<size_t>(end_ - beg_); }
172 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
173 C4_ALWAYS_INLINE
void assign(U s_) noexcept { str = (s_); len = (s_ ? strlen(s_) : 0); }
178 C4_ALWAYS_INLINE
basic_substring& operator= (C (&s_)[N]) noexcept { str = (s_); len = (N-1);
return *
this; }
185 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
186 C4_ALWAYS_INLINE
basic_substring& operator= (U s_) noexcept { str = s_; len = s_ ? strlen(s_) : 0;
return *
this; }
195 C4_ALWAYS_INLINE C4_PURE
bool has_str() const noexcept {
return ! empty() && str[0] != C(0); }
196 C4_ALWAYS_INLINE C4_PURE
bool empty() const noexcept {
return (len == 0 || str ==
nullptr); }
197 C4_ALWAYS_INLINE C4_PURE
bool not_empty() const noexcept {
return (len != 0 && str !=
nullptr); }
198 C4_ALWAYS_INLINE C4_PURE
size_t size() const noexcept {
return len; }
201 C4_ALWAYS_INLINE C4_PURE
iterator end () noexcept {
return str + len; }
206 C4_ALWAYS_INLINE C4_PURE C *
data() noexcept {
return str; }
207 C4_ALWAYS_INLINE C4_PURE C
const*
data() const noexcept {
return str; }
209 C4_ALWAYS_INLINE C4_PURE C & operator[] (
size_t i) noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
210 C4_ALWAYS_INLINE C4_PURE C
const& operator[] (
size_t i)
const noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
212 C4_ALWAYS_INLINE C4_PURE C &
front() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
213 C4_ALWAYS_INLINE C4_PURE C
const&
front() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
215 C4_ALWAYS_INLINE C4_PURE C &
back() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
216 C4_ALWAYS_INLINE C4_PURE C
const&
back() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
227 C4_XASSERT((str !=
nullptr) || len == 0);
228 if(C4_LIKELY(str !=
nullptr && len > 0))
229 return (*str != c) ? *str - c : (
static_cast<int>(len) - 1);
234 C4_PURE
int compare(
const char *C4_RESTRICT that,
size_t sz)
const noexcept
236 C4_XASSERT(that || sz == 0);
237 C4_XASSERT(str || len == 0);
238 if(C4_LIKELY(str && that))
241 const size_t min = len < sz ? len : sz;
242 for(
size_t i = 0; i < min; ++i)
243 if(str[i] != that[i])
244 return str[i] < that[i] ? -1 : 1;
255 C4_XASSERT(len == 0 && sz == 0);
258 return len < sz ? -1 : 1;
263 C4_ALWAYS_INLINE C4_PURE
bool operator== (std::nullptr_t)
const noexcept {
return str ==
nullptr; }
264 C4_ALWAYS_INLINE C4_PURE
bool operator!= (std::nullptr_t)
const noexcept {
return str !=
nullptr; }
266 C4_ALWAYS_INLINE C4_PURE
bool operator== (C
const c)
const noexcept {
return this->compare(c) == 0; }
267 C4_ALWAYS_INLINE C4_PURE
bool operator!= (C
const c)
const noexcept {
return this->compare(c) != 0; }
268 C4_ALWAYS_INLINE C4_PURE
bool operator< (C
const c)
const noexcept {
return this->compare(c) < 0; }
269 C4_ALWAYS_INLINE C4_PURE
bool operator> (C
const c)
const noexcept {
return this->compare(c) > 0; }
270 C4_ALWAYS_INLINE C4_PURE
bool operator<= (C
const c)
const noexcept {
return this->compare(c) <= 0; }
271 C4_ALWAYS_INLINE C4_PURE
bool operator>= (C
const c)
const noexcept {
return this->compare(c) >= 0; }
280 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator== (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) == 0; }
281 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator!= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) != 0; }
282 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator< (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) < 0; }
283 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator> (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) > 0; }
284 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator<= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) <= 0; }
285 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator>= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) >= 0; }
297 return that.is_super(*
this);
303 if(C4_LIKELY(len > 0))
304 return that.str >= str && that.str+that.len <= str+len;
306 return that.len == 0 && that.str == str && str !=
nullptr;
313 return that.str+that.len > str && that.str < str+len;
321 C4_ASSERT(first >= 0 && first <= len);
328 C4_ASSERT(first >= 0 && first <= len);
329 C4_ASSERT((num >= 0 && num <= len) || (num ==
npos));
330 size_t rnum = num !=
npos ? num : len - first;
331 C4_ASSERT((first >= 0 && first + rnum <= len) || (num == 0));
338 C4_ASSERT(first >= 0 && first <= len);
339 last = last !=
npos ? last : len;
340 C4_ASSERT(first <= last);
341 C4_ASSERT(last >= 0 && last <= len);
348 C4_ASSERT(num <= len || num ==
npos);
355 C4_ASSERT(num <= len || num ==
npos);
366 C4_ASSERT(
left >= 0 &&
left <= len);
375 C4_ASSERT(pos <= len || pos ==
npos);
376 return (pos !=
npos) ?
384 C4_ASSERT(pos <= len || pos ==
npos);
385 return (pos !=
npos) ?
393 C4_ASSERT(pos <= len || pos ==
npos);
394 return (pos !=
npos) ?
402 C4_ASSERT(pos <= len || pos ==
npos);
403 return (pos !=
npos) ?
404 basic_substring(str + (pos + !include_pos), len - (pos + !include_pos)) :
414 C4_ASSERT(is_super(subs) || subs.empty());
415 auto ssb = subs.begin();
418 if(ssb >= b && ssb <= e)
419 return sub(0,
static_cast<size_t>(ssb - b));
428 C4_ASSERT(is_super(subs) || subs.empty());
429 auto sse = subs.end();
432 if(sse >= b && sse <= e)
433 return sub(
static_cast<size_t>(sse - b),
static_cast<size_t>(e - sse));
450 size_t pos = first_not_of(c);
462 size_t pos = first_not_of(chars);
474 size_t pos = last_not_of(c,
npos);
476 return sub(0, pos+1);
486 size_t pos = last_not_of(chars,
npos);
488 return sub(0, pos+1);
496 return triml(c).
trimr(c);
502 return triml(chars).
trimr(chars);
509 if( ! begins_with(pattern))
511 return sub(pattern.
len < len ? pattern.
len : len);
518 if( ! ends_with(pattern))
520 return left_of(len - (pattern.
len < len ? pattern.
len : len));
530 size_t find(
const C c,
size_t start_pos=0)
const
532 return first_of(c, start_pos);
536 C4_ASSERT(start_pos ==
npos || (start_pos >= 0 && start_pos <= len));
537 if(len < pattern.
len)
return npos;
538 for(
size_t i = start_pos, e = len - pattern.
len + 1; i < e; ++i)
541 for(
size_t j = 0; j < pattern.
len; ++j)
543 C4_ASSERT(i + j < len);
544 if(str[i + j] != pattern.
str[j])
561 size_t count(
const C c,
size_t pos=0)
const
563 C4_ASSERT(pos >= 0 && pos <= len);
569 pos = find(c, pos + 1);
577 C4_ASSERT(pos >= 0 && pos <= len);
583 pos = find(c, pos + c.
len);
598 pos = find(pattern, pos);
608 operator bool()
const {
return which !=
NONE && pos !=
npos; }
614 return first_of_any_iter(&s[0], &s[0] + 2);
620 return first_of_any_iter(&s[0], &s[0] + 3);
626 return first_of_any_iter(&s[0], &s[0] + 4);
632 return first_of_any_iter(&s[0], &s[0] + 5);
638 for(
size_t i = 0; i < len; ++i)
641 for(It it = first_span; it != last_span; ++curr, ++it)
643 auto const& chars = *it;
644 if((i + chars.len) > len)
continue;
646 for(
size_t j = 0; j < chars.len; ++j)
648 C4_ASSERT(i + j < len);
649 if(str[i + j] != chars[j])
669 return len > 0 ? str[0] == c :
false;
679 for(
size_t i = 0; i < num; ++i)
692 if(len < pattern.
len)
696 for(
size_t i = 0; i < pattern.
len; ++i)
698 if(str[i] != pattern[i])
713 for(
size_t i = 0; i < chars.
len; ++i)
715 if(str[0] == chars.
str[i])
726 return len > 0 ? str[len-1] == c :
false;
736 for(
size_t i = len - num; i < len; ++i)
749 if(len < pattern.
len)
753 for(
size_t i = 0, s = len-pattern.
len; i < pattern.
len; ++i)
755 if(str[s+i] != pattern[i])
770 for(
size_t i = 0; i < chars.
len; ++i)
772 if(str[len - 1] == chars[i])
785 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
786 for(
size_t i = start; i < len; ++i)
797 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
800 for(
size_t i = start-1; i != size_t(-1); --i)
811 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
812 for(
size_t i = start; i < len; ++i)
814 for(
size_t j = 0; j < chars.
len; ++j)
816 if(str[i] == chars[j])
826 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
829 for(
size_t i = start-1; i != size_t(-1); --i)
831 for(
size_t j = 0; j < chars.
len; ++j)
833 if(str[i] == chars[j])
844 for(
size_t i = 0; i < len; ++i)
854 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
855 for(
size_t i = start; i < len; ++i)
865 for(
size_t i = len-1; i != size_t(-1); --i)
875 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
878 for(
size_t i = start-1; i != size_t(-1); --i)
888 for(
size_t i = 0; i < len; ++i)
891 for(
size_t j = 0; j < chars.
len; ++j)
893 if(str[i] == chars.
str[j])
909 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
910 for(
size_t i = start; i < len; ++i)
913 for(
size_t j = 0; j < chars.
len; ++j)
915 if(str[i] == chars.
str[j])
931 for(
size_t i = len-1; i != size_t(-1); --i)
934 for(
size_t j = 0; j < chars.
len; ++j)
936 if(str[i] == chars.
str[j])
952 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
955 for(
size_t i = start-1; i != size_t(-1); --i)
958 for(
size_t j = 0; j < chars.
len; ++j)
960 if(str[i] == chars.
str[j])
986 size_t b = find(open);
989 size_t e = find(close, b+1);
1001 size_t b = find(open_close);
1003 for(
size_t i = b+1; i < len; ++i)
1008 if(str[i-1] != escape)
1010 return range(b, i+1);
1022 size_t b = find(open);
1024 size_t e, curr = b+1, count = 0;
1025 const char both[] = {open, close,
'\0'};
1026 while((e = first_of(both, curr)) !=
npos)
1033 else if(str[e] == close)
1035 if(count == 0)
return range(b, e+1);
1045 constexpr
const C dq(
'"'), sq(
'\'');
1046 if(len >= 2 && (str[len - 2] != C(
'\\')) &&
1047 ((begins_with(sq) && ends_with(sq))
1049 (begins_with(dq) && ends_with(dq))))
1051 return range(1, len -1);
1067 if(empty() || (first_non_empty_span().empty()))
1069 if(first_uint_span() == *
this)
1071 if(first_int_span() == *
this)
1073 if(first_real_span() == *
this)
1082 if(empty() || (first_non_empty_span().empty()))
1084 if(first_real_span() == *
this)
1093 if(empty() || (first_non_empty_span().empty()))
1095 if(first_uint_span() == *
this)
1097 if(first_int_span() == *
this)
1106 if(empty() || (first_non_empty_span().empty()))
1108 if(first_uint_span() == *
this)
1116 constexpr
const ro_substr empty_chars(
" \n\r\t");
1117 size_t pos = first_not_of(empty_chars);
1120 auto ret = sub(pos);
1121 pos = ret.first_of(empty_chars);
1122 return ret.first(pos);
1131 if(ne.
str[0] ==
'-')
1133 size_t skip_start = size_t(ne.
str[0] ==
'+');
1143 size_t skip_start = size_t(ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1149 C4_ASSERT(!empty());
1150 if(skip_start == len)
1152 C4_ASSERT(skip_start < len);
1153 if(len >= skip_start + 3)
1155 if(str[skip_start] !=
'0')
1157 for(
size_t i = skip_start; i < len; ++i)
1160 if(c < '0' || c >
'9')
1161 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1166 char next = str[skip_start + 1];
1167 if(next ==
'x' || next ==
'X')
1170 for(
size_t i = skip_start; i < len; ++i)
1172 const char c = str[i];
1173 if( ! _is_hex_char(c))
1174 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1178 else if(next ==
'b' || next ==
'B')
1181 for(
size_t i = skip_start; i < len; ++i)
1183 const char c = str[i];
1184 if(c !=
'0' && c !=
'1')
1185 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1189 else if(next ==
'o' || next ==
'O')
1192 for(
size_t i = skip_start; i < len; ++i)
1194 const char c = str[i];
1195 if(c < '0' || c >
'7')
1196 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1203 for(
size_t i = skip_start; i < len; ++i)
1205 const char c = str[i];
1206 if(c < '0' || c >
'9')
1207 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1218 const size_t skip_start = (ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1219 C4_ASSERT(skip_start == 0 || skip_start == 1);
1224 if(ne.
len >= skip_start+3)
1227 if(ne.
str[skip_start] !=
'0')
1229 if(ne.
str[skip_start] ==
'i')
1236 else if(ne.
str[skip_start] ==
'n')
1247 const char next = ne.
str[skip_start + 1];
1249 if(next ==
'x' || next ==
'X')
1252 else if(next ==
'b' || next ==
'B')
1255 else if(next ==
'o' || next ==
'O')
1270 return c ==
' ' || c ==
'\n'
1271 || c ==
']' || c ==
')' || c ==
'}'
1272 || c ==
',' || c ==
';' || c ==
'\r' || c ==
'\t' || c ==
'\0';
1276 static constexpr C4_ALWAYS_INLINE C4_CONST
bool _is_hex_char(
char c) noexcept
1278 return (c >=
'0' && c <=
'9') || (c >=
'a' && c <=
'f') || (c >=
'A' && c <=
'F');
1283 size_t posend = pos + word.
len;
1284 if(len >= posend && sub(pos, word.len) == word)
1285 if(len == posend || _is_delim_char(str[posend]))
1286 return first(posend);
1293 bool intchars =
false;
1294 bool fracchars =
false;
1297 for( ; pos < len; ++pos)
1299 const char c = str[pos];
1300 if(c >=
'0' && c <=
'9')
1307 goto fractional_part_dec;
1309 else if(c ==
'e' || c ==
'E')
1312 goto power_part_dec;
1314 else if(_is_delim_char(c))
1316 return intchars ? first(pos) : first(0);
1328 fractional_part_dec:
1330 C4_ASSERT(str[pos - 1] ==
'.');
1331 for( ; pos < len; ++pos)
1333 const char c = str[pos];
1334 if(c >=
'0' && c <=
'9')
1338 else if(c ==
'e' || c ==
'E')
1341 goto power_part_dec;
1343 else if(_is_delim_char(c))
1345 return intchars || fracchars ? first(pos) : first(0);
1352 return intchars || fracchars ?
1357 C4_ASSERT(str[pos - 1] ==
'e' || str[pos - 1] ==
'E');
1359 if((len == pos) || ((!intchars) && (!fracchars)))
1361 if(str[pos] ==
'-' || str[pos] ==
'+')
1364 for( ; pos < len; ++pos)
1366 const char c = str[pos];
1367 if(c >=
'0' && c <=
'9')
1369 else if(powchars && _is_delim_char(c))
1374 return powchars ? *this : first(0);
1380 bool intchars =
false;
1381 bool fracchars =
false;
1384 for( ; pos < len; ++pos)
1386 const char c = str[pos];
1394 goto fractional_part_hex;
1396 else if(c ==
'p' || c ==
'P')
1399 goto power_part_hex;
1401 else if(_is_delim_char(c))
1403 return intchars ? first(pos) : first(0);
1415 fractional_part_hex:
1417 C4_ASSERT(str[pos - 1] ==
'.');
1418 for( ; pos < len; ++pos)
1420 const char c = str[pos];
1425 else if(c ==
'p' || c ==
'P')
1428 goto power_part_hex;
1430 else if(_is_delim_char(c))
1432 return intchars || fracchars ? first(pos) : first(0);
1439 return intchars || fracchars ?
1444 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1448 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1454 for( ; pos < len; ++pos)
1456 const char c = str[pos];
1457 if(c >=
'0' && c <=
'9')
1459 else if(powchars && _is_delim_char(c))
1470 bool intchars =
false;
1471 bool fracchars =
false;
1474 for( ; pos < len; ++pos)
1476 const char c = str[pos];
1477 if(c ==
'0' || c ==
'1')
1484 goto fractional_part_bin;
1486 else if(c ==
'p' || c ==
'P')
1489 goto power_part_bin;
1491 else if(_is_delim_char(c))
1493 return intchars ? first(pos) : first(0);
1505 fractional_part_bin:
1507 C4_ASSERT(str[pos - 1] ==
'.');
1508 for( ; pos < len; ++pos)
1510 const char c = str[pos];
1511 if(c ==
'0' || c ==
'1')
1515 else if(c ==
'p' || c ==
'P')
1518 goto power_part_bin;
1520 else if(_is_delim_char(c))
1522 return intchars || fracchars ? first(pos) : first(0);
1529 return intchars || fracchars ?
1534 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1538 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1544 for( ; pos < len; ++pos)
1546 const char c = str[pos];
1547 if(c >=
'0' && c <=
'9')
1549 else if(powchars && _is_delim_char(c))
1560 bool intchars =
false;
1561 bool fracchars =
false;
1564 for( ; pos < len; ++pos)
1566 const char c = str[pos];
1567 if(c >=
'0' && c <=
'7')
1574 goto fractional_part_oct;
1576 else if(c ==
'p' || c ==
'P')
1579 goto power_part_oct;
1581 else if(_is_delim_char(c))
1583 return intchars ? first(pos) : first(0);
1595 fractional_part_oct:
1597 C4_ASSERT(str[pos - 1] ==
'.');
1598 for( ; pos < len; ++pos)
1600 const char c = str[pos];
1601 if(c >=
'0' && c <=
'7')
1605 else if(c ==
'p' || c ==
'P')
1608 goto power_part_oct;
1610 else if(_is_delim_char(c))
1612 return intchars || fracchars ? first(pos) : first(0);
1619 return intchars || fracchars ?
1624 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1628 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1634 for( ; pos < len; ++pos)
1636 const char c = str[pos];
1637 if(c >=
'0' && c <=
'9')
1639 else if(powchars && _is_delim_char(c))
1660 if(C4_LIKELY(*start_pos < len))
1662 for(
size_t i = *start_pos; i < len; i++)
1666 out->assign(str + *start_pos, i - *start_pos);
1671 out->assign(str + *start_pos, len - *start_pos);
1672 *start_pos = len + 1;
1677 bool valid = len > 0 && (*start_pos == len);
1678 if(valid && str && str[len-1] == sep)
1680 out->assign(str + len,
size_t(0));
1684 out->assign(str + len + 1,
size_t(0));
1686 *start_pos = len + 1;
1693 struct split_proxy_impl
1703 : m_proxy(proxy), m_pos(pos), m_sep(sep)
1710 m_proxy->m_str.next_split(m_sep, &m_pos, &m_str);
1725 C4_XASSERT((m_sep == that.
m_sep) &&
"cannot compare split iterators with different separators");
1730 return m_pos == that.
m_pos;
1739 : m_str(str_), m_start_pos(start_pos), m_sep(sep)
1743 split_iterator_impl begin()
const
1745 auto it = split_iterator_impl(
this, m_start_pos, m_sep);
1748 split_iterator_impl end()
const
1750 size_t pos = m_str.
size() + 1;
1751 auto it = split_iterator_impl(
this, pos, m_sep);
1763 C4_XASSERT((start_pos >= 0 && start_pos < len) || empty());
1764 auto ss = sub(0, len);
1776 if(C4_LIKELY(len > 1))
1778 auto pos = last_of(sep);
1783 return sub(pos + 1);
1789 return sub(pos + 1, 0);
1791 auto ppos = last_not_of(sep);
1797 auto pos0 = last_of(sep, ppos);
1813 if(begins_with(sep))
1829 if(C4_LIKELY(len > 1))
1831 auto pos = first_of(sep);
1844 auto ppos = first_not_of(sep);
1850 auto pos0 = first_of(sep, ppos);
1855 C4_XASSERT(pos0 > 0);
1857 return sub(0, pos0);
1867 if(begins_with(sep))
1884 auto ss = pop_right(sep, skip_empty);
1886 if(ss.find(sep) !=
npos)
1888 if(ss.ends_with(sep))
1896 ss = ss.sub(0, ss.len-1);
1906 auto ss = pop_left(sep, skip_empty);
1908 if(ss.find(sep) !=
npos)
1910 if(ss.begins_with(sep))
1934 auto ss = pop_right(sep,
true);
1941 auto ss = basename(sep);
1942 ss = ss.empty() ? *this : left_of(ss);
1948 return gpop_left(
'.');
1953 return pop_left(
'.');
1958 return pop_right(
'.');
1963 return gpop_right(
'.');
1977 for(
size_t i = 0; i < len; ++i)
1979 str[i] =
static_cast<C
>(::toupper(str[i]));
1987 for(
size_t i = 0; i < len; ++i)
1989 str[i] =
static_cast<C
>(::tolower(str[i]));
1999 for(
size_t i = 0; i < len; ++i)
2011 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2012 num = num !=
npos ? num : len - ifirst;
2013 num = num < that.
len ? num : that.
len;
2014 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2019 memcpy(str +
sizeof(C) * ifirst, that.
str,
sizeof(C) * num);
2028 if(len == 0)
return;
2029 detail::_do_reverse(str, str + len - 1);
2036 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2037 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2038 if(num == 0)
return;
2039 detail::_do_reverse(str + ifirst, str + ifirst + num - 1);
2046 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2047 C4_ASSERT(ilast >= 0 && ilast <= len);
2048 if(ifirst == ilast)
return;
2049 detail::_do_reverse(str + ifirst, str + ilast - 1);
2059 C4_ASSERT(pos >= 0 && pos+num <= len);
2060 size_t num_to_move = len - pos - num;
2061 memmove(str + pos, str + pos + num,
sizeof(C) * num_to_move);
2068 C4_ASSERT(first <= last);
2069 return erase(first,
static_cast<size_t>(last-first));
2077 C4_ASSERT(is_super(sub));
2078 C4_ASSERT(sub.
str >= str);
2079 return erase(
static_cast<size_t>(sub.
str - str), sub.
len);
2087 C4_REQUIRE_RW(
size_t)
replace(C value, C repl,
size_t pos=0)
2089 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2091 while((pos = find(value, pos)) !=
npos)
2105 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2107 while((pos = first_of(chars, pos)) !=
npos)
2123 C4_ASSERT( ! pattern.
empty());
2124 C4_ASSERT( !
this ->overlaps(dst));
2125 C4_ASSERT( ! pattern.
overlaps(dst));
2126 C4_ASSERT( ! repl .overlaps(dst));
2127 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2128 C4_SUPPRESS_WARNING_GCC_PUSH
2129 C4_SUPPRESS_WARNING_GCC(
"-Warray-bounds")
2130 #if (!defined(__clang__)) && (defined(__GNUC__) && (__GNUC__ >= 7))
2131 C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
2133 #define _c4append(first, last) \
2135 C4_ASSERT((last) >= (first)); \
2136 size_t num = static_cast<size_t>((last) - (first)); \
2137 if(num > 0 && sz + num <= dst.len) \
2139 memcpy(dst.str + sz, first, num * sizeof(C)); \
2147 size_t e = find(pattern, b);
2155 b = e + pattern.
size();
2156 }
while(b < len && b !=
npos);
2159 C4_SUPPRESS_WARNING_GCC_POP
2167 #undef C4_REQUIRE_RW
2184 C4_ALWAYS_INLINE substr
to_substr(substr s) noexcept {
return s; }
2186 C4_ALWAYS_INLINE csubstr
to_csubstr(substr s) noexcept {
return s; }
2188 C4_ALWAYS_INLINE csubstr
to_csubstr(csubstr s) noexcept {
return s; }
2192 C4_ALWAYS_INLINE substr
2193 to_substr(
char (&s)[N]) noexcept { substr ss(s, N-1);
return ss; }
2195 C4_ALWAYS_INLINE csubstr
2196 to_csubstr(
const char (&s)[N]) noexcept { csubstr ss(s, N-1);
return ss; }
2203 C4_ALWAYS_INLINE
typename std::enable_if<std::is_same<U, char*>::value, substr>::type
2209 C4_ALWAYS_INLINE
typename std::enable_if<std::is_same<U, const char*>::value || std::is_same<U, char*>::value, csubstr>::type
2225 template<
typename C,
size_t N>
inline bool operator< (
const char (&s)[N],
basic_substring<C> const that) noexcept {
return that.compare(s, N-1) > 0; }
2226 template<
typename C,
size_t N>
inline bool operator> (
const char (&s)[N],
basic_substring<C> const that) noexcept {
return that.compare(s, N-1) < 0; }
2247 #ifndef C4_SUBSTR_NO_OSTREAM_LSHIFT
2249 # pragma clang diagnostic push
2250 # pragma clang diagnostic ignored "-Wsign-conversion"
2251 #elif defined(__GNUC__)
2252 # pragma GCC diagnostic push
2253 # pragma GCC diagnostic ignored "-Wsign-conversion"
2257 template<
class OStream,
class C>
2273 # pragma clang diagnostic pop
2274 #elif defined(__GNUC__)
2275 # pragma GCC diagnostic pop
2285 # pragma clang diagnostic pop
2286 #elif defined(__GNUC__)
2287 # pragma GCC diagnostic pop
left_< T > left(T val, size_t width, char padchar=' ')
mark an argument to be aligned left
right_< T > right(T val, size_t width, char padchar=' ')
mark an argument to be aligned right
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
substr to_substr(substr s) noexcept
neutral version for use in generic code
bool operator!=(const char(&s)[N], basic_substring< C > const that) noexcept
bool operator>(const char(&s)[N], basic_substring< C > const that) noexcept
bool operator>=(const char(&s)[N], basic_substring< C > const that) noexcept
bool operator<=(const char(&s)[N], basic_substring< C > const that) noexcept
bool operator==(const char(&s)[N], basic_substring< C > const that) noexcept
bool operator<(const char(&s)[N], basic_substring< C > const that) noexcept
OStream & operator<<(OStream &os, basic_substring< C > s)
output the string to a stream
@ npos
a null string position
split_proxy_impl const * m_proxy
split_iterator_impl(split_proxy_impl const *proxy, size_t pos, C sep)
a non-owning string-view, consisting of a character pointer and a length.
basic_substring _first_real_span_hex(size_t pos) const noexcept
void reverse()
reverse in place
basic_substring first_uint_span() const
get the first span which can be interpreted as an unsigned integer
first_of_any_result first_of_any_iter(It first_span, It last_span) const
int compare(ro_substr const that) const noexcept
basic_substring(U s_) noexcept
Construct from a C-string (zero-terminated string)
size_t first_not_of(ro_substr chars) const
basic_substring gpop_right(C sep=C('/'), bool skip_empty=false) const
greedy pop right.
basic_substring trim(const C c) const
trim the character c left and right
C const & front() const noexcept
size_t count(const C c, size_t pos=0) const
count the number of occurrences of c
basic_substring _first_real_span_oct(size_t pos) const noexcept
bool begins_with(const C c) const
true if the first character of the string is c
first_of_any_result first_of_any(ro_substr s0, ro_substr s1) const
basic_substring sub(size_t first, size_t num) const noexcept
return [first,first+num[.
basic_substring pair_range(CC open, CC close) const
get the range delimited by an open-close pair of characters.
basic_substring pop_right(C sep=C('/'), bool skip_empty=false) const
pop right: return the first split from the right.
basic_substring range(size_t first, size_t last=npos) const noexcept
return [first,last[.
int compare(C const c) const noexcept
C const & back() const noexcept
size_t first_not_of(const C c) const
const_iterator begin() const noexcept
size_t last_not_of(const C c, size_t start) const
basic_substring left_of(ro_substr const subs) const noexcept
given subs a substring of the current string, get the portion of the current string to the left of it
basic_substring triml(const C c) const
trim left
bool ends_with(const C c) const
true if the last character of the string is c
size_t last_of(const C c, size_t start=npos) const
void tolower()
convert the string to lower-case
basic_substring trimr(ro_substr chars) const
trim right ANY of the characters
basic_substring(basic_substring const &) noexcept=default
basic_substring _first_real_span_bin(size_t pos) const noexcept
void toupper()
convert the string to upper-case
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3, ro_substr s4) const
basic_substring offs(size_t left, size_t right) const noexcept
offset from the ends: return [left,len-right[ ; ie, trim a number of characters from the left and rig...
basic_substring first_real_span() const
get the first span which can be interpreted as a real (floating-point) number
basic_substring unquoted() const
split_proxy_impl split_proxy
basic_substring first_int_span() const
get the first span which can be interpreted as a signed integer
basic_substring select(ro_substr pattern, size_t pos=0) const
get the substr consisting of the first occurrence of pattern after pos, or an empty substr if none oc...
basic_substring pair_range_esc(CC open_close, CC escape=CC('\\'))
get the range delimited by a single open-close character (eg, quotes).
size_t replace_all(rw_substr dst, ro_substr pattern, ro_substr repl, size_t pos=0) const
replace pattern with repl, and write the result into dst.
size_t count(ro_substr c, size_t pos=0) const
count the number of occurrences of s
basic_substring name_wo_extshort() const
split_proxy split(C sep, size_t start_pos=0) const
a view into the splits
basic_substring name_wo_extlong() const
basic_substring erase(ro_substr sub)
erase a part of the string.
size_t last_not_of(ro_substr chars) const
constexpr basic_substring() noexcept
basic_substring triml(ro_substr chars) const
trim left ANY of the characters.
size_t len
the length of the substring
basic_substring left_of(size_t pos, bool include_pos) const noexcept
return [0, pos+include_pos[ .
size_t last_not_of(ro_substr chars, size_t start) const
basic_substring last(size_t num) const noexcept
return the last num elements: [len-num,len[
size_t first_of(const C c, size_t start=0) const
basic_substring stripl(ro_substr pattern) const
remove a pattern from the left
bool ends_with(ro_substr pattern) const
true if the string ends with the given pattern
basic_substring right_of(size_t pos, bool include_pos) const noexcept
return [pos+!include_pos, len[
size_t find(const C c, size_t start_pos=0) const
basic_substring trim(ro_substr const chars) const
trim left and right ANY of the characters
const_iterator end() const noexcept
typename std::add_const< C >::type CC
CC=const char.
basic_substring(C *s_, size_t len_) noexcept
Construct from a pointer and length.
basic_substring erase_range(size_t first, size_t last)
basic_substring extshort() const
void assign(U s_) noexcept
Assign from a C-string (zero-terminated string)
basic_substring stripr(ro_substr pattern) const
remove a pattern from the right
size_t first_of(ro_substr chars, size_t start=0) const
size_t find(ro_substr pattern, size_t start_pos=0) const
void assign(C *s_, size_t len_) noexcept
Assign from a pointer and length.
iterator begin() noexcept
void assign(C(&s_)[N]) noexcept
Assign from an array.
bool ends_with_any(ro_substr chars) const
true if the last character of the string is any of the given chars
void fill(C val)
fill the entire contents with the given val
size_t size() const noexcept
basic_substring basename(C sep=C('/')) const
basic_substring(C *beg_, C *end_) noexcept
Construct from two pointers.
bool is_unsigned_integer() const
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
basic_substring _first_integral_span(size_t skip_start) const
bool not_empty() const noexcept
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
basic_substring left_of(size_t pos) const noexcept
return [0, pos[ .
void reverse_range(size_t ifirst, size_t ilast)
revert a range in place
basic_substring pair_range_nested(CC open, CC close) const
get the range delimited by an open-close pair of characters, with possibly nested occurrences.
C const * data() const noexcept
size_t replace(C value, C repl, size_t pos=0)
replace every occurrence of character value with the character repl
bool has_str() const noexcept
bool begins_with(ro_substr pattern) const
true if the string begins with the given pattern
basic_substring sub(size_t first) const noexcept
return [first,len[
basic_substring _first_real_span_dec(size_t pos) const noexcept
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2) const
bool begins_with_any(ro_substr chars) const
true if the first character of the string is any of the given chars
bool next_split(C sep, size_t *start_pos, basic_substring *out) const
returns true if the string has not been exhausted yet, meaning it's ok to call next_split() again.
basic_substring(basic_substring &&) noexcept=default
bool empty() const noexcept
basic_substring right_of(ro_substr const subs) const noexcept
given subs a substring of the current string, get the portion of the current string to the right of i...
size_t first_not_of(const C c, size_t start) const
void assign(C *beg_, C *end_) noexcept
Assign from two pointers.
basic_substring trimr(const C c) const
trim the character c from the right
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3) const
int compare(const char *that, size_t sz) const noexcept
constexpr basic_substring(C(&s_)[N]) noexcept
Construct from an array.
basic_substring dirname(C sep=C('/')) const
size_t replace(ro_substr chars, C repl, size_t pos=0)
replace every occurrence of each character in value with the character repl.
bool is_super(ro_substr const that) const noexcept
true if that is a substring of *this (ie, from the same buffer)
size_t last_not_of(const C c) const
bool ends_with(const C c, size_t num) const
true if the last num characters of the string are c
bool begins_with(const C c, size_t num) const
true if the first num characters of the string are c
static constexpr C4_CONST bool _is_hex_char(char c) noexcept
true if the character is in [0-9a-fA-F]
basic_substring erase(size_t pos, size_t num)
erase part of the string.
basic_substring _word_follows(size_t pos, csubstr word) const noexcept
void reverse_sub(size_t ifirst, size_t num)
revert a subpart in place
basic_substring gpop_left(C sep=C('/'), bool skip_empty=false) const
greedy pop left.
size_t first_not_of(ro_substr chars, size_t start) const
size_t last_of(ro_substr chars, size_t start=npos) const
void copy_from(ro_substr that, size_t ifirst=0, size_t num=npos)
set the current substring to a copy of the given csubstr
typename std::remove_const< C >::type NCC_
NCC_=non const char.
basic_substring first_non_empty_span() const
get the first span consisting exclusively of non-empty characters
C * str
a restricted pointer to the first character of the substring
basic_substring right_of(size_t pos) const noexcept
return [pos+1, len[
basic_substring pop_left(C sep=C('/'), bool skip_empty=false) const
return the first split from the left.
static constexpr C4_CONST bool _is_delim_char(char c) noexcept
true if the character is a delimiter character at the end
bool is_sub(ro_substr const that) const noexcept
true if *this is a substring of that (ie, from the same buffer)
basic_substring select(const C c, size_t pos=0) const
get the substr consisting of the first occurrence of c after pos, or an empty substr if none occurs
basic_substring extlong() const
#define _c4append(first, last)