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
89 using CC =
typename std::add_const<C>::type;
90 using NCC_ =
typename std::remove_const<C>::type;
101 enum :
size_t {
npos = (size_t)-1,
NONE = (
size_t)-1 };
105 C4_ALWAYS_INLINE
operator typename std::enable_if<!std::is_const<U>::value,
ro_substr const&>::type () const noexcept
125 C4_ALWAYS_INLINE
basic_substring& operator= (std::nullptr_t) noexcept { str =
nullptr; len = 0;
return *
this; }
127 C4_ALWAYS_INLINE
void clear() noexcept { str =
nullptr; len = 0; }
140 C4_ALWAYS_INLINE constexpr
basic_substring(C (&s_)[N]) noexcept : str(s_), len(N-1) {}
143 C4_ALWAYS_INLINE
basic_substring(C *s_,
size_t len_) noexcept : str(s_), len(len_) { C4_ASSERT(str || !len_); }
147 C4_ALWAYS_INLINE
basic_substring(C *beg_, C *end_) noexcept : str(beg_), len(
static_cast<size_t>(end_ - beg_)) { C4_ASSERT(end_ >= beg_); }
154 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
155 C4_ALWAYS_INLINE
basic_substring(U s_) noexcept : str(s_), len(s_ ? strlen(s_) : 0) {}
161 C4_ALWAYS_INLINE
void assign(C (&s_)[N]) noexcept { str = (s_); len = (N-1); }
164 C4_ALWAYS_INLINE
void assign(C *s_,
size_t len_) noexcept { str = s_; len = len_; C4_ASSERT(str || !len_); }
168 C4_ALWAYS_INLINE
void assign(C *beg_, C *end_) noexcept { C4_ASSERT(end_ >= beg_); str = (beg_); len =
static_cast<size_t>(end_ - beg_); }
175 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
176 C4_ALWAYS_INLINE
void assign(U s_) noexcept { str = (s_); len = (s_ ? strlen(s_) : 0); }
181 C4_ALWAYS_INLINE
basic_substring& operator= (C (&s_)[N]) noexcept { str = (s_); len = (N-1);
return *
this; }
188 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
189 C4_ALWAYS_INLINE
basic_substring& operator= (U s_) noexcept { str = s_; len = s_ ? strlen(s_) : 0;
return *
this; }
198 C4_ALWAYS_INLINE C4_PURE
bool has_str() const noexcept {
return ! empty() && str[0] != C(0); }
199 C4_ALWAYS_INLINE C4_PURE
bool empty() const noexcept {
return (len == 0 || str ==
nullptr); }
200 C4_ALWAYS_INLINE C4_PURE
bool not_empty() const noexcept {
return (len != 0 && str !=
nullptr); }
201 C4_ALWAYS_INLINE C4_PURE
size_t size() const noexcept {
return len; }
204 C4_ALWAYS_INLINE C4_PURE
iterator end () noexcept {
return str + len; }
209 C4_ALWAYS_INLINE C4_PURE C *
data() noexcept {
return str; }
210 C4_ALWAYS_INLINE C4_PURE C
const*
data() const noexcept {
return str; }
212 C4_ALWAYS_INLINE C4_PURE C & operator[] (
size_t i) noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
213 C4_ALWAYS_INLINE C4_PURE C
const& operator[] (
size_t i)
const noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
215 C4_ALWAYS_INLINE C4_PURE C &
front() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
216 C4_ALWAYS_INLINE C4_PURE C
const&
front() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
218 C4_ALWAYS_INLINE C4_PURE C &
back() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
219 C4_ALWAYS_INLINE C4_PURE C
const&
back() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
230 C4_XASSERT((str !=
nullptr) || len == 0);
231 if(C4_LIKELY(str !=
nullptr && len > 0))
232 return (*str != c) ? *str - c : (
static_cast<int>(len) - 1);
237 C4_PURE
int compare(
const char *C4_RESTRICT that,
size_t sz)
const noexcept
239 C4_XASSERT(that || sz == 0);
240 C4_XASSERT(str || len == 0);
241 if(C4_LIKELY(str && that))
244 const size_t min = len < sz ? len : sz;
245 for(
size_t i = 0; i < min; ++i)
246 if(str[i] != that[i])
247 return str[i] < that[i] ? -1 : 1;
258 C4_XASSERT(len == 0 && sz == 0);
261 return len < sz ? -1 : 1;
266 C4_ALWAYS_INLINE C4_PURE
bool operator== (std::nullptr_t)
const noexcept {
return str ==
nullptr; }
267 C4_ALWAYS_INLINE C4_PURE
bool operator!= (std::nullptr_t)
const noexcept {
return str !=
nullptr; }
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; }
272 C4_ALWAYS_INLINE C4_PURE
bool operator> (C
const c)
const noexcept {
return this->compare(c) > 0; }
273 C4_ALWAYS_INLINE C4_PURE
bool operator<= (C
const c)
const noexcept {
return this->compare(c) <= 0; }
274 C4_ALWAYS_INLINE C4_PURE
bool operator>= (C
const c)
const noexcept {
return this->compare(c) >= 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; }
286 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator> (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) > 0; }
287 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator<= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) <= 0; }
288 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator>= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) >= 0; }
300 return that.is_super(*
this);
306 if(C4_LIKELY(len > 0))
307 return that.str >= str && that.str+that.len <= str+len;
309 return that.len == 0 && that.str == str && str !=
nullptr;
316 return that.str+that.len > str && that.str < str+len;
324 C4_ASSERT(first >= 0 && first <= len);
331 C4_ASSERT(first >= 0 && first <= len);
332 C4_ASSERT((num >= 0 && num <= len) || (num ==
npos));
333 size_t rnum = num !=
npos ? num : len - first;
334 C4_ASSERT((first >= 0 && first + rnum <= len) || (num == 0));
341 C4_ASSERT(first >= 0 && first <= len);
342 last = last !=
npos ? last : len;
343 C4_ASSERT(first <= last);
344 C4_ASSERT(last >= 0 && last <= len);
351 C4_ASSERT(num <= len || num ==
npos);
358 C4_ASSERT(num <= len || num ==
npos);
369 C4_ASSERT(
left >= 0 &&
left <= len);
378 C4_ASSERT(pos <= len || pos ==
npos);
379 return (pos !=
npos) ?
387 C4_ASSERT(pos <= len || pos ==
npos);
388 return (pos !=
npos) ?
396 C4_ASSERT(pos <= len || pos ==
npos);
397 return (pos !=
npos) ?
405 C4_ASSERT(pos <= len || pos ==
npos);
406 return (pos !=
npos) ?
407 basic_substring(str + (pos + !include_pos), len - (pos + !include_pos)) :
417 C4_ASSERT(is_super(subs) || subs.empty());
418 auto ssb = subs.begin();
421 if(ssb >= b && ssb <= e)
422 return sub(0,
static_cast<size_t>(ssb - b));
431 C4_ASSERT(is_super(subs) || subs.empty());
432 auto sse = subs.end();
435 if(sse >= b && sse <= e)
436 return sub(
static_cast<size_t>(sse - b),
static_cast<size_t>(e - sse));
453 size_t pos = first_not_of(c);
465 size_t pos = first_not_of(chars);
477 size_t pos = last_not_of(c,
npos);
479 return sub(0, pos+1);
489 size_t pos = last_not_of(chars,
npos);
491 return sub(0, pos+1);
499 return triml(c).
trimr(c);
505 return triml(chars).
trimr(chars);
512 if( ! begins_with(pattern))
514 return sub(pattern.
len < len ? pattern.
len : len);
521 if( ! ends_with(pattern))
523 return left_of(len - (pattern.
len < len ? pattern.
len : len));
533 inline size_t find(
const C c,
size_t start_pos=0)
const
535 return first_of(c, start_pos);
539 C4_ASSERT(start_pos ==
npos || (start_pos >= 0 && start_pos <= len));
540 if(len < pattern.
len)
return npos;
541 for(
size_t i = start_pos, e = len - pattern.
len + 1; i < e; ++i)
544 for(
size_t j = 0; j < pattern.
len; ++j)
546 C4_ASSERT(i + j < len);
547 if(str[i + j] != pattern.
str[j])
564 inline size_t count(
const C c,
size_t pos=0)
const
566 C4_ASSERT(pos >= 0 && pos <= len);
572 pos = find(c, pos + 1);
580 C4_ASSERT(pos >= 0 && pos <= len);
586 pos = find(c, pos + c.
len);
601 pos = find(pattern, pos);
611 inline operator bool()
const {
return which !=
NONE && pos !=
npos; }
617 return first_of_any_iter(&s[0], &s[0] + 2);
623 return first_of_any_iter(&s[0], &s[0] + 3);
629 return first_of_any_iter(&s[0], &s[0] + 4);
635 return first_of_any_iter(&s[0], &s[0] + 5);
641 for(
size_t i = 0; i < len; ++i)
644 for(It it = first_span; it != last_span; ++curr, ++it)
646 auto const& chars = *it;
647 if((i + chars.len) > len)
continue;
649 for(
size_t j = 0; j < chars.len; ++j)
651 C4_ASSERT(i + j < len);
652 if(str[i + j] != chars[j])
672 return len > 0 ? str[0] == c :
false;
682 for(
size_t i = 0; i < num; ++i)
695 if(len < pattern.
len)
699 for(
size_t i = 0; i < pattern.
len; ++i)
701 if(str[i] != pattern[i])
716 for(
size_t i = 0; i < chars.
len; ++i)
718 if(str[0] == chars.
str[i])
729 return len > 0 ? str[len-1] == c :
false;
739 for(
size_t i = len - num; i < len; ++i)
752 if(len < pattern.
len)
756 for(
size_t i = 0, s = len-pattern.
len; i < pattern.
len; ++i)
758 if(str[s+i] != pattern[i])
773 for(
size_t i = 0; i < chars.
len; ++i)
775 if(str[len - 1] == chars[i])
788 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
789 for(
size_t i = start; i < len; ++i)
800 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
803 for(
size_t i = start-1; i != size_t(-1); --i)
814 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
815 for(
size_t i = start; i < len; ++i)
817 for(
size_t j = 0; j < chars.
len; ++j)
819 if(str[i] == chars[j])
829 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
832 for(
size_t i = start-1; i != size_t(-1); --i)
834 for(
size_t j = 0; j < chars.
len; ++j)
836 if(str[i] == chars[j])
847 for(
size_t i = 0; i < len; ++i)
857 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
858 for(
size_t i = start; i < len; ++i)
868 for(
size_t i = len-1; i != size_t(-1); --i)
878 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
881 for(
size_t i = start-1; i != size_t(-1); --i)
891 for(
size_t i = 0; i < len; ++i)
894 for(
size_t j = 0; j < chars.
len; ++j)
896 if(str[i] == chars.
str[j])
912 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
913 for(
size_t i = start; i < len; ++i)
916 for(
size_t j = 0; j < chars.
len; ++j)
918 if(str[i] == chars.
str[j])
934 for(
size_t i = len-1; i != size_t(-1); --i)
937 for(
size_t j = 0; j < chars.
len; ++j)
939 if(str[i] == chars.
str[j])
955 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
958 for(
size_t i = start-1; i != size_t(-1); --i)
961 for(
size_t j = 0; j < chars.
len; ++j)
963 if(str[i] == chars.
str[j])
989 size_t b = find(open);
992 size_t e = find(close, b+1);
1004 size_t b = find(open_close);
1006 for(
size_t i = b+1; i < len; ++i)
1011 if(str[i-1] != escape)
1013 return range(b, i+1);
1025 size_t b = find(open);
1027 size_t e, curr = b+1, count = 0;
1028 const char both[] = {open, close,
'\0'};
1029 while((e = first_of(both, curr)) !=
npos)
1036 else if(str[e] == close)
1038 if(count == 0)
return range(b, e+1);
1048 constexpr
const C dq(
'"'), sq(
'\'');
1049 if(len >= 2 && (str[len - 2] != C(
'\\')) &&
1050 ((begins_with(sq) && ends_with(sq))
1052 (begins_with(dq) && ends_with(dq))))
1054 return range(1, len -1);
1070 if(empty() || (first_non_empty_span().empty()))
1072 if(first_uint_span() == *
this)
1074 if(first_int_span() == *
this)
1076 if(first_real_span() == *
this)
1085 if(empty() || (first_non_empty_span().empty()))
1087 if(first_real_span() == *
this)
1096 if(empty() || (first_non_empty_span().empty()))
1098 if(first_uint_span() == *
this)
1100 if(first_int_span() == *
this)
1109 if(empty() || (first_non_empty_span().empty()))
1111 if(first_uint_span() == *
this)
1119 constexpr
const ro_substr empty_chars(
" \n\r\t");
1120 size_t pos = first_not_of(empty_chars);
1123 auto ret = sub(pos);
1124 pos = ret.first_of(empty_chars);
1125 return ret.first(pos);
1134 if(ne.
str[0] ==
'-')
1136 size_t skip_start = size_t(ne.
str[0] ==
'+');
1146 size_t skip_start = size_t(ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1152 C4_ASSERT(!empty());
1153 if(skip_start == len)
1155 C4_ASSERT(skip_start < len);
1156 if(len >= skip_start + 3)
1158 if(str[skip_start] !=
'0')
1160 for(
size_t i = skip_start; i < len; ++i)
1163 if(c < '0' || c >
'9')
1164 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1169 char next = str[skip_start + 1];
1170 if(next ==
'x' || next ==
'X')
1173 for(
size_t i = skip_start; i < len; ++i)
1175 const char c = str[i];
1176 if( ! _is_hex_char(c))
1177 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1181 else if(next ==
'b' || next ==
'B')
1184 for(
size_t i = skip_start; i < len; ++i)
1186 const char c = str[i];
1187 if(c !=
'0' && c !=
'1')
1188 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1192 else if(next ==
'o' || next ==
'O')
1195 for(
size_t i = skip_start; i < len; ++i)
1197 const char c = str[i];
1198 if(c < '0' || c >
'7')
1199 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1206 for(
size_t i = skip_start; i < len; ++i)
1208 const char c = str[i];
1209 if(c < '0' || c >
'9')
1210 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1221 const size_t skip_start = (ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1222 C4_ASSERT(skip_start == 0 || skip_start == 1);
1227 if(ne.
len >= skip_start+3)
1230 if(ne.
str[skip_start] !=
'0')
1232 if(ne.
str[skip_start] ==
'i')
1239 else if(ne.
str[skip_start] ==
'n')
1250 const char next = ne.
str[skip_start + 1];
1252 if(next ==
'x' || next ==
'X')
1255 else if(next ==
'b' || next ==
'B')
1258 else if(next ==
'o' || next ==
'O')
1273 return c ==
' ' || c ==
'\n'
1274 || c ==
']' || c ==
')' || c ==
'}'
1275 || c ==
',' || c ==
';' || c ==
'\r' || c ==
'\t' || c ==
'\0';
1279 static constexpr C4_ALWAYS_INLINE C4_CONST
bool _is_hex_char(
char c) noexcept
1281 return (c >=
'0' && c <=
'9') || (c >=
'a' && c <=
'f') || (c >=
'A' && c <=
'F');
1286 size_t posend = pos + word.
len;
1287 if(len >= posend && sub(pos, word.len) == word)
1288 if(len == posend || _is_delim_char(str[posend]))
1289 return first(posend);
1296 bool intchars =
false;
1297 bool fracchars =
false;
1300 for( ; pos < len; ++pos)
1302 const char c = str[pos];
1303 if(c >=
'0' && c <=
'9')
1310 goto fractional_part_dec;
1312 else if(c ==
'e' || c ==
'E')
1315 goto power_part_dec;
1317 else if(_is_delim_char(c))
1319 return intchars ? first(pos) : first(0);
1331 fractional_part_dec:
1333 C4_ASSERT(str[pos - 1] ==
'.');
1334 for( ; pos < len; ++pos)
1336 const char c = str[pos];
1337 if(c >=
'0' && c <=
'9')
1341 else if(c ==
'e' || c ==
'E')
1344 goto power_part_dec;
1346 else if(_is_delim_char(c))
1348 return intchars || fracchars ? first(pos) : first(0);
1355 return intchars || fracchars ?
1360 C4_ASSERT(str[pos - 1] ==
'e' || str[pos - 1] ==
'E');
1362 if((len == pos) || ((!intchars) && (!fracchars)))
1364 if(str[pos] ==
'-' || str[pos] ==
'+')
1367 for( ; pos < len; ++pos)
1369 const char c = str[pos];
1370 if(c >=
'0' && c <=
'9')
1372 else if(powchars && _is_delim_char(c))
1377 return powchars ? *this : first(0);
1383 bool intchars =
false;
1384 bool fracchars =
false;
1387 for( ; pos < len; ++pos)
1389 const char c = str[pos];
1397 goto fractional_part_hex;
1399 else if(c ==
'p' || c ==
'P')
1402 goto power_part_hex;
1404 else if(_is_delim_char(c))
1406 return intchars ? first(pos) : first(0);
1418 fractional_part_hex:
1420 C4_ASSERT(str[pos - 1] ==
'.');
1421 for( ; pos < len; ++pos)
1423 const char c = str[pos];
1428 else if(c ==
'p' || c ==
'P')
1431 goto power_part_hex;
1433 else if(_is_delim_char(c))
1435 return intchars || fracchars ? first(pos) : first(0);
1442 return intchars || fracchars ?
1447 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1451 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1457 for( ; pos < len; ++pos)
1459 const char c = str[pos];
1460 if(c >=
'0' && c <=
'9')
1462 else if(powchars && _is_delim_char(c))
1473 bool intchars =
false;
1474 bool fracchars =
false;
1477 for( ; pos < len; ++pos)
1479 const char c = str[pos];
1480 if(c ==
'0' || c ==
'1')
1487 goto fractional_part_bin;
1489 else if(c ==
'p' || c ==
'P')
1492 goto power_part_bin;
1494 else if(_is_delim_char(c))
1496 return intchars ? first(pos) : first(0);
1508 fractional_part_bin:
1510 C4_ASSERT(str[pos - 1] ==
'.');
1511 for( ; pos < len; ++pos)
1513 const char c = str[pos];
1514 if(c ==
'0' || c ==
'1')
1518 else if(c ==
'p' || c ==
'P')
1521 goto power_part_bin;
1523 else if(_is_delim_char(c))
1525 return intchars || fracchars ? first(pos) : first(0);
1532 return intchars || fracchars ?
1537 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1541 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1547 for( ; pos < len; ++pos)
1549 const char c = str[pos];
1550 if(c >=
'0' && c <=
'9')
1552 else if(powchars && _is_delim_char(c))
1563 bool intchars =
false;
1564 bool fracchars =
false;
1567 for( ; pos < len; ++pos)
1569 const char c = str[pos];
1570 if(c >=
'0' && c <=
'7')
1577 goto fractional_part_oct;
1579 else if(c ==
'p' || c ==
'P')
1582 goto power_part_oct;
1584 else if(_is_delim_char(c))
1586 return intchars ? first(pos) : first(0);
1598 fractional_part_oct:
1600 C4_ASSERT(str[pos - 1] ==
'.');
1601 for( ; pos < len; ++pos)
1603 const char c = str[pos];
1604 if(c >=
'0' && c <=
'7')
1608 else if(c ==
'p' || c ==
'P')
1611 goto power_part_oct;
1613 else if(_is_delim_char(c))
1615 return intchars || fracchars ? first(pos) : first(0);
1622 return intchars || fracchars ?
1627 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1631 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1637 for( ; pos < len; ++pos)
1639 const char c = str[pos];
1640 if(c >=
'0' && c <=
'9')
1642 else if(powchars && _is_delim_char(c))
1663 if(C4_LIKELY(*start_pos < len))
1665 for(
size_t i = *start_pos; i < len; i++)
1669 out->assign(str + *start_pos, i - *start_pos);
1674 out->assign(str + *start_pos, len - *start_pos);
1675 *start_pos = len + 1;
1680 bool valid = len > 0 && (*start_pos == len);
1681 if(valid && str && str[len-1] == sep)
1683 out->assign(str + len,
size_t(0));
1687 out->assign(str + len + 1,
size_t(0));
1689 *start_pos = len + 1;
1696 struct split_proxy_impl
1706 : m_proxy(proxy), m_pos(pos), m_sep(sep)
1713 m_proxy->m_str.next_split(m_sep, &m_pos, &m_str);
1728 C4_XASSERT((m_sep == that.
m_sep) &&
"cannot compare split iterators with different separators");
1733 return m_pos == that.
m_pos;
1742 : m_str(str_), m_start_pos(start_pos), m_sep(sep)
1746 split_iterator_impl begin()
const
1748 auto it = split_iterator_impl(
this, m_start_pos, m_sep);
1751 split_iterator_impl end()
const
1753 size_t pos = m_str.
size() + 1;
1754 auto it = split_iterator_impl(
this, pos, m_sep);
1766 C4_XASSERT((start_pos >= 0 && start_pos < len) || empty());
1767 auto ss = sub(0, len);
1779 if(C4_LIKELY(len > 1))
1781 auto pos = last_of(sep);
1786 return sub(pos + 1);
1792 return sub(pos + 1, 0);
1794 auto ppos = last_not_of(sep);
1800 auto pos0 = last_of(sep, ppos);
1816 if(begins_with(sep))
1832 if(C4_LIKELY(len > 1))
1834 auto pos = first_of(sep);
1847 auto ppos = first_not_of(sep);
1853 auto pos0 = first_of(sep, ppos);
1858 C4_XASSERT(pos0 > 0);
1860 return sub(0, pos0);
1870 if(begins_with(sep))
1887 auto ss = pop_right(sep, skip_empty);
1889 if(ss.find(sep) !=
npos)
1891 if(ss.ends_with(sep))
1899 ss = ss.sub(0, ss.len-1);
1909 auto ss = pop_left(sep, skip_empty);
1911 if(ss.find(sep) !=
npos)
1913 if(ss.begins_with(sep))
1937 auto ss = pop_right(sep,
true);
1944 auto ss = basename(sep);
1945 ss = ss.empty() ? *this : left_of(ss);
1951 return gpop_left(
'.');
1956 return pop_left(
'.');
1961 return pop_right(
'.');
1966 return gpop_right(
'.');
1980 for(
size_t i = 0; i < len; ++i)
1982 str[i] =
static_cast<C
>(::toupper(str[i]));
1990 for(
size_t i = 0; i < len; ++i)
1992 str[i] =
static_cast<C
>(::tolower(str[i]));
2002 for(
size_t i = 0; i < len; ++i)
2014 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2015 num = num !=
npos ? num : len - ifirst;
2016 num = num < that.
len ? num : that.
len;
2017 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2022 memcpy(str +
sizeof(C) * ifirst, that.
str,
sizeof(C) * num);
2031 if(len == 0)
return;
2032 detail::_do_reverse(str, str + len - 1);
2039 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2040 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2041 if(num == 0)
return;
2042 detail::_do_reverse(str + ifirst, str + ifirst + num - 1);
2049 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2050 C4_ASSERT(ilast >= 0 && ilast <= len);
2051 if(ifirst == ilast)
return;
2052 detail::_do_reverse(str + ifirst, str + ilast - 1);
2062 C4_ASSERT(pos >= 0 && pos+num <= len);
2063 size_t num_to_move = len - pos - num;
2064 memmove(str + pos, str + pos + num,
sizeof(C) * num_to_move);
2071 C4_ASSERT(first <= last);
2072 return erase(first,
static_cast<size_t>(last-first));
2080 C4_ASSERT(is_super(sub));
2081 C4_ASSERT(sub.
str >= str);
2082 return erase(
static_cast<size_t>(sub.
str - str), sub.
len);
2090 C4_REQUIRE_RW(
size_t)
replace(C value, C repl,
size_t pos=0)
2092 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2094 while((pos = find(value, pos)) !=
npos)
2108 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2110 while((pos = first_of(chars, pos)) !=
npos)
2126 C4_ASSERT( ! pattern.
empty());
2127 C4_ASSERT( !
this ->overlaps(dst));
2128 C4_ASSERT( ! pattern.
overlaps(dst));
2129 C4_ASSERT( ! repl .overlaps(dst));
2130 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2131 C4_SUPPRESS_WARNING_GCC_PUSH
2132 C4_SUPPRESS_WARNING_GCC(
"-Warray-bounds")
2133 #if (!defined(__clang__)) && (defined(__GNUC__) && (__GNUC__ >= 7))
2134 C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
2136 #define _c4append(first, last) \
2138 C4_ASSERT((last) >= (first)); \
2139 size_t num = static_cast<size_t>((last) - (first)); \
2140 if(num > 0 && sz + num <= dst.len) \
2142 memcpy(dst.str + sz, first, num * sizeof(C)); \
2150 size_t e = find(pattern, b);
2158 b = e + pattern.
size();
2159 }
while(b < len && b !=
npos);
2162 C4_SUPPRESS_WARNING_GCC_POP
2170 #undef C4_REQUIRE_RW
2187 C4_ALWAYS_INLINE substr
to_substr(substr s) noexcept {
return s; }
2189 C4_ALWAYS_INLINE csubstr
to_csubstr(substr s) noexcept {
return s; }
2191 C4_ALWAYS_INLINE csubstr
to_csubstr(csubstr s) noexcept {
return s; }
2195 C4_ALWAYS_INLINE substr
2196 to_substr(
char (&s)[N]) noexcept { substr ss(s, N-1);
return ss; }
2198 C4_ALWAYS_INLINE csubstr
2199 to_csubstr(
const char (&s)[N]) noexcept { csubstr ss(s, N-1);
return ss; }
2206 C4_ALWAYS_INLINE
typename std::enable_if<std::is_same<U, char*>::value, substr>::type
2212 C4_ALWAYS_INLINE
typename std::enable_if<std::is_same<U, const char*>::value || std::is_same<U, char*>::value, csubstr>::type
2228 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; }
2229 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; }
2250 #ifndef C4_SUBSTR_NO_OSTREAM_LSHIFT
2252 # pragma clang diagnostic push
2253 # pragma clang diagnostic ignored "-Wsign-conversion"
2254 #elif defined(__GNUC__)
2255 # pragma GCC diagnostic push
2256 # pragma GCC diagnostic ignored "-Wsign-conversion"
2260 template<
class OStream,
class C>
2276 # pragma clang diagnostic pop
2277 #elif defined(__GNUC__)
2278 # pragma GCC diagnostic pop
2288 # pragma clang diagnostic pop
2289 #elif defined(__GNUC__)
2290 # 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)