1 #ifndef _C4_SUBSTR_HPP_
2 #define _C4_SUBSTR_HPP_
10 #include "c4/export.hpp"
11 #include "c4/language.hpp"
12 #include "c4/error.hpp"
13 #include "c4/substr_fwd.hpp"
16 # pragma clang diagnostic push
17 # pragma clang diagnostic ignored "-Wold-style-cast"
18 #elif defined(__GNUC__)
19 # pragma GCC diagnostic push
20 # pragma GCC diagnostic ignored "-Wtype-limits"
21 # pragma GCC diagnostic ignored "-Wuseless-cast"
22 # pragma GCC diagnostic ignored "-Wold-style-cast"
38 static inline void _do_reverse(C *C4_RESTRICT first, C *C4_RESTRICT last)
57 #define C4_REQUIRE_RW(ret_type) \
58 template <typename U=C> \
59 typename std::enable_if< ! std::is_const<U>::value, ret_type>::type
87 using CC =
typename std::add_const<C>::type;
88 using NCC_ =
typename std::remove_const<C>::type;
99 enum :
size_t {
npos = (size_t)-1,
NONE = (
size_t)-1 };
103 C4_ALWAYS_INLINE
operator typename std::enable_if<!std::is_const<U>::value,
ro_substr const&>::type () const noexcept
123 C4_ALWAYS_INLINE
basic_substring& operator= (std::nullptr_t) noexcept { str =
nullptr; len = 0;
return *
this; }
125 C4_ALWAYS_INLINE
void clear() noexcept { str =
nullptr; len = 0; }
138 C4_ALWAYS_INLINE constexpr
basic_substring(C (&s_)[N]) noexcept : str(s_), len(N-1) {}
141 C4_ALWAYS_INLINE
basic_substring(C *s_,
size_t len_) noexcept : str(s_), len(len_) { C4_ASSERT(str || !len_); }
145 C4_ALWAYS_INLINE
basic_substring(C *beg_, C *end_) noexcept : str(beg_), len(
static_cast<size_t>(end_ - beg_)) { C4_ASSERT(end_ >= beg_); }
152 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
153 C4_ALWAYS_INLINE
basic_substring(U s_) noexcept : str(s_), len(s_ ? strlen(s_) : 0) {}
159 C4_ALWAYS_INLINE
void assign(C (&s_)[N]) noexcept { str = (s_); len = (N-1); }
162 C4_ALWAYS_INLINE
void assign(C *s_,
size_t len_) noexcept { str = s_; len = len_; C4_ASSERT(str || !len_); }
166 C4_ALWAYS_INLINE
void assign(C *beg_, C *end_) noexcept { C4_ASSERT(end_ >= beg_); str = (beg_); len =
static_cast<size_t>(end_ - beg_); }
173 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
174 C4_ALWAYS_INLINE
void assign(U s_) noexcept { str = (s_); len = (s_ ? strlen(s_) : 0); }
179 C4_ALWAYS_INLINE
basic_substring& operator= (C (&s_)[N]) noexcept { str = (s_); len = (N-1);
return *
this; }
186 template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value,
int>::type=0>
187 C4_ALWAYS_INLINE
basic_substring& operator= (U s_) noexcept { str = s_; len = s_ ? strlen(s_) : 0;
return *
this; }
196 C4_ALWAYS_INLINE C4_PURE
bool has_str() const noexcept {
return ! empty() && str[0] != C(0); }
197 C4_ALWAYS_INLINE C4_PURE
bool empty() const noexcept {
return (len == 0 || str ==
nullptr); }
198 C4_ALWAYS_INLINE C4_PURE
bool not_empty() const noexcept {
return (len != 0 && str !=
nullptr); }
199 C4_ALWAYS_INLINE C4_PURE
size_t size() const noexcept {
return len; }
202 C4_ALWAYS_INLINE C4_PURE
iterator end () noexcept {
return str + len; }
207 C4_ALWAYS_INLINE C4_PURE C *
data() noexcept {
return str; }
208 C4_ALWAYS_INLINE C4_PURE C
const*
data() const noexcept {
return str; }
210 C4_ALWAYS_INLINE C4_PURE C & operator[] (
size_t i) noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
211 C4_ALWAYS_INLINE C4_PURE C
const& operator[] (
size_t i)
const noexcept { C4_ASSERT(i >= 0 && i < len);
return str[i]; }
213 C4_ALWAYS_INLINE C4_PURE C &
front() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
214 C4_ALWAYS_INLINE C4_PURE C
const&
front() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *str; }
216 C4_ALWAYS_INLINE C4_PURE C &
back() noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
217 C4_ALWAYS_INLINE C4_PURE C
const&
back() const noexcept { C4_ASSERT(len > 0 && str !=
nullptr);
return *(str + len - 1); }
228 C4_XASSERT((str !=
nullptr) || len == 0);
229 if(C4_LIKELY(str !=
nullptr && len > 0))
230 return (*str != c) ? *str - c : (
static_cast<int>(len) - 1);
235 C4_PURE
int compare(C
const* C4_RESTRICT that,
size_t sz)
const noexcept
237 #if defined(__GNUC__) && (__GNUC__ >= 6)
238 C4_SUPPRESS_WARNING_GCC_WITH_PUSH(
"-Wnull-dereference")
240 C4_XASSERT(that || sz == 0);
241 C4_XASSERT(str || len == 0);
242 if(C4_LIKELY(str && that))
245 const size_t min = len < sz ? len : sz;
246 for(
size_t i = 0; i < min; ++i)
247 if(str[i] != that[i])
248 return str[i] < that[i] ? -1 : 1;
259 C4_XASSERT(len == 0 && sz == 0);
262 return len < sz ? -1 : 1;
263 #if defined(__GNUC__) && (__GNUC__ >= 6)
264 C4_SUPPRESS_WARNING_GCC_POP
270 C4_ALWAYS_INLINE C4_PURE
bool operator== (std::nullptr_t)
const noexcept {
return str ==
nullptr; }
271 C4_ALWAYS_INLINE C4_PURE
bool operator!= (std::nullptr_t)
const noexcept {
return str !=
nullptr; }
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; }
275 C4_ALWAYS_INLINE C4_PURE
bool operator< (C
const c)
const noexcept {
return this->compare(c) < 0; }
276 C4_ALWAYS_INLINE C4_PURE
bool operator> (C
const c)
const noexcept {
return this->compare(c) > 0; }
277 C4_ALWAYS_INLINE C4_PURE
bool operator<= (C
const c)
const noexcept {
return this->compare(c) <= 0; }
278 C4_ALWAYS_INLINE C4_PURE
bool operator>= (C
const c)
const noexcept {
return this->compare(c) >= 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; }
289 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator< (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) < 0; }
290 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator> (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) > 0; }
291 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator<= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) <= 0; }
292 template<
size_t N> C4_ALWAYS_INLINE C4_PURE
bool operator>= (
const char (&that)[N])
const noexcept {
return this->compare(that, N-1) >= 0; }
304 return that.is_super(*
this);
310 if(C4_LIKELY(len > 0))
311 return that.str >= str && that.str+that.len <= str+len;
313 return that.len == 0 && that.str == str && str !=
nullptr;
320 return that.str+that.len > str && that.str < str+len;
328 C4_ASSERT(first >= 0 && first <= len);
335 C4_ASSERT(first >= 0 && first <= len);
336 C4_ASSERT((num >= 0 && num <= len) || (num ==
npos));
337 size_t rnum = num !=
npos ? num : len - first;
338 C4_ASSERT((first >= 0 && first + rnum <= len) || (num == 0));
345 C4_ASSERT(first >= 0 && first <= len);
346 last = last !=
npos ? last : len;
347 C4_ASSERT(first <= last);
348 C4_ASSERT(last >= 0 && last <= len);
355 C4_ASSERT(num <= len || num ==
npos);
362 C4_ASSERT(num <= len || num ==
npos);
373 C4_ASSERT(
left >= 0 &&
left <= len);
382 C4_ASSERT(pos <= len || pos ==
npos);
383 return (pos !=
npos) ?
391 C4_ASSERT(pos <= len || pos ==
npos);
392 return (pos !=
npos) ?
400 C4_ASSERT(pos <= len || pos ==
npos);
401 return (pos !=
npos) ?
409 C4_ASSERT(pos <= len || pos ==
npos);
410 return (pos !=
npos) ?
411 basic_substring(str + (pos + !include_pos), len - (pos + !include_pos)) :
421 C4_ASSERT(is_super(subs) || subs.empty());
422 auto ssb = subs.begin();
425 if(ssb >= b && ssb <= e)
426 return sub(0,
static_cast<size_t>(ssb - b));
435 C4_ASSERT(is_super(subs) || subs.empty());
436 auto sse = subs.end();
439 if(sse >= b && sse <= e)
440 return sub(
static_cast<size_t>(sse - b),
static_cast<size_t>(e - sse));
457 size_t pos = first_not_of(c);
469 size_t pos = first_not_of(chars);
481 size_t pos = last_not_of(c,
npos);
483 return sub(0, pos+1);
493 size_t pos = last_not_of(chars,
npos);
495 return sub(0, pos+1);
503 return triml(c).
trimr(c);
509 return triml(chars).
trimr(chars);
516 if( ! begins_with(pattern))
518 return sub(pattern.
len < len ? pattern.
len : len);
525 if( ! ends_with(pattern))
527 return left_of(len - (pattern.
len < len ? pattern.
len : len));
537 size_t find(
const C c,
size_t start_pos=0)
const
539 return first_of(c, start_pos);
543 C4_ASSERT(start_pos ==
npos || (start_pos >= 0 && start_pos <= len));
544 if(len < pattern.
len)
return npos;
545 for(
size_t i = start_pos, e = len - pattern.
len + 1; i < e; ++i)
548 for(
size_t j = 0; j < pattern.
len; ++j)
550 C4_ASSERT(i + j < len);
551 if(str[i + j] != pattern.
str[j])
568 size_t count(
const C c,
size_t pos=0)
const
570 C4_ASSERT(pos >= 0 && pos <= len);
576 pos = find(c, pos + 1);
584 C4_ASSERT(pos >= 0 && pos <= len);
590 pos = find(c, pos + c.
len);
605 pos = find(pattern, pos);
615 operator bool()
const {
return which !=
NONE && pos !=
npos; }
621 return first_of_any_iter(&s[0], &s[0] + 2);
627 return first_of_any_iter(&s[0], &s[0] + 3);
633 return first_of_any_iter(&s[0], &s[0] + 4);
639 return first_of_any_iter(&s[0], &s[0] + 5);
645 for(
size_t i = 0; i < len; ++i)
648 for(It it = first_span; it != last_span; ++curr, ++it)
650 auto const& chars = *it;
651 if((i + chars.len) > len)
continue;
653 for(
size_t j = 0; j < chars.len; ++j)
655 C4_ASSERT(i + j < len);
656 if(str[i + j] != chars[j])
676 #if defined(__GNUC__) && (__GNUC__ >= 6)
677 C4_SUPPRESS_WARNING_GCC_WITH_PUSH(
"-Wnull-dereference")
679 return len > 0 ? str[0] == c :
false;
680 #if defined(__GNUC__) && (__GNUC__ >= 6)
681 C4_SUPPRESS_WARNING_GCC_POP
692 for(
size_t i = 0; i < num; ++i)
705 if(len < pattern.
len)
709 for(
size_t i = 0; i < pattern.
len; ++i)
711 if(str[i] != pattern[i])
726 for(
size_t i = 0; i < chars.
len; ++i)
728 if(str[0] == chars.
str[i])
739 return len > 0 ? str[len-1] == c :
false;
749 for(
size_t i = len - num; i < len; ++i)
762 if(len < pattern.
len)
766 for(
size_t i = 0, s = len-pattern.
len; i < pattern.
len; ++i)
768 if(str[s+i] != pattern[i])
783 for(
size_t i = 0; i < chars.
len; ++i)
785 if(str[len - 1] == chars[i])
798 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
799 for(
size_t i = start; i < len; ++i)
810 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
813 for(
size_t i = start-1; i != size_t(-1); --i)
824 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
825 for(
size_t i = start; i < len; ++i)
827 for(
size_t j = 0; j < chars.
len; ++j)
829 if(str[i] == chars[j])
839 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
842 for(
size_t i = start-1; i != size_t(-1); --i)
844 for(
size_t j = 0; j < chars.
len; ++j)
846 if(str[i] == chars[j])
857 for(
size_t i = 0; i < len; ++i)
867 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
868 for(
size_t i = start; i < len; ++i)
878 for(
size_t i = len-1; i != size_t(-1); --i)
888 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
891 for(
size_t i = start-1; i != size_t(-1); --i)
901 for(
size_t i = 0; i < len; ++i)
904 for(
size_t j = 0; j < chars.
len; ++j)
906 if(str[i] == chars.
str[j])
922 C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
923 for(
size_t i = start; i < len; ++i)
926 for(
size_t j = 0; j < chars.
len; ++j)
928 if(str[i] == chars.
str[j])
944 for(
size_t i = len-1; i != size_t(-1); --i)
947 for(
size_t j = 0; j < chars.
len; ++j)
949 if(str[i] == chars.
str[j])
965 C4_ASSERT(start ==
npos || (start >= 0 && start <= len));
968 for(
size_t i = start-1; i != size_t(-1); --i)
971 for(
size_t j = 0; j < chars.
len; ++j)
973 if(str[i] == chars.
str[j])
999 size_t b = find(open);
1002 size_t e = find(close, b+1);
1014 size_t b = find(open_close);
1016 for(
size_t i = b+1; i < len; ++i)
1021 if(str[i-1] != escape)
1023 return range(b, i+1);
1035 size_t b = find(open);
1037 size_t e, curr = b+1, count = 0;
1038 const char both[] = {open, close,
'\0'};
1039 while((e = first_of(both, curr)) !=
npos)
1046 else if(str[e] == close)
1048 if(count == 0)
return range(b, e+1);
1058 constexpr
const C dq(
'"'), sq(
'\'');
1059 if(len >= 2 && (str[len - 2] != C(
'\\')) &&
1060 ((begins_with(sq) && ends_with(sq))
1062 (begins_with(dq) && ends_with(dq))))
1064 return range(1, len -1);
1080 if(empty() || (first_non_empty_span().empty()))
1082 if(first_uint_span() == *
this)
1084 if(first_int_span() == *
this)
1086 if(first_real_span() == *
this)
1095 if(empty() || (first_non_empty_span().empty()))
1097 if(first_real_span() == *
this)
1106 if(empty() || (first_non_empty_span().empty()))
1108 if(first_uint_span() == *
this)
1110 if(first_int_span() == *
this)
1119 if(empty() || (first_non_empty_span().empty()))
1121 if(first_uint_span() == *
this)
1129 constexpr
const ro_substr empty_chars(
" \n\r\t");
1130 size_t pos = first_not_of(empty_chars);
1133 auto ret = sub(pos);
1134 pos = ret.first_of(empty_chars);
1135 return ret.first(pos);
1144 if(ne.
str[0] ==
'-')
1146 size_t skip_start = size_t(ne.
str[0] ==
'+');
1156 size_t skip_start = size_t(ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1162 C4_ASSERT(!empty());
1163 if(skip_start == len)
1165 C4_ASSERT(skip_start < len);
1166 if(len >= skip_start + 3)
1168 if(str[skip_start] !=
'0')
1170 for(
size_t i = skip_start; i < len; ++i)
1173 if(c < '0' || c >
'9')
1174 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1179 char next = str[skip_start + 1];
1180 if(next ==
'x' || next ==
'X')
1183 for(
size_t i = skip_start; i < len; ++i)
1185 const char c = str[i];
1186 if( ! _is_hex_char(c))
1187 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1191 else if(next ==
'b' || next ==
'B')
1194 for(
size_t i = skip_start; i < len; ++i)
1196 const char c = str[i];
1197 if(c !=
'0' && c !=
'1')
1198 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1202 else if(next ==
'o' || next ==
'O')
1205 for(
size_t i = skip_start; i < len; ++i)
1207 const char c = str[i];
1208 if(c < '0' || c >
'7')
1209 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1216 for(
size_t i = skip_start; i < len; ++i)
1218 const char c = str[i];
1219 if(c < '0' || c >
'9')
1220 return i > skip_start && _is_delim_char(c) ? first(i) : first(0);
1231 const size_t skip_start = (ne.
str[0] ==
'+' || ne.
str[0] ==
'-');
1232 C4_ASSERT(skip_start == 0 || skip_start == 1);
1237 if(ne.
len >= skip_start+3)
1240 if(ne.
str[skip_start] !=
'0')
1242 if(ne.
str[skip_start] ==
'i')
1249 else if(ne.
str[skip_start] ==
'n')
1260 const char next = ne.
str[skip_start + 1];
1262 if(next ==
'x' || next ==
'X')
1265 else if(next ==
'b' || next ==
'B')
1268 else if(next ==
'o' || next ==
'O')
1283 return c ==
' ' || c ==
'\n'
1284 || c ==
']' || c ==
')' || c ==
'}'
1285 || c ==
',' || c ==
';' || c ==
'\r' || c ==
'\t' || c ==
'\0';
1289 static constexpr C4_ALWAYS_INLINE C4_CONST
bool _is_hex_char(
char c) noexcept
1291 return (c >=
'0' && c <=
'9') || (c >=
'a' && c <=
'f') || (c >=
'A' && c <=
'F');
1296 size_t posend = pos + word.
len;
1297 if(len >= posend && sub(pos, word.len) == word)
1298 if(len == posend || _is_delim_char(str[posend]))
1299 return first(posend);
1306 bool intchars =
false;
1307 bool fracchars =
false;
1310 for( ; pos < len; ++pos)
1312 const char c = str[pos];
1313 if(c >=
'0' && c <=
'9')
1320 goto fractional_part_dec;
1322 else if(c ==
'e' || c ==
'E')
1325 goto power_part_dec;
1327 else if(_is_delim_char(c))
1329 return intchars ? first(pos) : first(0);
1341 fractional_part_dec:
1343 C4_ASSERT(str[pos - 1] ==
'.');
1344 for( ; pos < len; ++pos)
1346 const char c = str[pos];
1347 if(c >=
'0' && c <=
'9')
1351 else if(c ==
'e' || c ==
'E')
1354 goto power_part_dec;
1356 else if(_is_delim_char(c))
1358 return intchars || fracchars ? first(pos) : first(0);
1365 return intchars || fracchars ?
1370 C4_ASSERT(str[pos - 1] ==
'e' || str[pos - 1] ==
'E');
1372 if((len == pos) || ((!intchars) && (!fracchars)))
1374 if(str[pos] ==
'-' || str[pos] ==
'+')
1377 for( ; pos < len; ++pos)
1379 const char c = str[pos];
1380 if(c >=
'0' && c <=
'9')
1382 else if(powchars && _is_delim_char(c))
1387 return powchars ? *this : first(0);
1393 bool intchars =
false;
1394 bool fracchars =
false;
1397 for( ; pos < len; ++pos)
1399 const char c = str[pos];
1407 goto fractional_part_hex;
1409 else if(c ==
'p' || c ==
'P')
1412 goto power_part_hex;
1414 else if(_is_delim_char(c))
1416 return intchars ? first(pos) : first(0);
1428 fractional_part_hex:
1430 C4_ASSERT(str[pos - 1] ==
'.');
1431 for( ; pos < len; ++pos)
1433 const char c = str[pos];
1438 else if(c ==
'p' || c ==
'P')
1441 goto power_part_hex;
1443 else if(_is_delim_char(c))
1445 return intchars || fracchars ? first(pos) : first(0);
1452 return intchars || fracchars ?
1457 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1461 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1467 for( ; pos < len; ++pos)
1469 const char c = str[pos];
1470 if(c >=
'0' && c <=
'9')
1472 else if(powchars && _is_delim_char(c))
1483 bool intchars =
false;
1484 bool fracchars =
false;
1487 for( ; pos < len; ++pos)
1489 const char c = str[pos];
1490 if(c ==
'0' || c ==
'1')
1497 goto fractional_part_bin;
1499 else if(c ==
'p' || c ==
'P')
1502 goto power_part_bin;
1504 else if(_is_delim_char(c))
1506 return intchars ? first(pos) : first(0);
1518 fractional_part_bin:
1520 C4_ASSERT(str[pos - 1] ==
'.');
1521 for( ; pos < len; ++pos)
1523 const char c = str[pos];
1524 if(c ==
'0' || c ==
'1')
1528 else if(c ==
'p' || c ==
'P')
1531 goto power_part_bin;
1533 else if(_is_delim_char(c))
1535 return intchars || fracchars ? first(pos) : first(0);
1542 return intchars || fracchars ?
1547 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1551 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1557 for( ; pos < len; ++pos)
1559 const char c = str[pos];
1560 if(c >=
'0' && c <=
'9')
1562 else if(powchars && _is_delim_char(c))
1573 bool intchars =
false;
1574 bool fracchars =
false;
1577 for( ; pos < len; ++pos)
1579 const char c = str[pos];
1580 if(c >=
'0' && c <=
'7')
1587 goto fractional_part_oct;
1589 else if(c ==
'p' || c ==
'P')
1592 goto power_part_oct;
1594 else if(_is_delim_char(c))
1596 return intchars ? first(pos) : first(0);
1608 fractional_part_oct:
1610 C4_ASSERT(str[pos - 1] ==
'.');
1611 for( ; pos < len; ++pos)
1613 const char c = str[pos];
1614 if(c >=
'0' && c <=
'7')
1618 else if(c ==
'p' || c ==
'P')
1621 goto power_part_oct;
1623 else if(_is_delim_char(c))
1625 return intchars || fracchars ? first(pos) : first(0);
1632 return intchars || fracchars ?
1637 C4_ASSERT(str[pos - 1] ==
'p' || str[pos - 1] ==
'P');
1641 if(len <= (pos+1) || (str[pos] !=
'+' && str[pos] !=
'-') || ((!intchars) && (!fracchars)))
1647 for( ; pos < len; ++pos)
1649 const char c = str[pos];
1650 if(c >=
'0' && c <=
'9')
1652 else if(powchars && _is_delim_char(c))
1673 if(C4_LIKELY(*start_pos < len))
1675 for(
size_t i = *start_pos; i < len; i++)
1679 out->assign(str + *start_pos, i - *start_pos);
1684 out->assign(str + *start_pos, len - *start_pos);
1685 *start_pos = len + 1;
1690 bool valid = len > 0 && (*start_pos == len);
1691 if(valid && str && str[len-1] == sep)
1693 out->assign(str + len,
size_t(0));
1697 out->assign(str + len + 1,
size_t(0));
1699 *start_pos = len + 1;
1706 struct split_proxy_impl
1716 : m_proxy(proxy), m_pos(pos), m_sep(sep)
1723 m_proxy->m_str.next_split(m_sep, &m_pos, &m_str);
1738 C4_XASSERT((m_sep == that.
m_sep) &&
"cannot compare split iterators with different separators");
1743 return m_pos == that.
m_pos;
1752 : m_str(str_), m_start_pos(start_pos), m_sep(sep)
1756 split_iterator_impl begin()
const
1758 auto it = split_iterator_impl(
this, m_start_pos, m_sep);
1761 split_iterator_impl end()
const
1763 size_t pos = m_str.
size() + 1;
1764 auto it = split_iterator_impl(
this, pos, m_sep);
1776 C4_XASSERT((start_pos >= 0 && start_pos < len) || empty());
1777 auto ss = sub(0, len);
1789 if(C4_LIKELY(len > 1))
1791 auto pos = last_of(sep);
1796 return sub(pos + 1);
1802 return sub(pos + 1, 0);
1804 auto ppos = last_not_of(sep);
1810 auto pos0 = last_of(sep, ppos);
1826 if(begins_with(sep))
1842 if(C4_LIKELY(len > 1))
1844 auto pos = first_of(sep);
1857 auto ppos = first_not_of(sep);
1863 auto pos0 = first_of(sep, ppos);
1868 C4_XASSERT(pos0 > 0);
1870 return sub(0, pos0);
1880 if(begins_with(sep))
1897 auto ss = pop_right(sep, skip_empty);
1899 if(ss.find(sep) !=
npos)
1901 if(ss.ends_with(sep))
1909 ss = ss.sub(0, ss.len-1);
1919 auto ss = pop_left(sep, skip_empty);
1921 if(ss.find(sep) !=
npos)
1923 if(ss.begins_with(sep))
1947 auto ss = pop_right(sep,
true);
1954 auto ss = basename(sep);
1955 ss = ss.empty() ? *this : left_of(ss);
1961 return gpop_left(
'.');
1966 return pop_left(
'.');
1971 return pop_right(
'.');
1976 return gpop_right(
'.');
1990 for(
size_t i = 0; i < len; ++i)
1992 str[i] =
static_cast<C
>(::toupper(str[i]));
2000 for(
size_t i = 0; i < len; ++i)
2002 str[i] =
static_cast<C
>(::tolower(str[i]));
2012 for(
size_t i = 0; i < len; ++i)
2022 C4_ASSERT(!overlaps(that));
2023 size_t num = that.
len <= len ? that.
len : len;
2028 memcpy(str, that.
str,
sizeof(C) * num);
2035 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2036 num = num !=
npos ? num : len - ifirst;
2037 num = num < that.
len ? num : that.
len;
2038 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2043 memcpy(str + (
sizeof(C) * ifirst), that.
str,
sizeof(C) * num);
2052 if(len == 0)
return;
2053 detail::_do_reverse(str, str + len - 1);
2060 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2061 C4_ASSERT(ifirst + num >= 0 && ifirst + num <= len);
2062 if(num == 0)
return;
2063 detail::_do_reverse(str + ifirst, str + ifirst + num - 1);
2070 C4_ASSERT(ifirst >= 0 && ifirst <= len);
2071 C4_ASSERT(ilast >= 0 && ilast <= len);
2072 if(ifirst == ilast)
return;
2073 detail::_do_reverse(str + ifirst, str + ilast - 1);
2083 C4_ASSERT(pos >= 0 && pos+num <= len);
2084 size_t num_to_move = len - pos - num;
2085 memmove(str + pos, str + pos + num,
sizeof(C) * num_to_move);
2092 C4_ASSERT(first <= last);
2093 return erase(first,
static_cast<size_t>(last-first));
2101 C4_ASSERT(is_super(sub));
2102 C4_ASSERT(sub.
str >= str);
2103 return erase(
static_cast<size_t>(sub.
str - str), sub.
len);
2111 C4_REQUIRE_RW(
size_t)
replace(C value, C repl,
size_t pos=0)
2113 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2115 while((pos = find(value, pos)) !=
npos)
2129 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2131 while((pos = first_of(chars, pos)) !=
npos)
2147 C4_ASSERT( ! pattern.
empty());
2148 C4_ASSERT( !
this ->overlaps(dst));
2149 C4_ASSERT( ! pattern.
overlaps(dst));
2150 C4_ASSERT( ! repl .overlaps(dst));
2151 C4_ASSERT((pos >= 0 && pos <= len) || pos ==
npos);
2152 C4_SUPPRESS_WARNING_GCC_PUSH
2153 C4_SUPPRESS_WARNING_GCC(
"-Warray-bounds")
2154 #if (!defined(__clang__)) && (defined(__GNUC__) && (__GNUC__ >= 7))
2155 C4_SUPPRESS_WARNING_GCC(
"-Wstringop-overflow")
2157 #define _c4append(first, last) \
2159 C4_ASSERT((last) >= (first)); \
2160 size_t num = static_cast<size_t>((last) - (first)); \
2161 if(num > 0 && sz + num <= dst.len) \
2163 memcpy(dst.str + sz, first, num * sizeof(C)); \
2171 size_t e = find(pattern, b);
2179 b = e + pattern.
size();
2180 }
while(b < len && b !=
npos);
2183 C4_SUPPRESS_WARNING_GCC_POP
2191 #undef C4_REQUIRE_RW
2208 C4_ALWAYS_INLINE substr
to_substr(substr s) noexcept {
return s; }
2210 C4_ALWAYS_INLINE csubstr
to_csubstr(substr s) noexcept {
return csubstr{s.str, s.len}; }
2212 C4_ALWAYS_INLINE csubstr
to_csubstr(csubstr s) noexcept {
return s; }
2215 template<
size_t N> C4_ALWAYS_INLINE substr
to_substr(
char (&s)[N]) noexcept
2217 return substr(s, N-1);
2219 template<
size_t N> C4_ALWAYS_INLINE csubstr
to_csubstr(
const char (&s)[N]) noexcept
2221 return csubstr(s, N-1);
2228 template<
class U> C4_ALWAYS_INLINE
auto to_substr(U s) noexcept
2229 ->
typename std::enable_if<std::is_same<U, char*>::value, substr>::type
2237 ->
typename std::enable_if<std::is_same<U, const char*>::value || std::is_same<U, char*>::value, csubstr>::type
2245 template<
class T>
struct is_string :
public std::false_type {};
2254 template<>
struct is_string<const char*> :
public std::true_type {};
2257 template<>
struct is_string<char*> :
public std::true_type {};
2260 template<
size_t N>
struct is_string<const char[N]> :
public std::true_type {};
2263 template<
size_t N>
struct is_string<char[N]> :
public std::true_type {};
2266 template<
size_t N>
struct is_string<const char (&)[N]> :
public std::true_type {};
2269 template<
size_t N>
struct is_string<char (&)[N]> :
public std::true_type {};
2272 template<
size_t N>
struct is_string<const char (&&)[N]> :
public std::true_type {};
2275 template<
size_t N>
struct is_string<char (&&)[N]> :
public std::true_type {};
2290 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; }
2291 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; }
2312 #ifndef C4_SUBSTR_NO_OSTREAM_LSHIFT
2314 # pragma clang diagnostic push
2315 # pragma clang diagnostic ignored "-Wsign-conversion"
2316 #elif defined(__GNUC__)
2317 # pragma GCC diagnostic push
2318 # pragma GCC diagnostic ignored "-Wsign-conversion"
2322 template<
class OStream,
class C>
2338 # pragma clang diagnostic pop
2339 #elif defined(__GNUC__)
2340 # pragma GCC diagnostic pop
2350 # pragma clang diagnostic pop
2351 #elif defined(__GNUC__)
2352 # 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
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
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.
int compare(C const *that, size_t sz) const noexcept
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 copy_from(ro_substr that, size_t ifirst, size_t num=npos)
copy a string to this substr, starting at a specified given position
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
void copy_from(ro_substr that)
copy a string to this substr, starting at 0
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
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
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
a traits class to mark a type as a string type (meaning c4::to_csubstr() can be used directly).
a traits class to mark a type as a writeable string type (meaning c4::to_substr() can be used directl...
#define _c4append(first, last)