20#if defined __has_builtin
21# if __has_builtin(__builtin_mul_overflow) && __has_builtin(__builtin_add_overflow)
22# define HAS_BUILTIN_OVERFLOW
27#define _no_unique_address msvc::no_unique_address
28#define decl_empty_bases __declspec(empty_bases)
30#define _no_unique_address no_unique_address
31#define decl_empty_bases
43#define SS_CONSTEVAL constexpr
45#define SS_CONSTEVAL consteval
56inline constexpr bool wchar_is_u16 =
sizeof(wchar_t) ==
sizeof(
char16_t);
58using wchar_type = std::conditional<wchar_is_u16, char16_t, char32_t>::type;
60inline wchar_type* to_w(
wchar_t* p) {
61 return (
reinterpret_cast<wchar_type*
>(p));
64inline const wchar_type* to_w(
const wchar_t* p) {
65 return (
reinterpret_cast<const wchar_type*
>(p));
68inline wchar_t* from_w(wchar_type* p) {
69 return (
reinterpret_cast<wchar_t*
>(p));
72inline const wchar_t* from_w(
const wchar_type* p) {
73 return (
reinterpret_cast<const wchar_t*
>(p));
82using uu8s = std::make_unsigned<u8s>::type;
84template<
typename T,
typename K = void,
typename... Types>
85struct is_one_of_type {
86 static constexpr bool value = std::is_same_v<T, K> || is_one_of_type<T, Types...>::value;
89struct is_one_of_type<T, void> : std::false_type {};
92concept is_one_of_char_v = is_one_of_type<K, u8s, ubs, wchar_t, u16s, u32s>::value;
95concept is_one_of_std_char_v = is_one_of_type<K, u8s, ubs, wchar_t, wchar_type>::value;
97template<is_one_of_std_
char_v From>
98auto to_one_of_std_char(From* from) {
99 if constexpr (std::is_same_v<From, u8s> || std::is_same_v<From, wchar_t>) {
101 }
else if constexpr (std::is_same_v<From, ubs>) {
102 return reinterpret_cast<u8s*
>(from);
108template<is_one_of_std_
char_v From>
109auto to_one_of_std_char(
const From* from) {
110 if constexpr (std::is_same_v<From, u8s> || std::is_same_v<From, wchar_t>) {
112 }
else if constexpr (std::is_same_v<From, ubs>) {
113 return reinterpret_cast<const u8s*
>(from);
120struct to_std_char_type : std::type_identity<K>{};
123struct to_std_char_type<char8_t>{
128struct to_std_char_type<char16_t>{
129 using type = std::conditional_t<
sizeof(char16_t) ==
sizeof(
wchar_t), wchar_t,
void>;
133struct to_std_char_type<char32_t>{
134 using type = std::conditional_t<
sizeof(char32_t) ==
sizeof(
wchar_t), wchar_t,
void>;
138using to_std_char_t =
typename to_std_char_type<K>::type;
141struct to_base_char_type : std::type_identity<K>{};
144struct to_base_char_type<char8_t>{
149struct to_base_char_type<wchar_t>{
150 using type = std::conditional_t<
sizeof(char16_t) ==
sizeof(
wchar_t), char16_t,
char32_t>;
154using to_base_char_t =
typename to_base_char_type<K>::type;
171template<
typename K1,
typename K2>
220template<
typename T>
struct const_lit;
223template<is_one_of_
char_v T,
size_t N>
224struct const_lit<const T(&)[N]> {
226 constexpr static size_t Count = N;
230concept is_const_lit_v =
requires {
231 typename const_lit<T>::symb_type;
236template<
typename K,
typename T>
struct const_lit_for;
238template<
typename K, is_equal_str_type_v<K> P,
size_t N>
239struct const_lit_for<K, const P(&)[N]> {
240 constexpr static size_t Count = N;
243template<
typename K,
size_t N>
244class const_lit_to_array {
247 constexpr size_t find(K s)
const {
248 if constexpr (Idx < N) {
249 return s == symbols_[Idx] ? Idx : find<Idx + 1>(s);
255 constexpr bool exist(K s)
const {
256 if constexpr (Idx < N) {
257 return s == symbols_[Idx] || exist<Idx + 1>(s);
262 const K (&symbols_)[N + 1];
264 template<typename T, size_t M = const_lit_for<K, T>::Count>
requires (M == N + 1)
265 constexpr const_lit_to_array(T&& s)
266 : symbols_((
const K(&)[M])s) {}
268 constexpr bool contain(K s)
const {
271 constexpr size_t index_of(K s)
const {
277concept StrTypeCommon =
requires(
const A& a) {
278 typename std::remove_cvref_t<A>::symb_type;
279 { a.is_empty() } -> std::same_as<bool>;
280 { a.length() } -> std::convertible_to<size_t>;
285concept HasSymbType =
requires {
286 typename std::remove_cvref_t<A>::symb_type;
310template<
typename A,
typename K>
311concept StrType = StrTypeCommon<A> &&
requires(
const A& a) {
312 { a.symbols() } -> std::same_as<const K*>;
313} && std::is_same_v<typename std::remove_cvref_t<A>::symb_type, K>;
315template<
typename T>
struct is_std_string_source : std::false_type{};
317template<
typename K,
typename A>
318struct is_std_string_source<
std::basic_string<K, std::char_traits<K>, A>> : std::true_type{};
321struct is_std_string_source<std::basic_string_view<K, std::char_traits<K>>> : std::true_type{};
324concept is_std_string_source_v = is_std_string_source<T>::value;
327concept StdStrSource = is_std_string_source_v<std::remove_cvref_t<T>>;
329template<
typename T,
typename K>
333concept StrSource = StdStrSource<T> || is_const_lit_v<T> || StrTypeCommon<T>;
336concept StrSourceNoLiteral = StdStrSource<T> || StrTypeCommon<T>;
338template<
typename T>
struct is_std_string : std::false_type{};
340template<
typename K,
typename A>
341struct is_std_string<std::basic_string<K, std::char_traits<K>, A>> : std::true_type{};
344concept is_std_string_v = is_std_string<T>::value;
345template<
typename T,
typename K>
349struct symb_type_from_src {
354template<
typename K,
size_t N>
355struct symb_type_from_src<const K(&)[N]> {
359template<StdStrSource T>
360struct symb_type_from_src<T> {
361 using type =
typename std::remove_cvref_t<T>::value_type;
364template<HasSymbType T>
365struct symb_type_from_src<T> {
366 using type =
typename std::remove_cvref_t<T>::symb_type;
370using symb_type_from_src_t = symb_type_from_src<T>::type;
508 typename std::remove_cvref_t<A>::symb_type;
509 { a.length() } -> std::convertible_to<size_t>;
510 { a.place(std::declval<
typename std::remove_cvref_t<A>::symb_type*>()) } -> std::same_as<typename std::remove_cvref_t<A>::symb_type*>;
524template<
typename A,
typename K>
527template<
typename K,
typename T>
528struct convert_to_strexpr;
530template<
typename A,
typename K>
531concept strexpr_from =
requires(
const A& a) {
532 {convert_to_strexpr<K, std::remove_cvref_t<A>>::convert(a)} ->
StrExprForType<K>;
535template<
typename A,
typename K>
536concept strexpr_std =
requires(
const A& a) {
537 {convert_to_strexpr<K, std::remove_cvref_t<A>>::convert(a)} -> StdStringForType<K>;
540template<
typename A,
typename K>
543template<
typename A,
typename K>
546template<
typename A,
typename K>
547concept to_strexpr_meth =
requires(
const A& a) {
551template<
typename A,
typename K>
552concept to_strexpr_std =
requires(
const A& a) {
553 {a.template to_strexpr<K>()} -> StdStringForType<K>;
556template<
typename A,
typename K>
557concept convertible_to_strexpr = strexpr_for<A, K> || strexpr_from<A, K> || strexpr_std<A, K> || to_strexpr_type<A, K> || to_strexpr_meth<A, K> || to_strexpr_std<A, K>;
559template<
typename K, strexpr_from<K> T>
560constexpr auto to_strexpr(T&& t) {
561 return convert_to_strexpr<K, std::remove_cvref_t<T>>::convert(std::forward<T>(t));
564template<
typename K, strexpr_for<K> T>
565constexpr typename convert_to_strexpr<K, std::remove_cvref_t<T>>::type to_strexpr(T&& t) {
566 return {std::forward<T>(t)};
569template<
typename K, to_strexpr_type<K> T>
570constexpr typename std::remove_cvref_t<T>::strexpr to_strexpr(T&& t) {
571 return {std::forward<T>(t)};
574template<
typename K, to_strexpr_meth<K> T>
575constexpr auto to_strexpr(T&& t) {
576 return t.template to_strexpr<K>();
580struct expr_stdstr_c {
584 expr_stdstr_c(T t) : t_(std::move(t)){}
586 constexpr size_t length() const noexcept {
589 constexpr symb_type* place(symb_type* p)
const noexcept {
590 size_t s = t_.size();
591 std::char_traits<K>::copy(p, (
const K*)t_.data(), s);
596template<
typename K, strexpr_std<K> T>
597constexpr auto to_strexpr(T&& t) {
598 using type =
decltype(convert_to_strexpr<K, std::remove_cvref_t<T>>::convert(std::forward<T>(t)));
599 return expr_stdstr_c<K, type>{convert_to_strexpr<K, std::remove_cvref_t<T>>::convert(std::forward<T>(t))};
602template<
typename K, to_strexpr_std<K> T>
603constexpr auto to_strexpr(T&& t) {
604 using type =
decltype(t.template to_strexpr<K>());
605 return expr_stdstr_c<K, type>{t.template to_strexpr<K>()};
608template<
typename K,
typename T>
609using convert_to_strexpr_t =
decltype(to_strexpr<K>(std::declval<T>()));
627template<
typename K,
typename Allocator, StrExpr A>
628constexpr std::basic_string<K, std::char_traits<K>, Allocator> to_std_string(
const A& expr) {
629 std::basic_string<K, std::char_traits<K>, Allocator> res;
630 if (
size_t l = expr.length()) {
631 auto fill = [&](K* ptr,
size_t size) ->
size_t {
632 expr.place((
typename A::symb_type*)ptr);
635 if constexpr (
requires { res.resize_and_overwrite(l, fill); }) {
636 res.resize_and_overwrite(l, fill);
637 }
else if constexpr (
requires{ res._Resize_and_overwrite(l, fill); }) {
639 res._Resize_and_overwrite(l, fill);
642 expr.place((
typename A::symb_type*)res.data());
658template<
typename Impl>
660 template<is_equal_str_type_v<
typename Impl::symb_type> P,
typename Allocator>
661 constexpr operator std::basic_string<P, std::char_traits<P>, Allocator>()
const {
662 return to_std_string<P, Allocator>(*
static_cast<const Impl*
>(
this));
681template<StrExpr A, StrExprForType<
typename A::symb_type> B>
683 using symb_type =
typename A::symb_type;
686 constexpr strexprjoin(
const A& a_,
const B& b_) : a(a_), b(b_){}
687 constexpr size_t length()
const noexcept {
688 return a.length() + b.length();
690 constexpr symb_type* place(symb_type* p)
const noexcept {
691 return (symb_type*)b.place((
typename B::symb_type*)a.place(p));
718template<StrExpr A, StrExprForType<
typename A::symb_type> B>
743template<StrExpr A, StrExprForType<
typename A::symb_type> B,
bool last = true>
745 using symb_type =
typename A::symb_type;
748 template<
typename... Args>
749 constexpr strexprjoin_c(
const A& a_, Args&&... args_) : a(a_), b(std::forward<Args>(args_)...) {}
750 constexpr size_t length()
const noexcept {
751 return a.length() + b.length();
753 constexpr symb_type* place(symb_type* p)
const noexcept {
754 if constexpr (last) {
755 return (symb_type*)b.place((
typename B::symb_type*)a.place(p));
757 return a.place((symb_type*)b.place((
typename B::symb_type*)p));
776template<StrExpr A, convertible_to_strexpr<
typename A::symb_type> B>
778 return {a, to_strexpr<typename A::symb_type>(std::forward<B>(b))};
795template<StrExpr A, convertible_to_strexpr<
typename A::symb_type> B>
797 return {a, to_strexpr<typename A::symb_type>(std::forward<B>(b))};
837 constexpr size_t length()
const noexcept {
840 constexpr symb_type* place(symb_type* p)
const noexcept {
877struct expr_char : expr_to_std_string<expr_char<K>>{
880 constexpr expr_char(K v) : value(v){}
881 constexpr size_t length() const noexcept {
884 constexpr symb_type* place(symb_type* p)
const noexcept {
902template<
typename K, StrExprForType<K> A>
922template<
typename K,
size_t N>
923struct expr_literal : expr_to_std_string<expr_literal<K, N>> {
925 const K (&str)[N + 1];
926 constexpr expr_literal(
const K (&str_)[N + 1]) : str(str_){}
928 constexpr size_t length() const noexcept {
931 constexpr symb_type* place(symb_type* p)
const noexcept {
932 if constexpr (N != 0)
933 std::char_traits<K>::copy(p, str, N);
990template<typename T, size_t N = const_lit<T>::Count>
991constexpr expr_literal<typename const_lit<T>::symb_type,
static_cast<size_t>(N - 1)>
e_t(T&& s) {
995template<
bool first,
typename K,
size_t N,
typename A>
996struct expr_literal_join : expr_to_std_string<expr_literal_join<first, K, N, A>> {
998 using atype =
typename A::symb_type;
999 const K (&str)[N + 1];
1001 constexpr expr_literal_join(
const K (&str_)[N + 1],
const A& a_) : str(str_), a(a_){}
1003 constexpr size_t length() const noexcept {
1004 return N + a.length();
1006 constexpr symb_type* place(symb_type* p)
const noexcept {
1007 if constexpr (N != 0) {
1008 if constexpr (first) {
1009 std::char_traits<K>::copy(p, str, N);
1010 return (symb_type*)a.place((atype*)(p + N));
1012 p = (symb_type*)a.place((atype*)p);
1013 std::char_traits<K>::copy(p, str, N);
1030constexpr expr_literal_join<
false, P, (N - 1), A>
operator+(
const A& a, T&& s) {
1041template<StrExpr A, typename T, typename P = typename const_lit<T>::symb_type,
size_t N = const_lit<T>::Count>
requires is_equal_str_type_v<typename A::symb_type, P>
1042constexpr expr_literal_join<
true, P, (N - 1), A>
operator+(T&& s,
const A& a) {
1059template<
typename K,
size_t N,
size_t S = ' '>
1061 using symb_type = K;
1062 constexpr size_t length()
const noexcept {
1065 constexpr symb_type* place(symb_type* p)
const noexcept {
1066 if constexpr (N != 0)
1067 std::char_traits<K>::assign(p, N,
static_cast<K
>(S));
1121 using symb_type = K;
1124 constexpr expr_pad(
size_t len_, K s_) : len(len_), s(s_){}
1125 constexpr size_t length()
const noexcept {
1128 constexpr symb_type* place(symb_type* p)
const noexcept {
1130 std::char_traits<K>::assign(p, len, s);
1153template<
typename K,
size_t N>
1154struct expr_repeat_lit : expr_to_std_string<expr_repeat_lit<K, N>> {
1155 using symb_type = K;
1157 const K (&s)[N + 1];
1158 constexpr expr_repeat_lit(
size_t repeat,
const K (&s_)[N + 1]) : repeat_(repeat), s(s_){}
1159 constexpr size_t length() const noexcept {
1162 constexpr symb_type* place(symb_type* p)
const noexcept {
1164 for (
size_t i = 0; i < repeat_; i++) {
1165 std::char_traits<K>::copy(p, s, N);
1175 using symb_type =
typename A::symb_type;
1178 constexpr expr_repeat_expr(
size_t repeat,
const A& expr) : repeat_(repeat), expr_(expr){}
1179 constexpr size_t length() const noexcept {
1181 return repeat_ * expr_.length();
1185 constexpr symb_type* place(symb_type* p)
const noexcept {
1188 return expr_.place(p);
1190 symb_type* start = p;
1192 size_t len = size_t(p - start);
1194 for (
size_t i = 1; i < repeat_; i++) {
1195 std::char_traits<symb_type>::copy(p, start, len);
1217template<typename T, typename K = const_lit<T>::symb_type,
size_t M = const_lit<T>::Count>
requires (M > 0)
1218constexpr expr_repeat_lit<K, M - 1>
e_repeat(T&& s,
size_t l) {
1236constexpr expr_repeat_expr<A>
e_repeat(
const A& s,
size_t l) {
1253template<StrExpr A, StrExprForType<
typename A::symb_type> B>
1255 using symb_type =
typename A::symb_type;
1256 using my_type = expr_choice<A, B>;
1261 constexpr expr_choice(
const A& _a,
const B& _b,
bool _choice) : a(_a), b(_b), choice(_choice){}
1263 constexpr size_t length()
const noexcept {
1264 return choice ? a.length() : b.length();
1266 constexpr symb_type* place(symb_type* ptr)
const noexcept {
1267 return choice ? a.place(ptr) : (symb_type*)b.place((
typename B::symb_type*)ptr);
1284 using symb_type =
typename A::symb_type;
1285 using my_type = expr_if<A>;
1288 constexpr expr_if(
const A& _a,
bool _choice) : a(_a), choice(_choice){}
1290 constexpr size_t length()
const noexcept {
1291 return choice ? a.length() : 0;
1293 constexpr symb_type* place(symb_type* ptr)
const noexcept {
1294 return choice ? a.place(ptr) : ptr;
1344template<
typename L, StrExprForType<L> A,
size_t N,
bool Compare>
1346 using symb_type = L;
1347 const symb_type (&str)[N + 1];
1350 constexpr expr_choice_one_lit(
const symb_type (&_str)[N + 1],
const A& _a,
bool _choice) : str(_str), a(_a), choice(_choice){}
1352 constexpr size_t length()
const noexcept {
1353 return choice == Compare ? a.length() : N;
1355 constexpr symb_type* place(symb_type* ptr)
const noexcept {
1356 if (choice == Compare) {
1357 return (L*)a.place((
typename A::symb_type*)ptr);
1359 if constexpr (N != 0) {
1360 std::char_traits<symb_type>::copy(ptr, str, N);
1410template<
typename K,
size_t N,
typename P,
size_t M>
1412 using symb_type = K;
1413 const K (&str_a)[N + 1];
1414 const P (&str_b)[M + 1];
1416 constexpr expr_choice_two_lit(
const K(&_str_a)[N + 1],
const P(&_str_b)[M + 1],
bool _choice)
1417 : str_a(_str_a), str_b(_str_b), choice(_choice){}
1419 constexpr size_t length()
const noexcept {
1420 return choice ? N : M;
1422 constexpr symb_type* place(symb_type* ptr)
const noexcept {
1424 if constexpr (N != 0) {
1425 std::char_traits<symb_type>::copy(ptr, str_a, N);
1429 if constexpr (M != 0) {
1430 std::char_traits<symb_type>::copy(ptr, (
const K(&)[M + 1])str_b, M);
1465template<StrExpr A, StrExprForType<
typename A::symb_type> B>
1475template<StrExpr A, typename T, size_t N = const_lit_for<typename A::symb_type, T>::Count>
1485template<StrExpr A, typename T, size_t N = const_lit_for<typename A::symb_type, T>::Count>
1494template<typename T, typename L, typename K = typename const_lit<T>::symb_type,
typename P =
typename const_lit<L>::symb_type,
1495 size_t N = const_lit<T>::Count,
size_t M = const_lit_for<typename const_lit<T>::symb_type, L>::Count>
1496 requires is_equal_str_type_v<K, P>
1498 return {str_a, str_b, c};
1552template<typename T, size_t N = const_lit<T>::Count>
1554 using K =
typename const_lit<T>::symb_type;
1555 const K empty[1] = {0};
1568template<
typename K, StdStrSourceForType<K> T>
1570 using symb_type = K;
1573 expr_stdstr(
const T& t) : t_(t){}
1575 constexpr size_t length()
const noexcept {
1578 constexpr symb_type* place(symb_type* p)
const noexcept {
1579 size_t s = t_.size();
1580 std::char_traits<K>::copy(p, (
const K*)t_.data(), s);
1595template<
typename K, StdStrSource T>
1596struct convert_to_strexpr<K, T> {
1601constexpr const size_t npos =
static_cast<size_t>(-1);
1605std::char_traits<K> char_traits_selector(...);
1608using ch_traits =
decltype(char_traits_selector<K>(
int(0)));
1611concept FromIntNumber =
1612 is_one_of_type<std::remove_cv_t<T>,
unsigned char, int, short, long,
long long, unsigned,
unsigned short,
unsigned long,
unsigned long long>::value;
1615concept ToIntNumber = FromIntNumber<T> || is_one_of_type<T, int8_t>::value;
1617template<
typename K,
bool I,
typename T>
1620 std::make_unsigned_t<T> val;
1621 constexpr need_sign(T t) : negate(t < 0), val(t < 0 ?
std::make_unsigned_t<T>{} - t : t) {}
1622 constexpr void after(K*& ptr) {
1628template<
typename K,
typename T>
1629struct need_sign<K, false, T> {
1631 constexpr need_sign(T t) : val(t){}
1632 constexpr void after(K*&) {}
1635template<
typename K,
typename T>
1636constexpr size_t fromInt(K* bufEnd, T val) {
1637 const char* twoDigit =
1638 "0001020304050607080910111213141516171819"
1639 "2021222324252627282930313233343536373839"
1640 "4041424344454647484950515253545556575859"
1641 "6061626364656667686970717273747576777879"
1642 "8081828384858687888990919293949596979899";
1644 need_sign<K, std::is_signed_v<T>, T> store(val);
1646 while (store.val >= 100) {
1647 const char* ptr = twoDigit + (store.val % 100) * 2;
1648 *--itr =
static_cast<K
>(ptr[1]);
1649 *--itr =
static_cast<K
>(ptr[0]);
1652 if (store.val < 10) {
1653 *--itr =
static_cast<K
>(
'0' + store.val);
1655 const char* ptr = twoDigit + store.val * 2;
1656 *--itr =
static_cast<K
>(ptr[1]);
1657 *--itr =
static_cast<K
>(ptr[0]);
1660 return size_t(bufEnd - itr);
1666template<
typename K,
typename T>
1668 using symb_type = K;
1669 using my_type = expr_num<K, T>;
1671 enum { bufSize = 24 };
1672 mutable K buf[bufSize];
1675 constexpr expr_num(T t) : value(t) {}
1676 constexpr expr_num(expr_num&& t) noexcept : value(t.value) {}
1678 constexpr size_t length() const noexcept {
1679 value =
static_cast<T
>(fromInt(buf + bufSize, value));
1680 return static_cast<size_t>(value);
1682 constexpr K* place(K* ptr)
const noexcept {
1683 size_t len =
static_cast<size_t>(value);
1684 ch_traits<K>::copy(ptr, buf + bufSize - len, len);
1699template<
typename K, FromIntNumber T>
1700struct convert_to_strexpr<K, T> {
1701 using type = expr_num<K, T>;
1719template<
typename K, FromIntNumber T>
1726enum class int_align { none, left, right, center };
1727enum class int_plus_sign {none, plus, space};
1728enum class int_prefix {none, lcase, ucase};
1730enum fmt_kinds :
unsigned {
1741template<
typename A,
typename B,
unsigned Kind>
1745 inline static constexpr unsigned kind = Kind;
1749 inline static constexpr unsigned kind = fmt_none;
1752template<
int_align A>
1754 inline static constexpr unsigned kind = fmt_align;
1757template<
size_t W
idth = 0>
1759 inline static constexpr unsigned kind = fmt_width;
1762template<
unsigned Fill = ' '>
1764 inline static constexpr unsigned kind = fmt_fill;
1767template<
int_plus_sign S>
1769 inline static constexpr unsigned kind = fmt_sign;
1773 inline static constexpr unsigned kind = fmt_upper;
1778 inline static constexpr unsigned kind = fmt_prefix;
1782 inline static constexpr unsigned kind = fmt_zero;
1786concept FmtParam =
requires {
1787 {std::remove_cvref_t<T>::kind} -> std::same_as<const unsigned&>;
1790template<FmtParam A, FmtParam B>
1791requires((A::kind & B::kind) == 0)
1792constexpr auto operator | (
const A&,
const B&) {
1793 return fmt_info<A, B, A::kind | B::kind>{};
1796inline constexpr align_info<int_align::left> l;
1797inline constexpr align_info<int_align::right> r;
1798inline constexpr align_info<int_align::center> c;
1801inline constexpr width_info<W> w;
1804inline constexpr fill_info<F> f;
1806inline constexpr sign_info<int_plus_sign::plus> sp;
1807inline constexpr sign_info<int_plus_sign::space> ss;
1808inline constexpr upper_info u;
1809inline constexpr prefix_info<int_prefix::lcase> p;
1810inline constexpr prefix_info<int_prefix::ucase> P;
1811inline constexpr zero_info z;
1813inline constexpr width_info<unsigned(-1)> wp;
1814inline constexpr fmt_default df;
1817struct extract_align : std::false_type {
1818 inline static constexpr int_align align = int_align::none;
1821template<
int_align A>
1822struct extract_align<align_info<A>> : std::true_type {
1823 inline static constexpr int_align align = A;
1826template<
typename A,
typename B,
unsigned U>
1827struct extract_align<fmt_info<A, B, U>> {
1828 inline static constexpr bool in_a = extract_align<A>::value, in_b = extract_align<B>::value, value = in_a | in_b;
1829 inline static constexpr int_align align = extract_align<std::conditional_t<in_a, A, std::conditional_t<in_b, B, align_info<int_align::none>>>>::align;
1833struct extract_width : std::false_type {
1834 inline static constexpr size_t width = 0;
1838struct extract_width<width_info<W>> : std::true_type {
1839 inline static constexpr size_t width = W;
1842template<
typename A,
typename B,
unsigned U>
1843struct extract_width<fmt_info<A, B, U>> {
1844 inline static constexpr bool in_a = extract_width<A>::value, in_b = extract_width<B>::value, value = in_a | in_b;
1845 inline static constexpr size_t width = extract_width<std::conditional_t<in_a, A, std::conditional_t<in_b, B, width_info<0>>>>::width;
1849struct extract_fill : std::false_type {
1850 inline static constexpr unsigned fill =
' ';
1854struct extract_fill<fill_info<F>> : std::true_type {
1855 inline static constexpr unsigned fill = F;
1858template<
typename A,
typename B,
unsigned U>
1859struct extract_fill<fmt_info<A, B, U>> {
1860 inline static constexpr bool in_a = extract_fill<A>::value, in_b = extract_fill<B>::value, value = in_a | in_b;
1861 inline static constexpr unsigned fill = extract_fill<std::conditional_t<in_a, A, std::conditional_t<in_b, B, fill_info<
' '>>>>::fill;
1865struct extract_sign : std::false_type {
1866 inline static constexpr int_plus_sign sign = int_plus_sign::none;
1869template<
int_plus_sign S>
1870struct extract_sign<sign_info<S>> : std::true_type {
1871 inline static constexpr int_plus_sign sign = S;
1874template<
typename A,
typename B,
unsigned U>
1875struct extract_sign<fmt_info<A, B, U>> {
1876 inline static constexpr bool in_a = extract_sign<A>::value, in_b = extract_sign<B>::value, value = in_a | in_b;
1877 inline static constexpr int_plus_sign sign = extract_sign<std::conditional_t<in_a, A, std::conditional_t<in_b, B, sign_info<int_plus_sign::none>>>>::sign;
1881struct extract_upper : std::false_type {};
1884struct extract_upper<upper_info> : std::true_type {};
1886template<
typename A,
typename B,
unsigned U>
1887struct extract_upper<fmt_info<A, B, U>> {
1888 inline static constexpr bool in_a = extract_upper<A>::value, in_b = extract_upper<B>::value,
1889 value = extract_upper<std::conditional_t<in_a, A, std::conditional_t<in_b, B, std::false_type>>>::value;
1893struct extract_prefix : std::false_type{
1894 inline static constexpr int_prefix prefix = int_prefix::none;
1897template<
int_prefix P>
1898struct extract_prefix<prefix_info<P>> : std::true_type {
1899 inline static constexpr int_prefix prefix = P;
1902template<
typename A,
typename B,
unsigned U>
1903struct extract_prefix<fmt_info<A, B, U>> {
1904 inline static constexpr bool in_a = extract_prefix<A>::value, in_b = extract_prefix<B>::value, value = in_a | in_b;
1905 inline static constexpr int_prefix prefix = extract_prefix<std::conditional_t<in_a, A, std::conditional_t<in_b, B, prefix_info<int_prefix::none>>>>::prefix;
1909struct extract_zero : std::false_type{};
1912struct extract_zero<zero_info> : std::true_type {};
1914template<
typename A,
typename B,
unsigned U>
1915struct extract_zero<fmt_info<A, B, U>> {
1916 inline static constexpr bool in_a = extract_zero<A>::value, in_b = extract_zero<B>::value,
1917 value = extract_zero<std::conditional_t<in_a, A, std::conditional_t<in_b, B, std::false_type>>>::value;
1920template<
int_align Align,
unsigned W
idth,
unsigned Fill,
int_plus_sign Sign,
bool Upper,
int_prefix Prefix,
bool Zero>
1922 inline static constexpr int_align align = Align;
1923 inline static constexpr unsigned width = Width;
1924 inline static constexpr unsigned fill = Fill;
1925 inline static constexpr int_plus_sign sign = Sign;
1926 inline static constexpr bool upper = Upper;
1927 inline static constexpr int_prefix prefix = Prefix;
1928 inline static constexpr bool zero = Zero;
1932using p_to_fmt_t = fmt_params<
1933 extract_align<T>::align,
1934 extract_width<T>::width,
1935 extract_fill<T>::fill,
1936 extract_sign<T>::sign,
1937 extract_upper<T>::value,
1938 extract_prefix<T>::prefix,
1939 extract_zero<T>::value>;
1942using to_fmt_t = p_to_fmt_t<std::remove_cvref_t<T>>;
1945struct is_fmt_params : std::false_type{};
1947template<
int_align Align,
unsigned W
idth,
unsigned Fill,
int_plus_sign Sign,
bool Upper,
int_prefix Prefix,
bool Zero>
1948struct is_fmt_params<fmt_params<Align, Width, Fill, Sign, Upper, Prefix, Zero>> : std::true_type{};
1951concept FmtParamSet = is_fmt_params<T>::value;
1955template<f::FmtParam F>
1956constexpr auto operator|(
const F& f, fmt_end) {
1960template<
char C = 0,
char...Chars>
1961constexpr auto parse_fmt_symbol();
1963template<
unsigned N,
char C =
'Z',
char...Chars>
1964constexpr auto parse_width() {
1965 if constexpr (C >=
'0' && C <=
'9') {
1966 return parse_width<N * 10 + C -
'0', Chars...>();
1968 return w<N> | parse_fmt_symbol<C, Chars...>();
1972template<
unsigned N,
char C =
'Z',
char...Chars>
1973constexpr auto parse_fill() {
1974 if constexpr (C >=
'0' && C <=
'9') {
1975 return parse_fill<N * 16 + C -
'0', Chars...>();
1976 }
else if constexpr (C >=
'a' && C <=
'f') {
1977 return parse_fill<N * 16 + C -
'a' + 10, Chars...>();
1978 }
else if constexpr (C >=
'A' && C <=
'F') {
1979 return parse_fill<N * 16 + C -
'A' + 10, Chars...>();
1981 return f<N> | parse_fmt_symbol<C, Chars...>();
1985template<
char C,
char...Chars>
1986constexpr auto parse_fmt_symbol() {
1987 if constexpr (C ==
'0') {
1988 return z | parse_fmt_symbol<Chars...>();
1989 }
else if constexpr (C ==
'a') {
1990 return p | parse_fmt_symbol<Chars...>();
1991 }
else if constexpr (C ==
'A') {
1992 return P | parse_fmt_symbol<Chars...>();
1993 }
else if constexpr (C ==
'b') {
1994 return l | parse_fmt_symbol<Chars...>();
1995 }
else if constexpr (C ==
'c') {
1996 return c | parse_fmt_symbol<Chars...>();
1997 }
else if constexpr (C ==
'd') {
1998 return r | parse_fmt_symbol<Chars...>();
1999 }
else if constexpr (C ==
'e') {
2000 return sp | parse_fmt_symbol<Chars...>();
2001 }
else if constexpr (C ==
'f') {
2002 return ss | parse_fmt_symbol<Chars...>();
2003 }
else if constexpr (C ==
'E') {
2004 return u | parse_fmt_symbol<Chars...>();
2005 }
else if constexpr (C ==
'\'') {
2006 return parse_fmt_symbol<Chars...>();
2007 }
else if constexpr (C ==
'F') {
2008 return parse_fill<0, Chars...>();
2009 }
else if constexpr (C >=
'1' && C <=
'9') {
2010 return parse_width<C -
'0', Chars...>();
2016template<
unsigned R,
typename T>
2017struct fmt_radix_info {};
2019template<
unsigned N,
char C = 0,
char...Chars>
2020constexpr auto parse_radix() {
2021 if constexpr (C >=
'0' && C <=
'9') {
2022 return parse_radix<N * 10 + C -
'0', Chars...>();
2023 }
else if constexpr (C == 0) {
2024 return fmt_radix_info<N, to_fmt_t<fmt_default>>{};
2026 return fmt_radix_info<N,
decltype(parse_fmt_symbol<C, Chars...>())>{};
2030template<
char C1,
char C2,
char C3,
char...Chars>
2031constexpr auto skip_0x() {
2032 static_assert(C1 ==
'0' && C2 ==
'x' &&
"Fmt symbols must begin with 0x");
2033 static_assert(C3 >=
'1' && C3 <=
'9' &&
"Radix must begin with 1-9");
2034 return parse_radix<C3 -
'0', Chars...>();
2039template<
typename K,
bool Ucase>
2040inline constexpr K digits_symbols[36] = {K(
'0'), K(
'1'), K(
'2'), K(
'3'), K(
'4'), K(
'5'), K(
'6'), K(
'7'), K(
'8'), K(
'9'),
2041 K(Ucase ?
'A' :
'a'), K(Ucase ?
'B' :
'b'), K(Ucase ?
'C' :
'c'), K(Ucase ?
'D' :
'd'), K(Ucase ?
'E' :
'e'),
2042 K(Ucase ?
'F' :
'f'), K(Ucase ?
'G' :
'g'), K(Ucase ?
'H' :
'h'), K(Ucase ?
'I' :
'i'), K(Ucase ?
'J' :
'j'),
2043 K(Ucase ?
'K' :
'k'), K(Ucase ?
'L' :
'l'), K(Ucase ?
'M' :
'm'), K(Ucase ?
'N' :
'n'), K(Ucase ?
'O' :
'o'),
2044 K(Ucase ?
'P' :
'p'), K(Ucase ?
'Q' :
'q'), K(Ucase ?
'R' :
'r'), K(Ucase ?
'S' :
's'), K(Ucase ?
'T' :
't'),
2045 K(Ucase ?
'U' :
'u'), K(Ucase ?
'V' :
'v'), K(Ucase ?
'W' :
'w'), K(Ucase ?
'X' :
'x'), K(Ucase ?
'Y' :
'y'),
2046 K(Ucase ?
'Z' :
'z'),
2049template<FromIntNumber T,
unsigned Radix, f::FmtParamSet FP>
requires (Radix > 1 && Radix <= 36)
2050struct expr_integer_src {
2053 constexpr expr_integer_src(T v) : value_(v){}
2054 constexpr expr_integer_src(T v,
unsigned w) : value_(v), width_(w){}
2056 template<is_std_
string_v S>
2059 template<
typename S>
requires std::is_constructible_v<S, empty_expr<typename S::symb_type>>
2063template<is_one_of_
char_v K, FromIntNumber T,
unsigned Radix, f::FmtParamSet FP>
2064requires (Radix > 1 && Radix <= 36)
2066 using symb_type = K;
2068 enum { bufSize = 64 };
2069 mutable K buf[bufSize];
2073 mutable bool negate_{};
2075 constexpr expr_integer(T t) : value_(t){}
2076 constexpr expr_integer(T t,
unsigned w) : value_(t), width_(w){}
2077 constexpr expr_integer(
const expr_integer_src<T, Radix, FP>& v) : value_(v.value_), width_(v.width_){}
2079 constexpr expr_integer(expr_integer&& t) noexcept : value_(t.value_), width_(t.width_){}
2081 constexpr size_t length() const noexcept {
2082 K* bufEnd = std::end(buf), *itr = bufEnd;
2085 need_sign<K, std::is_signed_v<T>, T> store(value_);
2087 *--itr = digits_symbols<K, FP::upper>[store.val % Radix];
2090 if constexpr (std::is_signed_v<T>) {
2091 negate_ = store.negate;
2094 *--itr = digits_symbols<K, FP::upper>[0];
2096 size_t len = bufEnd - itr;
2098 if constexpr (std::is_signed_v<T>) {
2099 if constexpr (FP::sign != f::int_plus_sign::none) {
2107 if constexpr (FP::prefix != f::int_prefix::none && (Radix == 2 || Radix == 8 || Radix == 16)) {
2109 if constexpr (Radix != 8) {
2113 if constexpr (FP::width == unsigned(-1)) {
2114 return std::max<size_t>(width_, len);
2116 return std::max<size_t>(FP::width, len);
2119 constexpr K* place(K* ptr)
const noexcept {
2120 size_t len =
static_cast<size_t>(value_), all_len = len;
2121 if constexpr (std::is_signed_v<T>) {
2122 if constexpr (FP::sign != f::int_plus_sign::none) {
2130 if constexpr (FP::prefix != f::int_prefix::none && (Radix == 2 || Radix == 8 || Radix == 16)) {
2132 if constexpr (Radix != 8) {
2136 if constexpr (FP::zero) {
2137 if constexpr (std::is_signed_v<T>) {
2141 if constexpr (FP::sign == f::int_plus_sign::plus) {
2143 }
else if constexpr (FP::sign == f::int_plus_sign::space) {
2148 if constexpr (FP::prefix != f::int_prefix::none && (Radix == 2 || Radix == 8 || Radix == 16)) {
2150 if constexpr (Radix == 2) {
2151 *ptr++ = FP::prefix == f::int_prefix::lcase ? K(
'b') : K(
'B');
2152 }
else if constexpr (Radix == 16) {
2153 *ptr++ = FP::prefix == f::int_prefix::lcase ? K(
'x') : K(
'X');
2157 if constexpr (FP::width == unsigned(-1)) {
2158 if (width_ > all_len) {
2159 before = width_ - all_len;
2162 if (FP::width > all_len) {
2163 before = FP::width - all_len;
2167 ch_traits<K>::assign(ptr, before, K(
'0'));
2170 ch_traits<K>::copy(ptr, std::end(buf) - len, len);
2173 size_t before = 0, after = 0;
2174 if constexpr (FP::width == unsigned(-1)) {
2175 if (width_ > all_len) {
2176 if constexpr (FP::align == f::int_align::left) {
2177 after = width_ - all_len;
2178 }
else if constexpr (FP::align == f::int_align::center) {
2179 before = (width_ - all_len) / 2;
2180 after = width_ - all_len - before;
2182 before = width_ - all_len;
2186 if (FP::width > all_len) {
2187 if constexpr (FP::align == f::int_align::left) {
2188 after = FP::width - all_len;
2189 }
else if constexpr (FP::align == f::int_align::center) {
2190 before = (FP::width - all_len) / 2;
2191 after = FP::width - all_len - before;
2193 before = FP::width - all_len;
2198 ch_traits<K>::assign(ptr, before, K(FP::fill));
2201 if constexpr (std::is_signed_v<T>) {
2205 if constexpr (FP::sign == f::int_plus_sign::plus) {
2207 }
else if constexpr (FP::sign == f::int_plus_sign::space) {
2212 if constexpr (FP::prefix != f::int_prefix::none && (Radix == 2 || Radix == 8 || Radix == 16)) {
2214 if constexpr (Radix == 2) {
2215 *ptr++ = FP::prefix == f::int_prefix::lcase ? K(
'b') : K(
'B');
2216 }
else if constexpr (Radix == 16) {
2217 *ptr++ = FP::prefix == f::int_prefix::lcase ? K(
'x') : K(
'X');
2220 ch_traits<K>::copy(ptr, std::end(buf) - len, len);
2224 ch_traits<K>::assign(ptr, after, K(FP::fill));
2232template<FromIntNumber T,
unsigned Radix, f::FmtParamSet FP>
requires (Radix > 1 && Radix <= 36)
2233template<is_std_string_v S>
2234expr_integer_src<T, Radix, FP>::operator S()
const {
2235 using st =
typename S::value_type;
2236 using Al =
typename S::allocator_type;
2237 return to_std_string<st, Al>(expr_integer<st, T, Radix, FP>{value_, width_});
2240template<FromIntNumber T,
unsigned Radix, f::FmtParamSet FP>
requires (Radix > 1 && Radix <= 36)
2242expr_integer_src<T, Radix, FP>::operator S()
const {
2243 using st =
typename S::symb_type;
2244 return S{expr_integer<st, T, Radix, FP>{value_, width_}};
2247template<
typename K, FromIntNumber T,
unsigned Radix, f::FmtParamSet FP>
2248struct convert_to_strexpr<K, expr_integer_src<T, Radix, FP>> {
2249 using type = expr_integer<K, T, Radix, FP>;
2252template<
unsigned R,
typename T,
typename F>
2253struct flags_checker {
2255 using flags_t = f::to_fmt_t<F>;
2256 inline static constexpr unsigned Radix = R;
2259template<
typename T,
bool ArgW
idth>
2260concept good_int_flags =
2261 T::Radix > 1 && T::Radix <= 36
2264 && (T::flags_t::align == f::int_align::none || !T::flags_t::zero)
2267 && (std::is_signed_v<typename T::val_t> || T::flags_t::sign == f::int_plus_sign::none)
2270 && (T::flags_t::width == unsigned(-1)) == ArgWidth
2273 && (T::flags_t::prefix == f::int_prefix::none || T::Radix == 2 || T::Radix == 8 || T::Radix == 16);
2382template<
unsigned R, auto fp = f::df, FromIntNumber T>
requires good_int_flags<flags_checker<R, T,
decltype(fp)>,
false>
2384 using fmt_param = f::to_fmt_t<
decltype(fp)>;
2385 return expr_integer_src<T, R, fmt_param>{v};
2391template<
unsigned R, auto fp = f::df, FromIntNumber T>
requires good_int_flags<flags_checker<R, T,
decltype(fp)>,
true>
2393 using fmt_param = f::to_fmt_t<
decltype(fp)>;
2394 return expr_integer_src<T, R, fmt_param>{v, w};
2397template<
typename T,
unsigned R,
typename F>
requires good_int_flags<flags_checker<R, T, F>,
false>
2398auto operator / (T v,
const f::fmt_radix_info<R, F>&) {
2399 return expr_integer_src<T, R, f::to_fmt_t<F>>{v};
2404 using symb_type = K;
2405 mutable u8s buf[40];
2408 constexpr expr_real(
double d) : v(d) {}
2409 constexpr expr_real(
float d) : v(d) {}
2411 size_t length() const noexcept {
2412 auto [ptr, ec] = std::to_chars(buf, buf + std::size(buf), v);
2413 l = ec != std::errc{} ? 0 : ptr - buf;
2416 K* place(K* ptr)
const noexcept {
2417 if constexpr (
sizeof(K) ==
sizeof(buf[0])) {
2418 ch_traits<K>::copy(ptr, (K*)buf, l);
2420 for (
size_t i = 0; i < l; i++) {
2438template<
typename K, std::
floating_po
int T>
2439struct convert_to_strexpr<K, T> {
2440 using type = expr_real<K>;
2454template<
typename K>
requires is_one_of_char_v<K>
2455inline constexpr expr_real<K>
e_num(
double t) {
2459template<FromIntNumber Val,
bool All,
bool Ucase,
bool Ox>
2460struct expr_hex_src {
2461 explicit constexpr expr_hex_src(Val v) : v_(v){}
2465template<
typename K, FromIntNumber Val,
bool All,
bool Ucase,
bool Ox>
2467 using symb_type = K;
2468 mutable need_sign<K, std::is_signed_v<Val>, Val> v_;
2469 mutable K buf_[
sizeof(Val) * 2]{};
2471 explicit constexpr expr_hex(Val v) : v_(v){}
2472 constexpr expr_hex(
const expr_hex_src<Val, All, Ucase, Ox>& v) : v_(v.v_){}
2474 constexpr size_t length() const noexcept {
2475 K *ptr = buf_ + std::size(buf_);
2478 *--ptr = digits_symbols<K, Ucase>[v_.val & 0xF];
2482 *--ptr = digits_symbols<K, Ucase>[v_.val & 0xF];
2487 if constexpr (All) {
2488 if (
size_t need =
sizeof(Val) * 2 - l) {
2489 ch_traits<K>::assign(buf_, need, K(
'0'));
2491 l =
sizeof(Val) * 2;
2497 if constexpr (std::is_signed_v<Val>) {
2498 return l + (Ox ? 2 : 0) + (v_.negate ? 1 : 0);
2500 return l + (Ox ? 2 : 0);
2502 constexpr K* place(K* ptr)
const noexcept {
2503 if constexpr (std::is_signed_v<Val>) {
2512 if constexpr (All) {
2513 ch_traits<K>::copy(ptr, buf_,
sizeof(Val) * 2);
2514 return ptr +
sizeof(Val) * 2;
2516 ch_traits<K>::copy(ptr, buf_ + std::size(buf_) - v_.val, v_.val);
2517 return ptr + v_.val;
2583template<
unsigned Flags = 0, FromIntNumber T>
2585 return expr_hex_src<T, (Flags &
HexFlags::Short) == 0, (Flags & HexFlags::Lcase) == 0, (Flags & HexFlags::No0x) == 0>{v};
2588template<
char c = 0,
char...Chars>
2589constexpr unsigned parse_f16_flags() {
2590 if constexpr (c ==
'1') {
2592 }
else if constexpr (c ==
'2') {
2593 return HexFlags::Lcase | parse_f16_flags<Chars...>();
2594 }
else if constexpr (c ==
'3') {
2595 return HexFlags::No0x | parse_f16_flags<Chars...>();
2604template<FromIntNumber T,
unsigned Flags>
2605constexpr auto operator/(T v,
const f16flags<Flags>&) {
2606 return expr_hex_src<T, (Flags &
HexFlags::Short) == 0, (Flags & HexFlags::Lcase) == 0, (Flags & HexFlags::No0x) == 0>{v};
2609inline namespace literals {
2610template<
char...Chars>
2611constexpr auto operator""_f16() {
2612 return f16flags<parse_f16_flags<Chars...>()>{};
2624template<
typename K,
typename Val,
bool All,
bool Ucase,
bool Ox>
2625struct convert_to_strexpr<K, expr_hex_src<Val, All, Ucase, Ox>> {
2626 using type = expr_hex<K, Val, All, Ucase, Ox>;
2630struct expr_pointer : expr_hex<K, uintptr_t, true, true, true> {
2631 constexpr expr_pointer(
const void* ptr) : expr_hex<K, uintptr_t, true, true, true>((uintptr_t)ptr){}
2642template<
typename K,
typename T>
2643struct convert_to_strexpr<K, const T*> {
2644 using type = expr_pointer<K>;
2647template<
typename K, StrExprForType<K> A,
bool Left>
2649 using symb_type = K;
2653 mutable size_t alen_{};
2654 constexpr expr_fill(K symbol,
size_t width,
const A& a) : symbol_(symbol), width_(width), a_(a){}
2656 constexpr size_t length() const noexcept {
2657 alen_ = a_.length();
2658 return std::max(alen_, width_);
2660 constexpr K* place(K* ptr)
const noexcept {
2661 if (alen_ >= width_) {
2662 return (K*)a_.place((
typename A::symb_type*)ptr);
2664 size_t w = width_ - alen_;
2665 if constexpr (Left) {
2666 ch_traits<K>::assign(ptr, w, symbol_);
2668 return (K*)a_.place((
typename A::symb_type*)ptr);
2670 ptr = (K*)a_.place((
typename A::symb_type*)ptr);
2671 ch_traits<K>::assign(ptr, w, symbol_);
2698template<StrExpr A,
typename K =
typename A::symb_type>
2699expr_fill<K, A, true>
e_fill_left(
const A& a,
size_t width, K symbol = K(
' ')) {
2700 return {symbol, width, a};
2724template<StrExpr A,
typename K =
typename A::symb_type>
2725expr_fill<K, A, false>
e_fill_right(
const A& a,
size_t width, K symbol = K(
' ')) {
2726 return {symbol, width, a};
2745template<
typename K,
typename T,
size_t I,
bool tail,
bool skip_empty>
2746struct expr_join : expr_to_std_string<expr_join<K, T, I, tail, skip_empty>> {
2747 using symb_type = K;
2748 using my_type = expr_join<K, T, I, tail, skip_empty>;
2752 constexpr expr_join(
const T& _s,
const K* _delim) : s(_s), delim(_delim){}
2754 constexpr size_t length() const noexcept {
2756 for (
const auto& t: s) {
2757 size_t len = t.length();
2758 if (len > 0 || !skip_empty) {
2759 if (I > 0 && l > 0) {
2765 return l + (tail && I > 0 && (l > 0 || (!skip_empty && s.size() > 0))? I : 0);
2767 constexpr K* place(K* ptr)
const noexcept {
2772 for (
const auto& t: s) {
2773 size_t copyLen = t.length();
2774 if (I > 0 && write != ptr && (copyLen || !skip_empty)) {
2775 ch_traits<K>::copy(write, delim, I);
2778 ch_traits<K>::copy(write, t.data(), copyLen);
2781 if (I > 0 && tail && (write != ptr || (!skip_empty && s.size() > 0))) {
2782 ch_traits<K>::copy(write, delim, I);
2802template<bool tail = false, bool skip_empty = false, typename L, typename K = typename const_lit<L>::symb_type,
size_t I = const_lit<L>::Count,
typename T>
2803inline constexpr auto e_join(
const T& s, L&& d) {
2804 return expr_join<K, T, I - 1, tail, skip_empty>{s, d};
2808concept is_const_pattern = N > 1 && N <= 17;
2810template<
typename K,
size_t I>
2812 constexpr static const size_t value = size_t(K(~0x7F)) << ((I - 1) *
sizeof(K) * 8) | _ascii_mask<K, I - 1>::value;
2816struct _ascii_mask<K, 0> {
2817 constexpr static const size_t value = 0;
2822 using uns = std::make_unsigned_t<K>;
2823 constexpr static const size_t WIDTH =
sizeof(size_t) /
sizeof(uns);
2824 constexpr static const size_t VALUE = _ascii_mask<uns, WIDTH>::value;
2828constexpr inline bool isAsciiUpper(K k) {
2829 return k >=
'A' && k <=
'Z';
2833constexpr inline bool isAsciiLower(K k) {
2834 return k >=
'a' && k <=
'z';
2838constexpr inline K makeAsciiLower(K k) {
2839 return isAsciiUpper(k) ? k | 0x20 : k;
2843constexpr inline K makeAsciiUpper(K k) {
2844 return isAsciiLower(k) ? k & ~0x20 : k;
2847enum TrimSides { TrimLeft = 1, TrimRight = 2, TrimAll = 3 };
2848template<TrimS
ides S,
typename K,
size_t N,
bool withSpaces = false>
2849struct trim_operator;
2852struct digits_selector {
2853 using wider_type = uint16_t;
2857struct digits_selector<2> {
2858 using wider_type = uint32_t;
2862struct digits_selector<4> {
2863 using wider_type = uint64_t;
2877template<
bool CanNegate,
bool CheckOverflow,
typename T>
2878struct result_type_selector {
2883struct result_type_selector<true, false, T> {
2884 using type = std::make_unsigned_t<T>;
2887template<
unsigned Base>
2888constexpr unsigned digit_width() {
2907template<
typename T,
unsigned Base>
2908constexpr unsigned max_overflow_digits = (
sizeof(T) * CHAR_BIT) / digit_width<Base>();
2911struct convert_result {
2918 inline static constexpr uint8_t NUMBERS[] = {
2919 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
2920 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3,
2921 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
2922 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16,
2923 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 255, 255, 255, 255, 255, 255, 255,
2924 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
2925 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
2926 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
2927 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
2928 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
2930 template<
typename K,
unsigned Base>
2931 static constexpr std::make_unsigned_t<K> toDigit(K s) {
2932 auto us =
static_cast<std::make_unsigned_t<K>
>(s);
2933 if constexpr (Base <= 10) {
2936 if constexpr (
sizeof(K) == 1) {
2939 return us < 256 ? NUMBERS[us] : us;
2944 template<
typename K, ToIntNumber T,
unsigned Base,
bool CheckOverflow>
2946 static constexpr convert_result<T> parse(
const K* start,
const K* current,
const K* end,
bool negate) {
2947 using u_type = std::make_unsigned_t<T>;
2948 #ifndef HAS_BUILTIN_OVERFLOW
2949 u_type maxMult = 0, maxAdd = 0;
2950 if constexpr (CheckOverflow) {
2951 maxMult = std::numeric_limits<u_type>::max() / Base;
2952 maxAdd = std::numeric_limits<u_type>::max() % Base;
2956 unsigned maxDigits = max_overflow_digits<u_type, Base>;
2958 const K* from = current;
2960 bool no_need_check_o_f = !CheckOverflow || end - current <= maxDigits;
2962 if (no_need_check_o_f) {
2964 const u_type digit = toDigit<K, Base>(*current);
2965 if (digit >= Base) {
2968 number = number * Base + digit;
2969 if (++current == end) {
2975 for (;maxDigits; maxDigits--) {
2976 const u_type digit = toDigit<K, Base>(*current);
2977 if (digit >= Base) {
2980 number = number * Base + digit;
2987 const u_type digit = toDigit<K, Base>(*current);
2988 if (digit >= Base) {
2991 #ifdef HAS_BUILTIN_OVERFLOW
2992 if (__builtin_mul_overflow(number, Base, &number) ||
2993 __builtin_add_overflow(number, digit, &number)) {
2995 if (number < maxMult || (number == maxMult && number < maxAdd)) {
2996 number = number * Base + digit;
3000 while(++current < end) {
3001 if (toDigit<K, Base>(*current) >= Base) {
3007 if (++current == end) {
3015 if constexpr (std::is_signed_v<T>) {
3016 result = negate ? 0 - number : number;
3017 if constexpr (CheckOverflow) {
3019 if (number > (u_type)std::numeric_limits<T>::max() + (negate ? 1 : 0)) {
3030 return {result, error, size_t(current - start)};
3037 template<
typename K, ToIntNumber T,
unsigned Base = 0,
bool CheckOverflow = true,
bool SkipWs = true,
bool AllowSign = true>
3038 requires(Base == -1 || (Base < 37 && Base != 1))
3039 static constexpr convert_result<T> to_integer(
const K* start,
size_t len)
noexcept {
3040 const K *ptr = start, *end = ptr + len;
3041 bool negate =
false;
3042 if constexpr (SkipWs) {
3043 while (ptr < end && std::make_unsigned_t<K>(*ptr) <=
' ')
3047 if constexpr (std::is_signed_v<T>) {
3048 if constexpr (AllowSign) {
3053 }
else if (*ptr ==
'-') {
3065 }
else if constexpr (AllowSign) {
3074 if constexpr (Base == 0 || Base == -1) {
3078 if (*ptr ==
'x' || *ptr ==
'X') {
3079 return parse<K, T, 16, CheckOverflow>(start, ++ptr, end, negate);
3081 if constexpr (Base == -1) {
3082 if (*ptr ==
'b' || *ptr ==
'B') {
3083 return parse<K, T, 2, CheckOverflow>(start, ++ptr, end, negate);
3085 if (*ptr ==
'o' || *ptr ==
'O') {
3086 return parse<K, T, 8, CheckOverflow>(start, ++ptr, end, negate);
3089 return parse<K, T, 8, CheckOverflow>(start, --ptr, end, negate);
3093 return parse<K, T, 10, CheckOverflow>(start, ptr, end, negate);
3095 return parse<K, T, Base, CheckOverflow>(start, ptr, end, negate);
3101template<
typename K,
typename Impl>
3102class null_terminated {
3110 constexpr const K* c_str()
const {
return static_cast<const Impl*
>(
this)->symbols(); }
3113template<
typename K,
typename Impl,
bool Mutable>
class buffer_pointers;
3123template<
typename K,
typename Impl>
3124class buffer_pointers<K, Impl, false> {
3125 constexpr const Impl& d()
const {
return *
static_cast<const Impl*
>(
this); }
3133 constexpr const K* data()
const {
return d().symbols(); }
3140 constexpr const K* begin()
const {
return d().symbols(); }
3147 constexpr const K* end()
const {
return d().symbols() + d().length(); }
3154 constexpr const K* cbegin()
const {
return d().symbols(); }
3161 constexpr const K* cend()
const {
return d().symbols() + d().length(); }
3164template<
typename K,
typename Impl>
3165class buffer_pointers<K, Impl, true> :
public buffer_pointers<K, Impl, false> {
3166 constexpr Impl& d() {
return *
static_cast<Impl*
>(
this); }
3167 using base = buffer_pointers<K, Impl, false>;
3175 constexpr const K* data()
const {
return base::data(); }
3182 constexpr const K* begin()
const {
return base::begin(); }
3189 constexpr const K* end()
const {
return base::end(); }
3196 constexpr const K* cbegin()
const {
return base::cbegin(); }
3203 constexpr const K* cend()
const {
return base::cend(); }
3210 constexpr K* data() {
return d().str(); }
3217 constexpr K* begin() {
return d().str(); }
3224 constexpr K* end() {
return d().str() + d().length(); }
3233template<
typename K,
typename StrSrc>
3235 using str_t = StrSrc;
3240 constexpr SplitterBase(str_t text, str_t delim) : text_(text), delim_(delim) {}
3246 return text_.length() == str::npos;
3255 if (!text_.length()) {
3260 }
else if (text_.length() == str::npos) {
3261 return {
nullptr, 0};
3263 size_t pos = text_.find(delim_),
next = 0;
3264 if (pos == str::npos) {
3265 pos = text_.length();
3268 next = pos + delim_.length();
3270 str_t result{text_.str, pos};
3283inline constexpr from_end to_end{0};
3286concept first_index_in_str = std::is_same_v<T, from_end> || std::is_convertible_v<T, size_t>;
3289concept last_index_in_str = first_index_in_str<T> || std::is_same_v<T, from_begin>;
3315template<
typename K,
typename StrRef,
typename Impl,
bool Mutable>
3316class str_src_algs :
public buffer_pointers<K, Impl, Mutable> {
3317 constexpr const Impl& d()
const noexcept {
3318 return *
static_cast<const Impl*
>(
this);
3320 constexpr size_t _len()
const noexcept {
3321 return d().length();
3323 constexpr const K* _str()
const noexcept {
3324 return d().symbols();
3326 constexpr bool _is_empty()
const noexcept {
3327 return d().is_empty();
3331 using symb_type = K;
3332 using str_piece = StrRef;
3333 using traits = ch_traits<K>;
3334 using uns_type = std::make_unsigned_t<K>;
3335 using my_type = Impl;
3336 using base = str_src_algs<K, StrRef, Impl, Mutable>;
3337 str_src_algs() =
default;
3351 constexpr K*
place(K* ptr)
const noexcept {
3352 size_t myLen = _len();
3353 traits::copy(ptr, _str(), myLen);
3367 size_t tlen = std::min(_len(), bufSize - 1);
3368 traits::copy(buffer, _str(), tlen);
3388 constexpr std::basic_string_view<D>
to_sv() const noexcept {
3389 return {(
const D*)_str(), _len()};
3398 constexpr operator std::basic_string_view<D, Traits>()
const {
3399 return {(
const D*)_str(), _len()};
3407 template<
typename D = K,
typename Traits = std::
char_traits<D>,
typename Allocator = std::allocator<D>>
requires is_equal_str_type_v<K, D>
3408 constexpr std::basic_string<D, Traits, Allocator>
to_string()
const {
3409 return {(
const D*)_str(), _len()};
3418 constexpr operator std::basic_string<D, Traits, Allocator>()
const {
3419 return {(
const D*)_str(), _len()};
3427 constexpr operator str_piece() const noexcept {
3428 return str_piece{_str(), _len()};
3436 constexpr str_piece
to_str() const noexcept {
3437 return {_str(), _len()};
3462 [[deprecated(
"Use 'get' or [start, end] in C++23")]]
3463 constexpr str_piece
operator()(ptrdiff_t from, ptrdiff_t len = 0) const noexcept {
3464 size_t myLen = _len(), idxStart = from >= 0 ? from : (ptrdiff_t)myLen > -from ? myLen + from : 0,
3465 idxEnd = len > 0 ? idxStart + len : (ptrdiff_t)myLen > -len ? myLen + len : 0;
3468 if (idxStart > idxEnd)
3470 return str_piece{_str() + idxStart, idxEnd - idxStart};
3504 template<first_index_in_str S, last_index_in_str E>
3505 constexpr str_piece
get(S start, E end)
const noexcept {
3506 size_t idxStart, idxEnd, len = _len();
3507 if constexpr (std::is_same_v<S, from_end>) {
3508 idxStart = start.idx_ > len ? 0 : len - start.idx_;
3510 idxStart = std::min(
size_t(start), len);
3513 if constexpr (std::is_same_v<E, from_begin>) {
3514 idxEnd = std::max(idxStart, std::min(end.idx_, len));
3515 }
else if constexpr (std::is_same_v<E, from_end>) {
3516 idxEnd = std::max(idxStart, end.idx_ > len ? 0 : len - end.idx_);
3519 idxEnd = len - idxStart < e ? len : idxStart + e;
3522 return str_piece{_str() + idxStart, idxEnd - idxStart};
3525#if defined(__cpp_multidimensional_subscript) && __cpp_multidimensional_subscript >= 202211L
3530 template<first_index_in_str S, last_index_in_str E>
3531 constexpr str_piece operator[](S start, E end)
const noexcept {
3532 return get(start, end);
3539 constexpr str_piece
prefix(
size_t count)
const noexcept {
3540 return get(0, count);
3546 constexpr str_piece
suffix(
size_t count)
const noexcept {
3547 return get(from_end(count), to_end);
3559 constexpr str_piece
mid(
size_t from,
size_t len = -1) const noexcept {
3560 size_t myLen = _len(), idxStart = from, idxEnd = from > std::numeric_limits<size_t>::max() - len ? myLen : from + len;
3563 if (idxStart > idxEnd)
3565 return str_piece{_str() + idxStart, idxEnd - idxStart};
3581 constexpr str_piece
from_to(
size_t from,
size_t to)
const noexcept {
3582 return str_piece{_str() + from, to - from};
3594 constexpr str_piece
until(str_piece pattern,
size_t offset = 0) const noexcept {
3614 constexpr K
at(ptrdiff_t idx)
const {
3615 return _str()[idx >= 0 ? idx : _len() + idx];
3619 constexpr int compare(
const K* text,
size_t len)
const {
3620 size_t myLen = _len();
3621 int cmp = traits::compare(_str(), text, std::min(myLen, len));
3622 return cmp == 0 ? (myLen > len ? 1 : myLen == len ? 0 : -1) : cmp;
3633 return compare(o.symbols(), o.length());
3644 size_t myLen = _len(), idx = 0;
3645 const K* ptr = _str();
3646 for (; idx < myLen; idx++) {
3647 uns_type s1 = (uns_type)text[idx];
3651 uns_type s2 = (uns_type)ptr[idx];
3654 }
else if (s1 > s2) {
3658 return text[idx] == 0 ? 0 : -1;
3661 constexpr bool equal(
const K* text,
size_t len)
const noexcept {
3662 return len == _len() && traits::compare(_str(), text, len) == 0;
3672 constexpr bool equal(str_piece other)
const noexcept {
3673 return equal(other.symbols(), other.length());
3684 return equal(other._str(), other._len());
3693 return compare(other._str(), other._len()) <=> 0;
3701 template<typename T, size_t N = const_lit_for<K, T>::Count>
3703 return N - 1 == _len() && traits::compare(_str(), (
const K*)other, N - 1) == 0;
3711 template<typename T, size_t N = const_lit_for<K, T>::Count>
3713 size_t myLen = _len();
3714 int cmp = traits::compare(_str(), (
const K*)other, std::min(myLen, N - 1));
3715 int res = cmp == 0 ? (myLen > N - 1 ? 1 : myLen == N - 1 ? 0 : -1) : cmp;
3721 constexpr int compare_ia(
const K* text,
size_t len)
const noexcept {
3723 return _is_empty() ? 0 : 1;
3724 size_t myLen = _len(), checkLen = std::min(myLen, len);
3725 const uns_type *ptr1 =
reinterpret_cast<const uns_type*
>(_str()), *ptr2 =
reinterpret_cast<const uns_type*
>(text);
3726 while (checkLen--) {
3727 uns_type s1 = *ptr1++, s2 = *ptr2++;
3730 s1 = makeAsciiLower(s1);
3731 s2 = makeAsciiLower(s2);
3737 return myLen == len ? 0 : myLen > len ? 1 : -1;
3748 return compare_ia(text.symbols(), text.length());
3759 constexpr bool equal_ia(str_piece text)
const noexcept {
3760 return text.length() == _len() && compare_ia(text.symbols(), text.length()) == 0;
3770 constexpr bool less_ia(str_piece text)
const noexcept {
3771 return compare_ia(text.symbols(), text.length()) < 0;
3774 constexpr size_t find(
const K* pattern,
size_t lenPattern,
size_t offset)
const noexcept {
3775 size_t lenText = _len();
3778 if (!lenPattern || offset >= lenText || offset + lenPattern > lenText)
3781 const K *text = _str(), *last = text + lenText - lenPattern, first = pattern[0];
3783 for (
const K* fnd = text + offset;; ++fnd) {
3784 fnd = traits::find(fnd, last - fnd, first);
3787 if (traits::compare(fnd + 1, pattern, lenPattern) == 0)
3788 return static_cast<size_t>(fnd - text);
3801 constexpr size_t find(str_piece pattern,
size_t offset = 0) const noexcept {
3802 return find(pattern.symbols(), pattern.length(), offset);
3820 template<
typename Exc,
typename ... Args>
requires std::is_constructible_v<Exc, Args...>
3821 constexpr size_t find_or_throw(str_piece pattern,
size_t offset = 0, Args&& ... args)
const noexcept {
3822 if (
auto fnd = find(pattern.symbols(), pattern.length(), offset); fnd != str::npos) {
3825 throw Exc(std::forward<Args>(args)...);
3837 constexpr size_t find_end(str_piece pattern,
size_t offset = 0) const noexcept {
3838 size_t fnd = find(pattern.symbols(), pattern.length(), offset);
3839 return fnd == str::npos ? fnd : fnd + pattern.length();
3851 constexpr size_t find_or_all(str_piece pattern,
size_t offset = 0) const noexcept {
3852 auto fnd = find(pattern.symbols(), pattern.length(), offset);
3853 return fnd == str::npos ? _len() : fnd;
3866 auto fnd = find(pattern.symbols(), pattern.length(), offset);
3867 return fnd == str::npos ? _len() : fnd + pattern.length();
3870 constexpr size_t find_last(
const K* pattern,
size_t lenPattern,
size_t offset)
const noexcept {
3871 if (lenPattern == 1)
3872 return find_last(pattern[0], offset);
3873 size_t lenText = std::min(_len(), offset);
3876 if (!lenPattern || lenPattern > lenText)
3880 const K *text = _str() + lenPattern, last = pattern[lenPattern];
3881 lenText -= lenPattern;
3883 if (text[--lenText] == last) {
3884 if (traits::compare(text + lenText - lenPattern, pattern, lenPattern) == 0) {
3901 constexpr size_t find_last(str_piece pattern,
size_t offset = -1) const noexcept {
3902 return find_last(pattern.symbols(), pattern.length(), offset);
3915 size_t fnd = find_last(pattern.symbols(), pattern.length(), offset);
3916 return fnd == str::npos ? fnd : fnd + pattern.length();
3929 auto fnd = find_last(pattern.symbols(), pattern.length(), offset);
3930 return fnd == str::npos ? _len() : fnd;
3943 size_t fnd = find_last(pattern.symbols(), pattern.length(), offset);
3944 return fnd == str::npos ? _len() : fnd + pattern.length();
3956 constexpr bool contains(str_piece pattern,
size_t offset = 0) const noexcept {
3957 return find(pattern, offset) != str::npos;
3969 constexpr size_t find(K s,
size_t offset = 0) const noexcept {
3970 size_t len = _len();
3972 const K *
str = _str(), *fnd = traits::find(
str + offset, len - offset, s);
3974 return static_cast<size_t>(fnd -
str);
3989 size_t len = _len();
3991 const K *
str = _str(), *fnd = traits::find(
str + offset, len - offset, s);
3993 return static_cast<size_t>(fnd -
str);
3998 template<
typename Op>
3999 constexpr void for_all_finded(
const Op& op,
const K* pattern,
size_t patternLen,
size_t offset,
size_t maxCount)
const {
4002 while (maxCount-- > 0) {
4003 size_t fnd = find(pattern, patternLen, offset);
4004 if (fnd == str::npos)
4007 offset = fnd + patternLen;
4022 template<
typename Op>
4023 constexpr void for_all_finded(
const Op& op, str_piece pattern,
size_t offset = 0,
size_t maxCount = 0)
const {
4024 for_all_finded(op, pattern.symbols(), pattern.length(), offset, maxCount);
4027 template<
typename To = std::vector<
size_t>>
4028 constexpr To find_all(
const K* pattern,
size_t patternLen,
size_t offset,
size_t maxCount)
const {
4030 for_all_finded([&](
auto f) { result.emplace_back(f); }, pattern, patternLen, offset, maxCount);
4045 template<
typename To = std::vector<
size_t>>
4046 constexpr To
find_all(str_piece pattern,
size_t offset = 0,
size_t maxCount = 0)
const {
4047 return find_all(pattern.symbols(), pattern.length(), offset, maxCount);
4049 template<
typename To = std::vector<
size_t>>
4050 constexpr void find_all_to(To& to,
const K* pattern,
size_t len,
size_t offset = 0,
size_t maxCount = 0)
const {
4051 return for_all_finded([&](
size_t pos) {
4052 to.emplace_back(pos);
4053 }, pattern, len, offset, maxCount);
4065 constexpr size_t find_last(K s,
size_t offset = -1) const noexcept {
4066 size_t len = std::min(_len(), offset);
4067 const K *text = _str();
4069 if (text[--len] == s)
4084 constexpr size_t find_first_of(str_piece pattern,
size_t offset = 0) const noexcept {
4085 return std::basic_string_view<K>{_str(), _len()}.find_first_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4097 constexpr std::pair<size_t, size_t>
find_first_of_idx(str_piece pattern,
size_t offset = 0) const noexcept {
4098 const K* text = _str();
4099 size_t fnd = std::basic_string_view<K>{text, _len()}.find_first_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4100 return {fnd, fnd == std::basic_string<K>::npos ? fnd : pattern.find(text[fnd]) };
4113 return std::basic_string_view<K>{_str(), _len()}.find_first_not_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4125 constexpr size_t find_last_of(str_piece pattern,
size_t offset = str::npos)
const noexcept {
4126 return std::basic_string_view<K>{_str(), _len()}.find_last_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4138 constexpr std::pair<size_t, size_t>
find_last_of_idx(str_piece pattern,
size_t offset = str::npos)
const noexcept {
4139 const K* text = _str();
4140 size_t fnd = std::basic_string_view<K>{text, _len()}.find_last_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4141 return {fnd, fnd == std::basic_string<K>::npos ? fnd : pattern.find(text[fnd]) };
4153 constexpr size_t find_last_not_of(str_piece pattern,
size_t offset = str::npos)
const noexcept {
4154 return std::basic_string_view<K>{_str(), _len()}.find_last_not_of(std::basic_string_view<K>{pattern.str, pattern.len}, offset);
4166 [[deprecated(
"Use sub(start, end)")]]
4167 constexpr my_type
substr(ptrdiff_t from, ptrdiff_t len = 0)
const {
4168 return my_type{d()(from, len)};
4170 template<first_index_in_str S, last_index_in_str E>
4171 constexpr my_type sub(S start, E end)
const noexcept {
4172 return my_type{get(start, end)};
4184 constexpr my_type
str_mid(
size_t from,
size_t len = -1)
const {
4185 return my_type{d().mid(from, len)};
4215 template<ToIntNumber T,
bool CheckOverflow = true,
unsigned Base = 0,
bool SkipWs = true,
bool AllowSign = true>
4217 auto [res, err, _] = int_convert::to_integer<K, T, Base, CheckOverflow, SkipWs, AllowSign>(_str(), _len());
4248 template<ToIntNumber T,
bool CheckOverflow = true,
unsigned Base = 0,
bool SkipWs = true,
bool AllowSign = true>
4249 constexpr convert_result<T>
to_int() const noexcept {
4250 return int_convert::to_integer<K, T, Base, CheckOverflow, SkipWs, AllowSign>(_str(), _len());
4258 template<
bool SkipWS = true,
bool AllowPlus = true>
4259 requires(
sizeof(K) == 1 &&
4260 requires { std::from_chars(std::declval<K*>(), std::declval<K*>(), std::declval<double&>()); })
4262 size_t len = _len();
4263 const K* ptr = _str();
4264 if constexpr (SkipWS) {
4265 while (len && uns_type(*ptr) <=
' ') {
4270 if constexpr (AllowPlus) {
4271 if (len && *ptr == K(
'+')) {
4279 if constexpr (
requires { std::from_chars(std::declval<K*>(), std::declval<K*>(), std::declval<double&>()); }) {
4281 if (std::from_chars((
const K*)ptr, (
const K*)ptr + len, d).ec == std::errc{}) {
4293 template<
bool SkipWS = true>
4294 requires(
sizeof(K) == 1 &&
4295 requires { std::from_chars(std::declval<K*>(), std::declval<K*>(), std::declval<double&>()); })
4297 size_t len = _len();
4298 const K* ptr = _str();
4299 if constexpr (SkipWS) {
4300 while (len && uns_type(*ptr) <=
' ') {
4306 if constexpr (
requires { std::from_chars(std::declval<K*>(), std::declval<K*>(), std::declval<double&>()); }) {
4308 if (std::from_chars((
const K*)ptr, (
const K*)ptr + len, d, std::chars_format::hex).ec == std::errc{}) {
4323 template<ToIntNumber T>
4328 template<
typename T,
typename Op>
4329 constexpr T splitf(
const K* delimiter,
size_t lendelimiter,
const Op& beforeFunc,
size_t offset)
const {
4330 size_t mylen = _len();
4331 std::conditional_t<std::is_same_v<T, void>, char, T> results;
4332 str_piece me{_str(), mylen};
4333 for (
size_t i = 0;; i++) {
4334 size_t beginOfDelim = find(delimiter, lendelimiter, offset);
4335 if (beginOfDelim == str::npos) {
4336 str_piece last{me.symbols() + offset, me.length() - offset};
4337 if constexpr (std::is_invocable_v<Op, str_piece&>) {
4340 if constexpr (
requires { results.emplace_back(last); }) {
4341 if (last.is_same(me)) {
4344 results.emplace_back(d());
4346 results.emplace_back(last);
4348 }
else if constexpr (
requires { results.push_back(last); }) {
4349 if (last.is_same(me)) {
4352 results.push_back(d());
4354 results.push_back(last);
4356 }
else if constexpr (
requires {results[i] = last;} &&
requires{std::size(results);}) {
4357 if (i < std::size(results)) {
4358 if (last.is_same(me)) {
4368 str_piece piece{me.symbols() + offset, beginOfDelim - offset};
4369 if constexpr (std::is_invocable_v<Op, str_piece&>) {
4372 if constexpr (
requires { results.emplace_back(piece); }) {
4373 results.emplace_back(piece);
4374 }
else if constexpr (
requires { results.push_back(piece); }) {
4375 results.push_back(piece);
4376 }
else if constexpr (
requires { results[i] = piece; } &&
requires{std::size(results);}) {
4377 if (i < std::size(results)) {
4379 if (i == results.size() - 1) {
4384 offset = beginOfDelim + lendelimiter;
4386 if constexpr (!std::is_same_v<T, void>) {
4420 template<
typename T,
typename Op>
4421 constexpr T
splitf(str_piece delimiter,
const Op& beforeFunc,
size_t offset = 0)
const {
4422 return splitf<T>(delimiter.symbols(), delimiter.length(), beforeFunc, offset);
4436 template<
typename T>
4437 constexpr T
split(str_piece delimiter,
size_t offset = 0)
const {
4438 return splitf<T>(delimiter.symbols(), delimiter.length(), 0, offset);
4443 constexpr bool starts_with(
const K* prefix,
size_t l)
const noexcept {
4444 return _len() >= l && 0 == traits::compare(_str(), prefix, l);
4462 return _len() >
prefix.length() &&
4464 trim_operator<TrimSides::TrimLeft, K, size_t(-1),
true>{}.isTrim(_str()[
prefix.length()]);
4475 return _len() >
prefix.length() &&
4477 trim_operator<TrimSides::TrimLeft, K, 0, false>{next_symbol}.isTrim(_str()[
prefix.length()]);
4479 template<typename T, size_t N = const_lit_for<K, T>::Count,
StrType<K> From>
requires is_const_pattern<N>
4480 constexpr bool starts_with_and_oneof(str_piece prefix, T&& next_symbol)
const noexcept {
4481 return _len() >= N &&
4482 starts_with(prefix) &&
4483 trim_operator<TrimSides::TrimLeft, K, N - 1,
false>{next_symbol}.isTrim(_str()[prefix.length()]);
4486 constexpr bool starts_with_ia(
const K* prefix,
size_t len)
const noexcept {
4487 size_t myLen = _len();
4491 const K* ptr1 = _str();
4493 K s1 = *ptr1++, s2 = *prefix++;
4496 if (makeAsciiLower(s1) != makeAsciiLower(s2))
4508 return starts_with_ia(
prefix.symbols(),
prefix.length());
4517 return _len() >
prefix.length() &&
4518 starts_with_ia(
prefix) &&
4519 trim_operator<TrimSides::TrimLeft, K, size_t(-1),
true>{}.isTrim(_str()[
prefix.length()]);
4530 return _len() >
prefix.length() &&
4531 starts_with_ia(
prefix) &&
4532 trim_operator<TrimSides::TrimLeft, K, 0, false>{next_symbol}.isTrim(_str()[
prefix.length()]);
4534 template<typename T, size_t N = const_lit_for<K, T>::Count,
StrType<K> From>
requires is_const_pattern<N>
4535 constexpr bool starts_with_ia_and_oneof(str_piece prefix, T&& next_symbol)
const noexcept {
4536 return _len() >= N &&
4537 starts_with_ia(prefix) &&
4538 trim_operator<TrimSides::TrimLeft, K, N - 1,
false>{next_symbol}.isTrim(_str()[prefix.length()]);
4543 constexpr bool prefix_in(
const K* text,
size_t len)
const noexcept {
4544 size_t myLen = _len();
4547 return !myLen || 0 == traits::compare(text, _str(), myLen);
4556 return prefix_in(text.symbols(), text.length());
4560 constexpr bool ends_with(
const K* suffix,
size_t len)
const noexcept {
4561 size_t myLen = _len();
4562 return len <= myLen && traits::compare(_str() + myLen - len, suffix, len) == 0;
4575 constexpr bool ends_with_ia(
const K* suffix,
size_t len)
const noexcept {
4576 size_t myLen = _len();
4580 const K* ptr1 = _str() + myLen - len;
4582 K s1 = *ptr1++, s2 = *suffix++;
4585 if (makeAsciiLower(s1) != makeAsciiLower(s2))
4597 return ends_with_ia(
suffix.symbols(),
suffix.length());
4606 if (std::is_constant_evaluated()) {
4607 for (
size_t idx = 0; idx < _len(); idx++) {
4608 if (uns_type(_str()[idx]) > 127) {
4614 const int sl = ascii_mask<K>::WIDTH;
4615 const size_t mask = ascii_mask<K>::VALUE;
4616 size_t len = _len();
4617 const uns_type* ptr =
reinterpret_cast<const uns_type*
>(_str());
4618 if constexpr (sl > 1) {
4619 const size_t roundMask =
sizeof(size_t) - 1;
4620 while (len >= sl && (
reinterpret_cast<size_t>(ptr) & roundMask) != 0) {
4626 if (*
reinterpret_cast<const size_t*
>(ptr) & mask)
4646 template<
typename R = my_type>
4648 return R::upperred_only_ascii_from(d());
4658 template<
typename R = my_type>
4660 return R::lowered_only_ascii_from(d());
4678 template<
typename R = my_type>
4679 R
replaced(str_piece pattern, str_piece repl,
size_t offset = 0,
size_t maxCount = 0)
const {
4680 return R::replaced_from(d(), pattern, repl, offset, maxCount);
4694 template<
typename R = str_piece>
4696 if (starts_with(
prefix)) {
4713 template<
typename R = str_piece>
4715 if (starts_with_ia(
prefix)) {
4732 template<
typename R = str_piece>
4735 return R{
get(0, from_end{
suffix.length()})};
4751 template<
typename R = str_piece>
4753 if (ends_with_ia(
suffix)) {
4754 return R{
get(0, from_end{
suffix.length()})};
4759 template<StrType<K> From>
4760 constexpr static my_type make_trim_op(
const From& from,
const auto& opTrim) {
4761 str_piece sfrom = from, newPos = opTrim(sfrom);
4762 if (newPos.is_same(sfrom)) {
4766 return my_type{newPos};
4768 template<TrimS
ides S, StrType<K> From>
4769 constexpr static my_type trim_static(
const From& from) {
4770 return make_trim_op(from, trim_operator<S, K,
static_cast<size_t>(-1),
true>{});
4773 template<TrimSides S, bool withSpaces, typename T, size_t N = const_lit_for<K, T>::Count, StrType<K> From>
4774 requires is_const_pattern<N>
4775 constexpr static my_type trim_static(
const From& from, T&& pattern) {
4776 return make_trim_op(from, trim_operator<S, K, N - 1, withSpaces>{pattern});
4779 template<TrimS
ides S,
bool withSpaces, StrType<K> From>
4780 constexpr static my_type trim_static(
const From& from, str_piece pattern) {
4781 return make_trim_op(from, trim_operator<S, K, 0, withSpaces>{{pattern}});
4791 template<
typename R = str_piece>
4793 return R::template trim_static<TrimSides::TrimAll>(d());
4803 template<
typename R = str_piece>
4805 return R::template trim_static<TrimSides::TrimLeft>(d());
4815 template<
typename R = str_piece>
4817 return R::template trim_static<TrimSides::TrimRight>(d());
4829 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4830 requires is_const_pattern<N>
4832 return R::template trim_static<TrimSides::TrimAll, false>(d(), pattern);
4844 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4845 requires is_const_pattern<N>
4847 return R::template trim_static<TrimSides::TrimLeft, false>(d(), pattern);
4859 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4860 requires is_const_pattern<N>
4862 return R::template trim_static<TrimSides::TrimRight, false>(d(), pattern);
4881 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4882 requires is_const_pattern<N>
4884 return R::template trim_static<TrimSides::TrimAll, true>(d(), pattern);
4900 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4901 requires is_const_pattern<N>
4903 return R::template trim_static<TrimSides::TrimLeft, true>(d(), pattern);
4919 template<typename R = str_piece, typename T, size_t N = const_lit_for<K, T>::Count>
4920 requires is_const_pattern<N>
4922 return R::template trim_static<TrimSides::TrimRight, true>(d(), pattern);
4937 template<
typename R = str_piece>
4939 return R::template trim_static<TrimSides::TrimAll, false>(d(), pattern);
4951 template<
typename R = str_piece>
4953 return R::template trim_static<TrimSides::TrimLeft, false>(d(), pattern);
4965 template<
typename R = str_piece>
4967 return R::template trim_static<TrimSides::TrimRight, false>(d(), pattern);
4983 template<
typename R = str_piece>
4985 return R::template trim_static<TrimSides::TrimAll, true>(d(), pattern);
5001 template<
typename R = str_piece>
5003 return R::template trim_static<TrimSides::TrimLeft, true>(d(), pattern);
5019 template<
typename R = str_piece>
5021 return R::template trim_static<TrimSides::TrimRight, true>(d(), pattern);
5035 template<
typename R = str_piece>
5037 str_piece res = *
this;
5038 while(res.starts_with(
prefix)) {
5039 res.remove_prefix(
prefix.length());
5040 if (--max_count == 0) {
5058 template<
typename R = str_piece>
5060 str_piece res = *
this;
5061 while(res.starts_with_ia(
prefix)) {
5062 res.remove_prefix(
prefix.length());
5063 if (--max_count == 0) {
5081 template<
typename R = str_piece>
5083 str_piece res = *
this;
5084 while(res.ends_with(
suffix)) {
5085 res.remove_suffix(
suffix.length());
5101 template<
typename R = str_piece>
5103 str_piece res = *
this;
5104 while(res.ends_with_ia(
suffix)) {
5105 res.remove_suffix(
suffix.length());
5124template<
size_t N>
requires (N > 1)
5125struct find_all_container {
5126 static constexpr size_t max_capacity = N;
5127 size_t positions_[N];
5130 constexpr void emplace_back(
size_t pos) {
5131 positions_[added_++] = pos;
5154struct str_src : str_src_algs<K, str_src<K>, str_src<K>, false> {
5155 using symb_type = K;
5156 using my_type = str_src<K>;
5158 const symb_type* str;
5161 str_src() =
default;
5166 template<typename T, size_t N = const_lit_for<K, T>::Count>
5167 constexpr str_src(T&& v) noexcept : str((
const K*)v), len(N - 1) {}
5173 constexpr str_src(
const K* p,
size_t l) noexcept : str(p), len(l) {}
5175 template<StrType<K> T>
5176 constexpr str_src(T&& t) :
str(t.symbols()), len(t.length()){}
5182 template<
typename A>
5183 constexpr str_src(
const std::basic_string<K, std::char_traits<K>, A>& s) noexcept : str(s.data()), len(s.length()) {}
5188 constexpr str_src(
const std::basic_string_view<K, std::char_traits<K>>& s) noexcept : str(s.data()), len(s.length()) {}
5201 constexpr const symb_type*
symbols() const noexcept {
5217 constexpr bool is_same(str_src<K> other)
const noexcept {
5218 return str == other.str && len == other.len;
5227 return str >= other.str && str + len <= other.str + other.len;
5290struct str_src_nt : str_src<K>, null_terminated<K, str_src_nt<K>> {
5291 using symb_type = K;
5292 using my_type = str_src_nt<K>;
5293 using base = str_src<K>;
5295 constexpr static const K empty_string[1] = {0};
5297 str_src_nt() =
default;
5314 template<
typename T>
requires std::is_same_v<std::remove_const_t<std::remove_pointer_t<std::remove_cvref_t<T>>>, K>
5317 base::str = base::len ? p : empty_string;
5323 template<typename T, size_t N = const_lit_for<K, T>::Count>
5324 constexpr str_src_nt(T&& v) noexcept : base(std::forward<T>(v)) {}
5330 constexpr str_src_nt(
const K* p,
size_t l) noexcept : base(p, l) {}
5332 template<StrType<K> T>
5335 base::len = t.length();
5341 template<
typename A>
5342 constexpr str_src_nt(
const std::basic_string<K, std::char_traits<K>, A>& s) noexcept : base(s) {}
5344 static const my_type empty_str;
5354 if (from > base::len) {
5357 return {base::str + from, base::len - from};
5362inline const str_src_nt<K> str_src_nt<K>::empty_str{str_src_nt<K>::empty_string, 0};
5363template<
typename K>
struct simple_str_selector;
5365inline namespace literals {
5425template<
char...Chars>
5426SS_CONSTEVAL
auto operator""_fmt() {
5427 return f::skip_0x<Chars...>();
5432#ifndef IN_FULL_SIMSTR
5435using simple_str = str_src<K>;
5438struct simple_str_selector {
5439 using type = simple_str<K>;
5443using simple_str_nt = str_src_nt<K>;
5446using Splitter = SplitterBase<K, str_src<K>>;
5448using ssa = str_src<u8s>;
5449using ssb = str_src<ubs>;
5450using ssw = str_src<wchar_t>;
5451using ssu = str_src<u16s>;
5452using ssuu = str_src<u32s>;
5453using stra = str_src_nt<u8s>;
5454using strb = str_src_nt<ubs>;
5455using strw = str_src_nt<wchar_t>;
5456using stru = str_src_nt<u16s>;
5457using struu = str_src_nt<u32s>;
5460consteval simple_str_nt<K> select_str(simple_str_nt<u8s> s8, simple_str_nt<ubs> sb, simple_str_nt<uws> sw, simple_str_nt<u16s> s16, simple_str_nt<u32s> s32) {
5461 if constexpr (std::is_same_v<K, u8s>)
5463 if constexpr (std::is_same_v<K, ubs>)
5465 if constexpr (std::is_same_v<K, uws>)
5467 if constexpr (std::is_same_v<K, u16s>)
5469 if constexpr (std::is_same_v<K, u32s>)
5473#define uni_string(K, p) select_str<K>(p, u8##p, L##p, u##p, U##p)
5475inline namespace literals {
5548template<
typename K,
bool withSpaces>
5549struct CheckSpaceTrim {
5550 constexpr bool is_trim_spaces(K s)
const {
5551 return s ==
' ' || (s >= 9 && s <= 13);
5555struct CheckSpaceTrim<K, false> {
5556 constexpr bool is_trim_spaces(K)
const {
5562struct CheckSymbolsTrim {
5564 constexpr bool is_trim_symbols(K s)
const {
5565 return symbols.len != 0 && str_src<K>::traits::find(symbols.str, symbols.len, s) !=
nullptr;
5569template<
typename K,
size_t N>
5570struct CheckConstSymbolsTrim {
5571 const const_lit_to_array<K, N> symbols;
5573 template<typename T, size_t M = const_lit_for<K, T>::Count>
requires (M == N + 1)
5574 constexpr CheckConstSymbolsTrim(T&& s) : symbols(std::forward<T>(s)) {}
5576 constexpr bool is_trim_symbols(K s)
const noexcept {
5577 return symbols.contain(s);
5582struct CheckConstSymbolsTrim<K, 0> {
5583 constexpr bool is_trim_symbols(K)
const {
5588template<
typename K,
size_t N>
5589struct SymbSelector {
5590 using type = CheckConstSymbolsTrim<K, N>;
5594struct SymbSelector<K, 0> {
5595 using type = CheckSymbolsTrim<K>;
5599struct SymbSelector<K, static_cast<size_t>(-1)> {
5600 using type = CheckConstSymbolsTrim<K, 0>;
5603template<TrimS
ides S,
typename K,
size_t N,
bool withSpaces>
5604struct trim_operator : SymbSelector<K, N>::type, CheckSpaceTrim<K, withSpaces> {
5605 constexpr bool isTrim(K s)
const {
5606 return CheckSpaceTrim<K, withSpaces>::is_trim_spaces(s) || SymbSelector<K, N>::type::is_trim_symbols(s);
5608 constexpr str_src<K> operator()(str_src<K> from)
const {
5609 if constexpr ((S & TrimSides::TrimLeft) != 0) {
5611 if (isTrim(*from.str)) {
5618 if constexpr ((S & TrimSides::TrimRight) != 0) {
5619 const K* back = from.str + from.len - 1;
5621 if (isTrim(*back)) {
5632template<TrimS
ides S,
typename K>
5633using SimpleTrim = trim_operator<S, K, size_t(-1),
true>;
5635template<TrimSides S = TrimSides::TrimAll, bool withSpaces = false, typename K, typename T, size_t N = const_lit_for<K, T>::Count>
5636 requires is_const_pattern<N>
5637constexpr inline auto trimOp(T&& pattern) {
5638 return trim_operator<S, K, N - 1, withSpaces>{pattern};
5641template<TrimS
ides S = TrimS
ides::TrimAll,
bool withSpaces = false,
typename K>
5642constexpr inline auto trimOp(str_src<K> pattern) {
5643 return trim_operator<S, K, 0, withSpaces>{pattern};
5646static constexpr size_t FIND_CACHE_SIZE = 16;
5648template<
typename K,
size_t N,
size_t L>
5649struct expr_replaces : expr_to_std_string<expr_replaces<K, N, L>> {
5650 using symb_type = K;
5651 using my_type = expr_replaces<K, N, L>;
5653 const K(&pattern)[N + 1];
5654 const K(&repl)[L + 1];
5655 mutable find_all_container<FIND_CACHE_SIZE> matches_;
5656 mutable size_t last_;
5658 constexpr expr_replaces(str_src<K> w,
const K(&p)[N + 1],
const K(&r)[L + 1]) : what(w), pattern(p), repl(r) {}
5660 constexpr size_t length()
const {
5661 size_t l = what.length();
5662 if constexpr (N == L) {
5665 what.find_all_to(matches_, pattern, N, 0, FIND_CACHE_SIZE);
5666 if (matches_.added_) {
5667 last_ = matches_.positions_[matches_.added_ - 1] + N;
5668 l += int(L - N) * matches_.added_;
5670 if (matches_.added_ == FIND_CACHE_SIZE) {
5672 size_t next = what.find(pattern, N, last_);
5673 if (next == str::npos) {
5682 matches_.added_ = -1;
5686 constexpr K* place(K* ptr)
const noexcept {
5687 if constexpr (N == L) {
5688 const K* from = what.symbols();
5689 for (
size_t start = 0; start < what.length();) {
5690 size_t next = what.find(pattern, N, start);
5691 if (next == str::npos) {
5692 next = what.length();
5694 size_t delta = next - start;
5695 ch_traits<K>::copy(ptr, from + start, delta);
5697 ch_traits<K>::copy(ptr, repl, L);
5703 if (matches_.added_ == 0) {
5704 return what.place(ptr);
5705 }
else if (matches_.added_ ==
size_t(-1)) {
5709 const K* from = what.symbols();
5710 for (
size_t start = 0, offset = matches_.positions_[0], idx = 1; ;) {
5711 ch_traits<K>::copy(ptr, from + start, offset - start);
5712 ptr += offset - start;
5713 ch_traits<K>::copy(ptr, repl, L);
5716 if (start >= last_) {
5717 size_t tail = what.length() - last_;
5718 ch_traits<K>::copy(ptr, from + last_, tail);
5722 offset = idx < FIND_CACHE_SIZE ? matches_.positions_[idx++] : what.find(pattern, N, start);
5742template<StrSource A,
typename K = symb_type_from_src_t<A>,
typename T,
size_t N = const_lit_for<K, T>::Count,
typename X,
size_t L = const_lit_for<K, X>::Count>
5745 return expr_replaces<K, N - 1, L - 1>{std::forward<A>(w), p, r};
5757 using symb_type = K;
5762 mutable find_all_container<FIND_CACHE_SIZE> matches_;
5763 mutable size_t last_;
5776 constexpr size_t length()
const {
5778 if (!plen || plen == rlen) {
5781 what.find_all_to(matches_, pattern.
symbols(), plen, 0, FIND_CACHE_SIZE);
5782 if (matches_.added_) {
5783 last_ = matches_.positions_[matches_.added_ - 1] + plen;
5784 l += int(rlen - plen) * matches_.added_;
5786 if (matches_.added_ == FIND_CACHE_SIZE) {
5788 size_t next = what.find(pattern.
symbols(), plen, last_);
5789 if (next == str::npos) {
5792 last_ = next + plen;
5798 matches_.added_ = -1;
5802 constexpr K* place(K* ptr)
const noexcept {
5805 const K* from = what.
symbols();
5806 for (
size_t start = 0; start < what.
length();) {
5807 size_t next = what.find(pattern, start);
5808 if (next == str::npos) {
5811 size_t delta = next - start;
5812 ch_traits<K>::copy(ptr, from + start, delta);
5814 ch_traits<K>::copy(ptr, repl.
symbols(), rlen);
5816 start = next + plen;
5820 if (matches_.added_ == 0) {
5821 return what.
place(ptr);
5822 }
else if (matches_.added_ == -1) {
5826 const K* from = what.
symbols();
5827 for (
size_t start = 0, offset = matches_.positions_[0], idx = 1; ;) {
5828 ch_traits<K>::copy(ptr, from + start, offset - start);
5829 ptr += offset - start;
5830 ch_traits<K>::copy(ptr, repl.
symbols(), rlen);
5832 start = offset + plen;
5833 if (start >= last_) {
5834 size_t tail = what.
length() - start;
5835 ch_traits<K>::copy(ptr, from + start, tail);
5839 offset = idx < FIND_CACHE_SIZE ? matches_.positions_[idx++] : what.find(pattern.
symbols(), plen, start);
5861template<
typename K, StrExprForType<K> E>
5863 using symb_type = K;
5867 mutable size_t replLen;
5868 mutable find_all_container<FIND_CACHE_SIZE> matches_;
5869 mutable size_t last_;
5883 constexpr size_t length()
const {
5888 matches_.positions_[0] = what.find(pattern);
5889 if (matches_.positions_[0] == -1) {
5893 matches_.added_ = 1;
5895 replLen = expr.length();
5896 if (replLen == plen) {
5900 what.find_all_to(matches_, pattern.
symbols(), plen, matches_.positions_[0] + plen, FIND_CACHE_SIZE - 1);
5902 last_ = matches_.positions_[matches_.added_ - 1] + plen;
5903 l += int(replLen - plen) * matches_.added_;
5905 if (matches_.added_ == FIND_CACHE_SIZE) {
5907 size_t next = what.find(pattern.
symbols(), plen, last_);
5908 if (next == str::npos) {
5911 last_ = next + plen;
5912 l += replLen - plen;
5916 matches_.added_ = -1;
5920 constexpr K* place(K* ptr)
const noexcept {
5921 if (matches_.added_ == 0) {
5923 return what.
place(ptr);
5924 }
else if (matches_.added_ == -1) {
5928 size_t plen = pattern.
length();
5929 const K* from = what.
symbols();
5930 ch_traits<K>::copy(ptr, from, matches_.positions_[0]);
5931 ptr += matches_.positions_[0];
5932 const K* repl = ptr;
5933 expr.
place((
typename E::symb_type*)ptr);
5935 size_t start = matches_.positions_[0] + plen;
5937 if (plen == replLen) {
5939 size_t next = what.find(pattern, start);
5940 if (next == str::npos) {
5943 size_t delta = next - start;
5944 ch_traits<K>::copy(ptr, from + start, delta);
5946 ch_traits<K>::copy(ptr, repl, replLen);
5948 start = next + plen;
5951 for (
size_t idx = 1;;) {
5952 if (start >= last_) {
5955 size_t next = idx < FIND_CACHE_SIZE ? matches_.positions_[idx++] : what.find(pattern, start);
5956 size_t delta = next - start;
5957 ch_traits<K>::copy(ptr, from + start, delta);
5959 ch_traits<K>::copy(ptr, repl, replLen);
5961 start = next + plen;
5964 size_t tail = what.
length() - start;
5965 ch_traits<K>::copy(ptr, from + start, tail);
5983template<StrSource A,
typename K = symb_type_from_src_t<A>,
typename T,
typename X>
5984 requires (std::is_constructible_v<str_src<K>, T> && std::is_constructible_v<str_src<K>, X> && (!is_const_lit_v<T> || !is_const_lit_v<X>))
6004template<StrSource A,
typename K = symb_type_from_src_t<A>,
typename T, StrExprForType<K> E>
6005 requires std::is_constructible_v<str_src<K>, T>
6006constexpr auto e_repl(A&& w, T&& p,
const E& expr) {
6011template<
bool UseVectorForReplace>
6012struct replace_search_result_store {
6014 std::pair<size_t, size_t> replaces_[16];
6018struct replace_search_result_store<true> : std::vector<std::pair<size_t, size_t>> {};
6022template<
typename K,
size_t N,
bool UseVectorForReplace>
6023struct expr_replace_const_symbols : expr_to_std_string<expr_replace_const_symbols<K, N, UseVectorForReplace>> {
6024 using symb_type = K;
6025 inline static const int BIT_SEARCH_TRESHHOLD = 4;
6026 const K pattern_[N];
6027 const str_src<K> source_;
6028 const str_src<K> replaces_[N];
6030 mutable replace_search_result_store<UseVectorForReplace> search_results_;
6032 [[_no_unique_address]]
6033 uu8s bit_mask_[N >= BIT_SEARCH_TRESHHOLD ? (
sizeof(K) == 1 ? 32 : 64) : 0]{};
6035 template<
typename ... Repl>
requires (
sizeof...(Repl) == N * 2)
6036 constexpr expr_replace_const_symbols(str_src<K> source, Repl&& ... repl) : expr_replace_const_symbols(0, source, std::forward<Repl>(repl)...) {}
6038 size_t length()
const {
6039 size_t l = source_.length();
6040 auto [fnd, num] = find_first_of(source_.str, source_.len);
6041 if (fnd == str::npos) {
6044 l += replaces_[num].len - 1;
6045 if constexpr (UseVectorForReplace) {
6046 search_results_.reserve((l >> 4) + 8);
6047 search_results_.emplace_back(fnd, num);
6048 for (
size_t start = fnd + 1;;) {
6049 auto [fnd, idx] = find_first_of(source_.str, source_.len, start);
6050 if (fnd == str::npos) {
6053 search_results_.emplace_back(fnd, idx);
6055 l += replaces_[idx].len - 1;
6058 const size_t max_store = std::size(search_results_.replaces_);
6059 search_results_.replaces_[0] = {fnd, num};
6060 search_results_.count_++;
6061 for (
size_t start = fnd + 1;;) {
6062 auto [found, idx] = find_first_of(source_.str, source_.len, start);
6063 if (found == str::npos) {
6066 if (search_results_.count_ < max_store) {
6067 search_results_.replaces_[search_results_.count_] = {found, idx};
6069 l += replaces_[idx].len - 1;
6070 search_results_.count_++;
6076 K* place(K* ptr)
const noexcept {
6078 const K* text = source_.str;
6079 if constexpr (UseVectorForReplace) {
6080 for (
const auto& [pos, num] : search_results_) {
6081 size_t delta = pos - start;
6082 ch_traits<K>::copy(ptr, text + start, delta);
6084 ptr = replaces_[num].place(ptr);
6088 const size_t max_store = std::size(search_results_.replaces_);
6089 size_t founded = search_results_.count_;
6090 for (
size_t idx = 0, stop = std::min(founded, max_store); idx < stop; idx++) {
6091 const auto [pos, num] = search_results_.replaces_[idx];
6092 size_t delta = pos - start;
6093 ch_traits<K>::copy(ptr, text + start, delta);
6095 ptr = replaces_[num].place(ptr);
6098 if (founded > max_store) {
6099 founded -= max_store;
6101 auto [fnd, idx] = find_first_of(source_.str, source_.len, start);
6102 size_t delta = fnd - start;
6103 ch_traits<K>::copy(ptr, text + start, delta);
6105 ptr = replaces_[idx].place(ptr);
6110 size_t tail = source_.len - start;
6111 ch_traits<K>::copy(ptr, text + start, tail);
6116 template<
typename ... Repl>
6117 constexpr expr_replace_const_symbols(
int, str_src<K> source, K s, str_src<K> r, Repl&&... repl) :
6118 expr_replace_const_symbols(0, source, std::forward<Repl>(repl)..., std::make_pair(s, r)){}
6120 template<
typename ... Repl>
requires (
sizeof...(Repl) == N)
6121 constexpr expr_replace_const_symbols(
int, str_src<K> source, Repl&&... repl) :
6122 source_(source), pattern_ {repl.first...}, replaces_{repl.second...}
6124 if constexpr (N >= BIT_SEARCH_TRESHHOLD) {
6125 for (
size_t idx = 0; idx < N; idx++) {
6126 uu8s s =
static_cast<uu8s
>(pattern_[idx]);
6127 if constexpr (
sizeof(K) == 1) {
6128 bit_mask_[s >> 3] |= 1 << (s & 7);
6130 if (std::make_unsigned_t<const K>(pattern_[idx]) > 255) {
6131 bit_mask_[32 + (s >> 3)] |= 1 << (s & 7);
6133 bit_mask_[s >> 3] |= 1 << (s & 7);
6140 template<
size_t Idx>
6141 size_t index_of(K s)
const {
6142 if constexpr (Idx < N) {
6143 return pattern_[Idx] == s ? Idx : index_of<Idx + 1>(s);
6147 bool is_in_mask(uu8s s)
const {
6148 return (bit_mask_[s >> 3] & (1 <<(s & 7))) != 0;
6150 bool is_in_mask2(uu8s s)
const {
6151 return (bit_mask_[32 + (s >> 3)] & (1 <<(s & 7))) != 0;
6153 bool is_in_pattern(K s,
size_t& idx)
const {
6154 if constexpr (N >= BIT_SEARCH_TRESHHOLD) {
6155 if constexpr (
sizeof(K) == 1) {
6156 if (is_in_mask(s)) {
6157 idx = index_of<0>(s);
6161 if (std::make_unsigned_t<const K>(s) > 255) {
6162 if (is_in_mask2(s)) {
6163 return (idx = index_of<0>(s)) != -1;
6166 if (is_in_mask(s)) {
6167 idx = index_of<0>(s);
6175 std::pair<size_t, size_t> find_first_of(
const K* text,
size_t len,
size_t offset = 0)
const {
6176 if constexpr (N >= BIT_SEARCH_TRESHHOLD) {
6178 while (offset < len) {
6179 if (is_in_pattern(text[offset], idx)) {
6180 return {offset, idx};
6185 while (offset < len) {
6186 if (
size_t idx = index_of<0>(text[offset]); idx != -1) {
6187 return {offset, idx};
6235template<
bool UseVector = false, StrSource A,
typename K = symb_type_from_src_t<A>,
typename ... Repl>
6236 requires (
sizeof...(Repl) % 2 == 0)
6238 return expr_replace_const_symbols<K,
sizeof...(Repl) / 2, UseVector>(std::forward<A>(src), std::forward<Repl>(other)...);
6280template<
typename K,
bool UseVectorForReplace = false>
6282 using symb_type = K;
6283 using str_t =
typename simple_str_selector<K>::type;
6284 inline static const int BIT_SEARCH_TRESHHOLD = 4;
6287 const std::vector<std::pair<K, str_t>>& replaces_;
6289 std::basic_string<K, ch_traits<K>, std::allocator<K>> pattern_;
6291 mutable replace_search_result_store<UseVectorForReplace> search_results_;
6293 uu8s bit_mask_[
sizeof(K) == 1 ? 32 : 64]{};
6322 : source_(source), replaces_(repl)
6324 size_t pattern_len = replaces_.size();
6325 pattern_.resize(pattern_len);
6326 K* pattern = pattern_.data();
6328 for (
size_t idx = 0; idx < replaces_.size(); idx++) {
6329 *pattern++ = replaces_[idx].first;
6332 if (pattern_len >= BIT_SEARCH_TRESHHOLD) {
6333 for (
size_t idx = 0; idx < pattern_len; idx++) {
6334 uu8s s =
static_cast<uu8s
>(pattern_[idx]);
6335 if constexpr (
sizeof(K) == 1) {
6336 bit_mask_[s >> 3] |= (1 << (s & 7));
6338 if (std::make_unsigned_t<K>(pattern_[idx]) > 255) {
6339 bit_mask_[32 + (s >> 3)] |= (1 << (s & 7));
6341 bit_mask_[s >> 3] |= (1 << (s & 7));
6348 size_t length()
const {
6349 size_t l = source_.
length();
6350 auto [fnd, num] = find_first_of(source_.str, source_.len);
6351 if (fnd == str::npos) {
6354 l += replaces_[num].second.len - 1;
6355 if constexpr (UseVectorForReplace) {
6356 search_results_.reserve((l >> 4) + 8);
6357 search_results_.emplace_back(fnd, num);
6358 for (
size_t start = fnd + 1;;) {
6359 auto [fnd, idx] = find_first_of(source_.str, source_.len, start);
6360 if (fnd == str::npos) {
6363 search_results_.emplace_back(fnd, idx);
6365 l += replaces_[idx].second.len - 1;
6368 const size_t max_store = std::size(search_results_.replaces_);
6369 search_results_.replaces_[0] = {fnd, num};
6370 search_results_.count_++;
6371 for (
size_t start = fnd + 1;;) {
6372 auto [found, idx] = find_first_of(source_.str, source_.len, start);
6373 if (found == str::npos) {
6376 if (search_results_.count_ < max_store) {
6377 search_results_.replaces_[search_results_.count_] = {found, idx};
6379 l += replaces_[idx].second.len - 1;
6380 search_results_.count_++;
6386 K* place(K* ptr)
const noexcept {
6388 const K* text = source_.str;
6389 if constexpr (UseVectorForReplace) {
6390 for (
const auto& [pos, num] : search_results_) {
6391 size_t delta = pos - start;
6392 ch_traits<K>::copy(ptr, text + start, delta);
6394 ptr = replaces_[num].second.place(ptr);
6398 const size_t max_store = std::size(search_results_.replaces_);
6399 size_t founded = search_results_.count_;
6400 for (
size_t idx = 0, stop = std::min(founded, max_store); idx < stop; idx++) {
6401 const auto [pos, num] = search_results_.replaces_[idx];
6402 size_t delta = pos - start;
6403 ch_traits<K>::copy(ptr, text + start, delta);
6405 ptr = replaces_[num].second.place(ptr);
6408 if (founded > max_store) {
6409 founded -= max_store;
6411 auto [fnd, idx] = find_first_of(source_.str, source_.len, start);
6412 size_t delta = fnd - start;
6413 ch_traits<K>::copy(ptr, text + start, delta);
6415 ptr = replaces_[idx].second.place(ptr);
6420 size_t tail = source_.len - start;
6421 ch_traits<K>::copy(ptr, text + start, tail);
6426 size_t index_of(K s)
const {
6427 return pattern_.find(s);
6430 bool is_in_mask(uu8s s)
const {
6431 return (bit_mask_[s >> 3] & (1 << (s & 7))) != 0;
6433 bool is_in_mask2(uu8s s)
const {
6434 return (bit_mask_[32 + (s >> 3)] & (1 << (s & 7))) != 0;
6437 bool is_in_pattern(K s,
size_t& idx)
const {
6438 if constexpr (
sizeof(K) == 1) {
6439 if (is_in_mask(s)) {
6444 if (std::make_unsigned_t<const K>(s) > 255) {
6445 if (is_in_mask2(s)) {
6446 return (idx = index_of(s)) != -1;
6449 if (is_in_mask(s)) {
6458 std::pair<size_t, size_t> find_first_of(
const K* text,
size_t len,
size_t offset = 0)
const {
6459 size_t pl = pattern_.length();
6460 if (pl >= BIT_SEARCH_TRESHHOLD) {
6462 while (offset < len) {
6463 if (is_in_pattern(text[offset], idx)) {
6464 return {offset, idx};
6469 while (offset < len) {
6470 if (
size_t idx = index_of(text[offset]); idx != -1) {
6471 return {offset, idx};
6480template<
typename K, StrExprForType<K> T>
6483 force_copy(
const T& t) : t_(t){}
6486template<
typename K,
typename T>
6487struct symb_type_from_src<force_copy<K, T>> {
6493force_copy(T&&) -> force_copy<typename T::symb_type, T>;
6495template<
typename K,
typename T>
6496constexpr auto to_subst(T&& t) {
6497 return to_strexpr<K>(std::forward<T>(t));
6500template<
typename K,
typename T,
size_t N>
6501constexpr auto to_subst(
const T(&t)[N]) {
6502 return expr_literal<T, N - 1>{t};
6505template<
typename K, StrExprForType<K> T>
6506constexpr decltype(
auto) to_subst(T&& t) {
6507 return std::forward<T>(t);
6510template<
typename K,
typename T>
6511constexpr T to_subst(
const force_copy<K, T>& t) {
6515template<
typename K,
typename T>
6516constexpr T to_subst(force_copy<K, T>&& t) {
6520template<
typename K,
typename T>
6521constexpr T to_subst(force_copy<K, T>& t) {
6525template<
typename K,
typename T>
6526using to_str_exp_t =
decltype(to_subst<K>(std::declval<T>()));
6535template<
typename K,
typename G,
typename Arg,
typename...Args>
6537 using symb_type = K;
6538 using store_t = std::tuple<to_str_exp_t<K, Args>...>;
6539 using arg_t = to_str_exp_t<K, Arg>;
6540 to_str_exp_t<K, G> glue_;
6543 mutable size_t glue_len_;
6577 : glue_(to_subst<K>(
std::forward<G>(glue)))
6578 , arg_(to_subst<K>(
std::forward<Arg>(arg)))
6579 , args_(to_subst<K>(
std::forward<Args>(args))...) {}
6581 constexpr size_t length() const noexcept {
6582 return [
this]<
size_t...Indexes>(std::index_sequence<Indexes...>) {
6583 glue_len_ = glue_.length();
6584 size_t l = arg_.length() + glue_len_ *
sizeof...(Args);
6585 ((l += std::get<Indexes>(args_).length()),...);
6587 }(std::make_index_sequence<
sizeof...(Args)>());
6589 constexpr K* place(K* ptr)
const noexcept {
6590 return [
this]<
size_t...Indexes>(K* ptr, std::index_sequence<Indexes...>) {
6591 ptr = (K*)arg_.place((
typename std::remove_cvref_t<arg_t>::symb_type*)ptr);
6592 const K* glueStart = ptr;
6593 ptr = glue_.place(ptr);
6596 ptr = (K*)std::get<Indexes>(args_).place((
typename std::remove_cvref_t<std::tuple_element_t<Indexes, store_t>>::symb_type*)ptr),
6597 glue_len_ > 0 && Indexes <
sizeof...(Args) - 1 ? (ch_traits<K>::copy(ptr, glueStart, glue_len_), ptr += glue_len_) :
nullptr
6601 }(ptr, std::make_index_sequence<
sizeof...(Args)>());
6605template<
typename T,
typename ... Args>
requires (
sizeof...(Args) > 1)
6606e_concat(T&&, Args&&...) -> e_concat<symb_type_from_src_t<T>, T, Args...>;
6608struct parse_subst_string_error {
6609 parse_subst_string_error(
const char*){}
6614template<
typename K,
size_t NParams>
6615constexpr size_t parse_pattern_string(str_src<K> pattern,
auto&& add_part,
auto&& add_param) {
6616 char used_args[NParams] = {0};
6617 const K* first = pattern.begin(), *last = pattern.end(), *start = first;
6620 auto find = [](
const K* from,
const K* last, K s) {
6621 while (from != last) {
6629 size_t idx_in_params = 0;
6631 while (first != last) {
6632 const K* open_pos = first;
6633 if (*first !=
'{') {
6634 open_pos = find(first, last,
'{');
6637 const K* close_pos = find(first, open_pos,
'}');
6638 if (close_pos == open_pos) {
6639 unsigned len = open_pos - first;
6640 add_part(first - start, len);
6645 if (close_pos == open_pos || *close_pos !=
'}') {
6646 throw parse_subst_string_error{
"unescaped }"};
6648 unsigned len = close_pos - first;
6649 add_part(first - start, len);
6651 first = ++close_pos;
6653 if (open_pos == last) {
6657 if (++open_pos == last) {
6658 throw parse_subst_string_error{
"unescaped {"};
6660 if (*open_pos ==
'}') {
6661 if (idx_in_params == -1) {
6662 throw parse_subst_string_error{
"already used param ids"};
6664 if (idx_in_params == NParams) {
6665 throw parse_subst_string_error{
"too many params"};
6667 used_args[idx_in_params] = 1;
6668 add_param(idx_in_params++);
6669 first = open_pos + 1;
6670 }
else if (*open_pos ==
'{') {
6671 add_part(open_pos - start, 1);
6673 first = open_pos + 1;
6675 if (idx_in_params != 0 && idx_in_params != -1) {
6676 throw parse_subst_string_error{
"already used non id params"};
6679 const K* end = find(open_pos, last,
'}');
6681 throw parse_subst_string_error{
"not found }"};
6683 auto [p, err, _] = str_src<K>(open_pos, end - open_pos).template to_int<unsigned, true, 10, false, false>();
6684 if (err != IntConvertResult::Success || p < 1 || p > NParams) {
6685 throw parse_subst_string_error{
"bad param id"};
6692 for (
auto c : used_args) {
6694 throw parse_subst_string_error{
"unused param"};
6703 unsigned is_param: 1;
6705 portion() =
default;
6707 constexpr void set_param(
unsigned param) {
6708 if (param >= (1 << 16)) {
6709 throw parse_subst_string_error{
"the parameter id is too large"};
6714 constexpr void set_part(
unsigned from,
unsigned l) {
6715 if (from >= (1 << 16) || len >= (1 << 15)) {
6716 throw parse_subst_string_error{
"the string part is too large"};
6724template<
typename K,
size_t PtLen,
size_t NParams>
6725struct subst_params {
6726 const K(&source_)[PtLen];
6727 unsigned all_len_{};
6731 portion portions_[1 + ((PtLen - 2) * 2 / 3)]{};
6733 consteval subst_params(
const K(&pattern)[PtLen]) : source_(pattern) {
6734 all_len_ = parse_pattern_string<K, NParams>(pattern,
6735 [
this](
unsigned from,
unsigned len) {
6736 portions_[actual_++].set_part(from, len);
6737 }, [
this](
unsigned param) {
6738 portions_[actual_++].set_param(param);
6750template<
typename K,
size_t PtLen,
typename ... Args>
6752 inline static constexpr size_t NParams =
sizeof...(Args);
6753 using symb_type = K;
6754 using store_t = std::tuple<to_str_exp_t<K, Args>...>;
6756 const details::subst_params<K, PtLen, NParams>& subst_;
6798 constexpr e_subst(
const details::subst_params<K, PtLen, NParams>& subst, Args&&...args)
6800 , args_(to_subst<K>(
std::forward<Args>(args))...){}
6802 constexpr size_t length() const noexcept {
6803 return [
this]<
size_t...Indexes>(std::index_sequence<Indexes...>) {
6805 size_t expr_length_[NParams] = {};
6806 ((expr_length_[idx++] = std::get<Indexes>(args_).length()),...);
6807 size_t l = subst_.all_len_;
6808 for (idx = 0; idx < subst_.actual_; idx++) {
6809 if (subst_.portions_[idx].is_param) {
6810 l += expr_length_[subst_.portions_[idx].start];
6814 }(std::make_index_sequence<
sizeof...(Args)>());
6816 template<
size_t Idx>
6817 constexpr K* place_idx(K* ptr,
size_t idx)
const noexcept {
6819 return (K*)std::get<Idx>(args_).place((
typename std::remove_cvref_t<std::tuple_element_t<Idx, store_t>>::symb_type*)ptr);
6821 if constexpr (Idx < NParams - 1) {
6822 return place_idx<Idx + 1>(ptr, idx);
6826 constexpr K* place(K* ptr)
const noexcept {
6827 for (
size_t idx = 0; idx < subst_.actual_; idx++) {
6828 if (subst_.portions_[idx].is_param) {
6829 ptr = place_idx<0>(ptr, subst_.portions_[idx].start);
6831 ch_traits<K>::copy(ptr, subst_.source_ + subst_.portions_[idx].start, subst_.portions_[idx].len);
6832 ptr += subst_.portions_[idx].len;
6840template<
typename K,
size_t N,
typename...Args>
requires (
sizeof...(Args) > 0)
6841e_subst(
const K(&)[N], Args&&...) -> e_subst<K, N, Args...>;
6848template<
typename K,
typename ... Args>
6850 inline static constexpr size_t Nparams =
sizeof...(Args);
6851 using symb_type = K;
6852 using store_t = std::tuple<to_str_exp_t<K, Args>...>;
6854 details::portion portions_[Nparams * 3];
6855 std::vector<details::portion> more_portions_;
6899 , args_(to_subst<K>(
std::forward<Args>(args))...) {
6901 all_len_ = details::parse_pattern_string<K, Nparams>(pattern_, [
this](
unsigned from,
unsigned len) {
6902 if (actual_ < std::size(portions_)) {
6903 portions_[actual_++].set_part(from, len);
6905 if (actual_ == std::size(portions_)) {
6906 more_portions_.reserve((pattern_.len - 1) * 2 / 3 - std::size(portions_));
6908 more_portions_.emplace_back().set_part(from, len);
6910 }, [
this](
unsigned param) {
6911 if (actual_ < std::size(portions_)) {
6912 portions_[actual_++].set_param(param);
6914 if (actual_ == std::size(portions_)) {
6915 more_portions_.reserve((pattern_.len - 1) * 2 / 3 - std::size(portions_));
6917 more_portions_.emplace_back().set_param(param);
6922 constexpr size_t length() const noexcept {
6923 return [
this]<
size_t...Indexes>(std::index_sequence<Indexes...>) {
6925 size_t expr_length_[Nparams] = {};
6926 ((expr_length_[idx++] = std::get<Indexes>(args_).length()),...);
6927 size_t l = all_len_;
6928 for (idx = 0; idx < actual_; idx++) {
6929 if (portions_[idx].is_param) {
6930 l += expr_length_[portions_[idx].start];
6933 for (
const auto& p : more_portions_) {
6935 l += expr_length_[p.start];
6939 }(std::make_index_sequence<
sizeof...(Args)>());
6941 template<
size_t Idx>
6942 constexpr K* place_idx(K* ptr,
size_t idx)
const noexcept {
6944 return (K*)std::get<Idx>(args_).place((
typename std::remove_cvref_t<std::tuple_element_t<Idx, store_t>>::symb_type*)ptr);
6946 if constexpr (Idx < Nparams - 1) {
6947 return place_idx<Idx + 1>(ptr, idx);
6951 constexpr K* place(K* ptr)
const noexcept {
6952 for (
size_t idx = 0; idx < actual_; idx++) {
6953 if (portions_[idx].is_param) {
6954 ptr = place_idx<0>(ptr, portions_[idx].start);
6956 ch_traits<K>::copy(ptr, pattern_.
symbols() + portions_[idx].start, portions_[idx].len);
6957 ptr += portions_[idx].len;
6960 for (
const auto& p : more_portions_) {
6962 ptr = place_idx<0>(ptr, p.start);
6964 const K* from = pattern_.
symbols() + p.start;
6965 for (
size_t idx = p.len; idx--;) {
6976template<StrSourceNoLiteral T,
typename...Args>
requires (
sizeof...(Args) > 0)
6977e_vsubst(T&&, Args&&...) -> e_vsubst<symb_type_from_src_t<T>, Args...>;
6979template<is_one_of_
char_v K,
bool upper>
6980struct expr_change_case_ascii : expr_to_std_string<expr_change_case_ascii<K, upper>>{
6981 using symb_type = K;
6985 template<StrSource S>
6986 expr_change_case_ascii(S&& s) : src_(std::forward<S>(s)){}
6988 constexpr size_t length() const noexcept {
6989 return src_.length();
6991 constexpr K* place(K* ptr)
const noexcept {
6992 const K* read = src_.str;
6993 for (
size_t l = src_.len; l--;) {
6994 if constexpr (upper) {
6995 *ptr++ = makeAsciiUpper(*read++);
6997 *ptr++ = makeAsciiLower(*read++);
7018template<is_one_of_
char_v K>
7020 using base = expr_change_case_ascii<K, true>;
7024template<StrSource S>
7041template<is_one_of_
char_v K>
7043 using base = expr_change_case_ascii<K, false>;
7047template<StrSource S>
7088template<
typename K,
typename A, StrExprForType<K> E>
7089std::basic_string<K, std::char_traits<K>, A>&
change(std::basic_string<K, std::char_traits<K>, A>&
str,
size_t from,
size_t count,
const E& expr) {
7090 size_t expr_length = expr.length();
7092 str.erase(from, count);
7095 size_t str_length =
str.length();
7096 if (from > str_length) {
7099 if (count > str_length || from + count > str_length) {
7100 count = str_length - from;
7102 size_t new_length = str_length - count + expr_length;
7103 size_t tail_length = str_length - count - from;
7105 if (new_length <= str_length) {
7106 K* data =
str.data();
7107 expr.place((
typename E::symb_type*)data + from);
7108 if (expr_length < count) {
7110 std::char_traits<K>::move(data + from + expr_length, data + from + count, tail_length);
7112 str.resize(new_length);
7115 auto fill = [&](K* data, size_t) ->
size_t {
7117 std::char_traits<K>::move(data + from + expr_length, data + from + count, tail_length);
7119 expr.place((
typename E::symb_type*)data + from);
7122 if constexpr (
requires{
str.resize_and_overwrite(new_length, fill);}) {
7123 str.resize_and_overwrite(new_length, fill);
7124 }
else if constexpr (
requires{
str._Resize_and_overwrite(new_length, fill);}) {
7125 str._Resize_and_overwrite(new_length, fill);
7127 str.resize(new_length);
7128 fill(
str.data(), 0);
7161template<
typename K,
typename A, StrExprForType<K> E>
7162std::basic_string<K, std::char_traits<K>, A>&
append(std::basic_string<K, std::char_traits<K>, A>&
str,
const E& expr) {
7194template<
typename K,
typename A, StrExprForType<K> E>
7195std::basic_string<K, std::char_traits<K>, A>&
prepend(std::basic_string<K, std::char_traits<K>, A>&
str,
const E& expr) {
7229template<
typename K,
typename A, StrExprForType<K> E>
7230std::basic_string<K, std::char_traits<K>, A>&
insert(std::basic_string<K, std::char_traits<K>, A>&
str,
size_t from,
const E& expr) {
7258template<
typename K,
typename A, StrExprForType<K> E>
7259std::basic_string<K, std::char_traits<K>, A>&
overwrite(std::basic_string<K, std::char_traits<K>, A>&
str,
const E& expr) {
7260 if (
size_t expr_length = expr.length()) {
7261 if (expr_length <=
str.length()) {
7262 K* data =
str.data();
7263 expr.place((
typename E::symb_type*)data);
7264 str.resize(expr_length);
7266 auto fill = [&](K* data, size_t) ->
size_t {
7267 expr.place((
typename E::symb_type*)data);
7270 if constexpr (
requires{
str.resize_and_overwrite(expr_length, fill);}) {
7271 str.resize_and_overwrite(expr_length, fill);
7272 }
else if constexpr (
requires{
str._Resize_and_overwrite(expr_length, fill);}) {
7273 str._Resize_and_overwrite(expr_length, fill);
7275 str.resize(expr_length);
7276 expr.place((
typename E::symb_type*)
str.data());
7287template<
typename K,
typename A,
typename E>
7288struct replace_grow_helper {
7289 using my_type = std::basic_string<K, std::char_traits<K>, A>;
7291 replace_grow_helper(my_type& src,
str_src<K> p,
const K* r,
size_t rl,
size_t mc,
size_t d,
const E& e)
7292 :
str(src), source(src), pattern(p), repl(const_cast<K*>(r)), replLen(rl), maxCount(mc), delta(d), expr(e) {}
7295 const str_src<K> source;
7296 const str_src<K> pattern;
7298 const size_t replLen;
7305 K* reserve_for_copy{};
7306 size_t end_of_piece{};
7307 size_t total_length{};
7309 std::optional<my_type> dst;
7312 size_t found[16] = {offset};
7315 offset += pattern.len;
7318 for (; idx < std::size(found) && maxCount > 0; idx++, maxCount--) {
7319 found[idx] = source.find(pattern, offset);
7320 if (found[idx] == npos) {
7323 offset = found[idx] + pattern.len;
7326 if (idx == std::size(found) && maxCount > 0 && (offset = source.find(pattern, offset)) != str::npos) {
7331 if (!reserve_for_copy) {
7334 end_of_piece = source.length();
7335 total_length = end_of_piece + all_delta;
7337 if (total_length <= str.capacity()) {
7342 dst_str = &dst.emplace();
7344 auto fill = [
this](K* p, size_t) ->
size_t {
7345 reserve_for_copy = p;
7346 return total_length;
7348 if constexpr (
requires{dst_str->_Resize_and_overwrite(total_length, fill);}) {
7349 dst_str->_Resize_and_overwrite(total_length, fill);
7350 }
else if constexpr (
requires{dst_str->resize_and_overwrite(total_length, fill);}) {
7351 dst_str->resize_and_overwrite(total_length, fill);
7353 dst_str->resize(total_length);
7354 reserve_for_copy = dst_str->data();
7357 const K* src_start = str.c_str();
7359 size_t pos = found[idx] + pattern.len;
7360 size_t lenOfPiece = end_of_piece - pos;
7361 ch_traits<K>::move(reserve_for_copy + pos + all_delta, src_start + pos, lenOfPiece);
7362 if constexpr (std::is_same_v<E, int>) {
7363 ch_traits<K>::copy(reserve_for_copy + pos + all_delta - replLen, repl, replLen);
7366 repl = reserve_for_copy + pos + all_delta - replLen;
7369 ch_traits<K>::copy(reserve_for_copy + pos + all_delta - replLen, repl, replLen);
7373 end_of_piece = found[idx];
7375 if (!all_delta && reserve_for_copy != src_start) {
7376 ch_traits<K>::copy(reserve_for_copy, src_start, found[0]);
7377 str = std::move(*dst);
7418template<
typename K,
typename A, StrExprForType<K> E,
typename T>
7419requires (std::is_constructible_v<str_src<K>, T>)
7420std::basic_string<K, std::char_traits<K>, A>&
replace(std::basic_string<K, std::char_traits<K>, A>&
str, T&& pattern,
const E& repl,
size_t offset = 0,
size_t max_count = -1) {
7425 str_src<K> spattern{std::forward<T>(pattern)};
7426 offset = src.find(pattern, offset);
7427 if (offset == npos) {
7430 size_t replLen = repl.length();
7432 if (spattern.len == replLen) {
7435 K* ptr =
str.data();
7436 replStart = ptr + offset;
7437 repl.place(replStart);
7439 while (--max_count) {
7440 offset = src.find(spattern, offset + replLen);
7443 ch_traits<K>::copy(ptr + offset, replStart, replLen);
7445 }
else if (spattern.len > replLen) {
7448 K* ptr =
str.data();
7449 replStart = ptr + offset;
7450 repl.place(replStart);
7451 size_t posWrite = offset + replLen;
7452 offset += spattern.len;
7454 while (--max_count) {
7455 size_t idx = src.find(spattern, offset);
7458 size_t lenOfPiece = idx - offset;
7459 ch_traits<K>::move(ptr + posWrite, ptr + offset, lenOfPiece);
7460 posWrite += lenOfPiece;
7461 ch_traits<K>::copy(ptr + posWrite, replStart, replLen);
7462 posWrite += replLen;
7463 offset = idx + spattern.len;
7465 size_t tailLen = src.len - offset;
7466 ch_traits<K>::move(ptr + posWrite, ptr + offset, tailLen);
7467 str.resize(posWrite + tailLen);
7469 details::replace_grow_helper<K, A, E>(
str, spattern,
nullptr, replLen, max_count, replLen - spattern.len, repl).replace(offset);
7494template<
typename K,
typename A>
7495std::basic_string<K, std::char_traits<K>, A>&
replace(std::basic_string<K, std::char_traits<K>, A>&
str,
str_src<K> pattern,
str_src<K> repl,
size_t offset = 0,
size_t max_count = -1) {
7500 offset = src.find(pattern, offset);
7501 if (offset == npos) {
7504 if (pattern.len == repl.len) {
7507 K* ptr =
str.data();
7508 while (max_count--) {
7509 ch_traits<K>::copy(ptr + offset, repl.str, repl.len);
7510 offset = src.find(pattern, offset + repl.len);
7514 }
else if (pattern.len > repl.len) {
7517 K* ptr =
str.data();
7518 ch_traits<K>::copy(ptr + offset, repl.str, repl.len);
7519 size_t posWrite = offset + repl.len;
7520 offset += pattern.len;
7522 while (--max_count) {
7523 size_t idx = src.find(pattern, offset);
7526 size_t lenOfPiece = idx - offset;
7527 ch_traits<K>::move(ptr + posWrite, ptr + offset, lenOfPiece);
7528 posWrite += lenOfPiece;
7529 ch_traits<K>::copy(ptr + posWrite, repl.str, repl.len);
7530 posWrite += repl.len;
7531 offset = idx + pattern.len;
7533 size_t tailLen = src.len - offset;
7534 ch_traits<K>::move(ptr + posWrite, ptr + offset, tailLen);
7535 str.resize(posWrite + tailLen);
7537 details::replace_grow_helper<K, A, int>(
str, pattern, repl.str, repl.len, max_count, repl.len - pattern.len, 0).replace(offset);
7542template<
typename K,
typename A,
typename T,
typename M>
7543requires (std::is_constructible_v<str_src<K>, T> && std::is_constructible_v<str_src<K>, M>)
7544std::basic_string<K, std::char_traits<K>, A>& replace(std::basic_string<K, std::char_traits<K>, A>&
str, T&& pattern, M&& repl,
size_t offset = 0,
size_t max_count = -1) {
7545 return replace(
str,
str_src<K>{std::forward<T>(pattern)}, str_src<K>{std::forward<M>(repl)}, offset, max_count);
7560template<
typename K,
typename A>
7561std::basic_string<K, std::char_traits<K>, A>&
make_ascii_upper(std::basic_string<K, std::char_traits<K>, A>&
str,
size_t from = 0,
size_t to = -1) {
7562 for (K *ptr =
str.data() + std::min(from,
str.length()), *end =
str.data() + std::min(to,
str.length()); ptr < end; ptr++) {
7564 if (isAsciiLower(s)) {
7583template<
typename K,
typename A>
7584std::basic_string<K, std::char_traits<K>, A>&
make_ascii_lower(std::basic_string<K, std::char_traits<K>, A>&
str,
size_t from = 0,
size_t to = -1) {
7585 for (K *ptr =
str.data() + std::min(from,
str.length()), *end =
str.data() + std::min(to,
str.length()); ptr < end; ptr++) {
7587 if (isAsciiUpper(s)) {
7622template<simstr::StdStrSource T>
7654template<
typename K,
typename A, simstr::StrExprForType<K> E>
7655std::basic_string<K, std::char_traits<K>, A>& operator |=(std::basic_string<K, std::char_traits<K>, A>& str,
const E& expr) {
7686template<
typename K,
typename A, simstr::StrExprForType<K> E>
7687std::basic_string<K, std::char_traits<K>, A>& operator ^=(std::basic_string<K, std::char_traits<K>, A>& str,
const E& expr) {
Class for sequentially obtaining substrings by a given delimiter.
Definition strexpr.h:3234
constexpr bool is_done() const
Find out if substrings are running out.
Definition strexpr.h:3245
constexpr str_t next()
Get the next substring.
Definition strexpr.h:3254
constexpr R trimmed_right_with_spaces(str_piece pattern) const
Get a string, removing characters specified by another string, as well as whitespace characters,...
Definition strexpr.h:5020
constexpr size_t find_last(K s, size_t offset=-1) const noexcept
Find the last occurrence of a character in this string.
Definition strexpr.h:4065
constexpr int compare_ia(str_piece text) const noexcept
Compare strings character by character and not case sensitive ASCII characters.
Definition strexpr.h:3747
constexpr int compare(str_piece o) const
Compare strings character by character.
Definition strexpr.h:3632
constexpr size_t find(K s, size_t offset=0) const noexcept
Find a character in this string.
Definition strexpr.h:3969
constexpr std::optional< R > strip_suffix(str_piece suffix) const
Get a string without a suffix if it ends with one.
Definition strexpr.h:4733
constexpr bool ends_with(str_piece suffix) const noexcept
Whether the string ends with the specified substring.
Definition strexpr.h:4570
constexpr bool operator==(const base &other) const noexcept
Operator comparing strings for equality.
Definition strexpr.h:3683
constexpr std::basic_string_view< D > to_sv() const noexcept
Convert to std::basic_string_view.
Definition strexpr.h:3388
constexpr str_piece get(S start, E end) const noexcept
Get part of a string as "str_src".
Definition strexpr.h:3505
constexpr bool starts_with_ia_and_ws(str_piece prefix) const noexcept
Check if a string begins with the specified case-insensitive ASCII substring followed by a whitespace...
Definition strexpr.h:4516
constexpr R trimmed_right() const
Get a string with whitespace removed on the right.
Definition strexpr.h:4816
constexpr R trimmed_prefix_ia(str_piece prefix, size_t max_count=0) const
Returns a string with the beginning of the string removed if it begins with the specified prefix,...
Definition strexpr.h:5059
constexpr size_t find_end_or_all(str_piece pattern, size_t offset=0) const noexcept
Find the end of the first occurrence of a substring in this string, or the end of a string.
Definition strexpr.h:3865
constexpr bool starts_with(str_piece prefix) const noexcept
Whether the string begins with the given substring.
Definition strexpr.h:4452
constexpr bool equal(str_piece other) const noexcept
String comparison for equality.
Definition strexpr.h:3672
void copy_to(K *buffer, size_t bufSize)
Copy the string to the specified buffer.
Definition strexpr.h:3366
constexpr size_t find_or_throw(str_piece pattern, size_t offset=0, Args &&... args) const noexcept
Find the beginning of the first occurrence of a substring in this string or throw an exception.
Definition strexpr.h:3821
constexpr str_piece until(str_piece pattern, size_t offset=0) const noexcept
Get the substring str_src from the beginning to the first found occurrence of the specified substring...
Definition strexpr.h:3594
constexpr R trimmed(str_piece pattern) const
Get a string with characters specified by another string removed, left and right.
Definition strexpr.h:4938
constexpr T as_int() const noexcept
Convert a string to a number of the given type.
Definition strexpr.h:4216
constexpr str_piece prefix(size_t count) const noexcept
Wraps get(0, count).
Definition strexpr.h:3539
constexpr bool less_ia(str_piece text) const noexcept
Whether a string is smaller than another string, character-by-character-insensitive,...
Definition strexpr.h:3770
constexpr T split(str_piece delimiter, size_t offset=0) const
Split a string into substrings using a given delimiter.
Definition strexpr.h:4437
constexpr R trimmed_right_with_spaces(T &&pattern) const
Get a string with the characters specified by the string literal removed, as well as whitespace chara...
Definition strexpr.h:4921
constexpr str_piece mid(size_t from, size_t len=-1) const noexcept
Get part of a string as "string chunk".
Definition strexpr.h:3559
constexpr my_type substr(ptrdiff_t from, ptrdiff_t len=0) const
Get a substring. Works similarly to operator(), only the result is the same type as the method applie...
Definition strexpr.h:4167
R upperred_only_ascii() const
Get a copy of the string in uppercase ASCII characters.
Definition strexpr.h:4647
constexpr str_piece to_str() const noexcept
Convert itself to a "string chunk" that includes the entire string.
Definition strexpr.h:3436
constexpr bool starts_with_and_ws(str_piece prefix) const noexcept
Check if a string begins with the specified substring followed by a whitespace ASCII character.
Definition strexpr.h:4461
constexpr str_piece from_to(size_t from, size_t to) const noexcept
Get the substring str_src from position from to position to (not including it).
Definition strexpr.h:3581
constexpr R trimmed_left_with_spaces(str_piece pattern) const
Get a string, removing characters specified by another string, as well as whitespace characters,...
Definition strexpr.h:5002
R replaced(str_piece pattern, str_piece repl, size_t offset=0, size_t maxCount=0) const
Get a copy of the string with occurrences of substrings replaced.
Definition strexpr.h:4679
constexpr int strcmp(const K *text) const
Compare with C-string character by character.
Definition strexpr.h:3643
constexpr R trimmed_left() const
Get a string with whitespace removed on the left.
Definition strexpr.h:4804
constexpr bool ends_with_ia(str_piece suffix) const noexcept
Whether the string ends with the specified substring in a case-insensitive ASCII character.
Definition strexpr.h:4596
constexpr size_t find_first_not_of(str_piece pattern, size_t offset=0) const noexcept
Find the first occurrence of a character not from the given character set.
Definition strexpr.h:4112
constexpr size_t find_first_of(str_piece pattern, size_t offset=0) const noexcept
Find the first occurrence of a character from a given character set.
Definition strexpr.h:4084
constexpr R trimmed_right(str_piece pattern) const
Get a string with characters specified by another string removed to the right.
Definition strexpr.h:4966
constexpr R trimmed_with_spaces(T &&pattern) const
Get a string with the characters specified by the string literal removed, as well as whitespace chara...
Definition strexpr.h:4883
constexpr str_piece suffix(size_t count) const noexcept
Wraps get(from_end(count), to_end).
Definition strexpr.h:3546
constexpr size_t find_last_or_all(str_piece pattern, size_t offset=-1) const noexcept
Find the beginning of the last occurrence of a substring in this string or the end of the string.
Definition strexpr.h:3928
constexpr R trimmed_prefix(str_piece prefix, size_t max_count=0) const
Returns a string with the beginning of the string removed if it begins with the specified prefix.
Definition strexpr.h:5036
constexpr bool starts_with_ia(str_piece prefix) const noexcept
Whether the string begins with the given substring in a case-insensitive ASCII character.
Definition strexpr.h:4507
constexpr R trimmed_left_with_spaces(T &&pattern) const
Get a string with the characters specified by the string literal removed, as well as whitespace chara...
Definition strexpr.h:4902
constexpr size_t find_last(str_piece pattern, size_t offset=-1) const noexcept
Find the beginning of the last occurrence of a substring in this string.
Definition strexpr.h:3901
constexpr size_t find_last_of(str_piece pattern, size_t offset=str::npos) const noexcept
Find the last occurrence of a character from a given character set.
Definition strexpr.h:4125
constexpr size_t find_or_all(K s, size_t offset=0) const noexcept
Find a character in this string or the end of a string.
Definition strexpr.h:3988
constexpr size_t find_last_not_of(str_piece pattern, size_t offset=str::npos) const noexcept
Find the last occurrence of a character not from the given character set.
Definition strexpr.h:4153
std::optional< double > to_double() const noexcept
Convert string to double.
Definition strexpr.h:4261
constexpr void for_all_finded(const Op &op, str_piece pattern, size_t offset=0, size_t maxCount=0) const
Call a functor on all found occurrences of a substring in this string.
Definition strexpr.h:4023
constexpr size_t find_end(str_piece pattern, size_t offset=0) const noexcept
Find the end of the occurrence of a substring in this string.
Definition strexpr.h:3837
std::optional< double > to_double_hex() const noexcept
Convert string in hex form to double.
Definition strexpr.h:4296
constexpr std::optional< R > strip_prefix(str_piece prefix) const
Get a string without a prefix if it starts with one.
Definition strexpr.h:4695
constexpr bool operator!() const noexcept
Check for emptiness.
Definition strexpr.h:3601
constexpr R trimmed() const
Get a string with whitespace removed on the left and right.
Definition strexpr.h:4792
constexpr size_t size() const
The size of the string in characters.
Definition strexpr.h:3377
constexpr To find_all(str_piece pattern, size_t offset=0, size_t maxCount=0) const
Find all occurrences of a substring in this string.
Definition strexpr.h:4046
constexpr my_type str_mid(size_t from, size_t len=-1) const
Get part of a string with an object of the same type to which the method is applied,...
Definition strexpr.h:4184
constexpr size_t find_end_of_last(str_piece pattern, size_t offset=-1) const noexcept
Find the end of the last occurrence of a substring in this string.
Definition strexpr.h:3914
constexpr std::pair< size_t, size_t > find_first_of_idx(str_piece pattern, size_t offset=0) const noexcept
Find the first occurrence of a character from a given character set.
Definition strexpr.h:4097
constexpr bool is_ascii() const noexcept
Whether the string contains only ASCII characters.
Definition strexpr.h:4603
constexpr size_t find(str_piece pattern, size_t offset=0) const noexcept
Find the beginning of the first occurrence of a substring in this string.
Definition strexpr.h:3801
constexpr SplitterBase< K, str_piece > splitter(str_piece delimiter) const
Retrieve a Splitter object by the given splitter, which allows sequential get substrings using the ne...
Definition strexpr.h:5119
constexpr std::pair< size_t, size_t > find_last_of_idx(str_piece pattern, size_t offset=str::npos) const noexcept
Find the last occurrence of a character from a given character set.
Definition strexpr.h:4138
constexpr R trimmed_left(T &&pattern) const
Get a string with the characters specified by the string literal removed from the left.
Definition strexpr.h:4846
constexpr size_t find_or_all(str_piece pattern, size_t offset=0) const noexcept
Find the beginning of the first occurrence of a substring in this string or the end of the string.
Definition strexpr.h:3851
constexpr R trimmed_left(str_piece pattern) const
Get a string with characters specified by another string removed from the left.
Definition strexpr.h:4952
constexpr bool operator==(T &&other) const noexcept
Operator for comparing a string and a string literal for equality.
Definition strexpr.h:3702
constexpr auto operator<=>(const base &other) const noexcept
String comparison operator.
Definition strexpr.h:3692
constexpr bool contains(str_piece pattern, size_t offset=0) const noexcept
Whether the string contains the specified substring.
Definition strexpr.h:3956
constexpr R trimmed_suffix_ia(str_piece suffix) const
Returns a string with the end of the string removed if it ends with the specified suffix,...
Definition strexpr.h:5102
constexpr str_piece operator()(ptrdiff_t from, ptrdiff_t len=0) const noexcept
Get part of a string as "str_src".
Definition strexpr.h:3463
constexpr K at(ptrdiff_t idx) const
Get the character at the given position.
Definition strexpr.h:3614
R lowered_only_ascii() const
Get a copy of the string in lowercase ASCII characters.
Definition strexpr.h:4659
constexpr R trimmed_with_spaces(str_piece pattern) const
Get a string, removing characters specified by another string, as well as whitespace characters,...
Definition strexpr.h:4984
constexpr auto operator<=>(T &&other) const noexcept
Comparison operator between a string and a string literal.
Definition strexpr.h:3712
constexpr std::basic_string< D, Traits, Allocator > to_string() const
Convert to std::basic_string.
Definition strexpr.h:3408
constexpr R trimmed_right(T &&pattern) const
Get a string with the characters specified by the string literal removed from the right.
Definition strexpr.h:4861
constexpr T splitf(str_piece delimiter, const Op &beforeFunc, size_t offset=0) const
Split a string into parts at a given delimiter, possibly applying a functor to each substring.
Definition strexpr.h:4421
constexpr R trimmed(T &&pattern) const
Get a string with the characters specified by the string literal removed from the left and right.
Definition strexpr.h:4831
constexpr size_t find_end_of_last_or_all(str_piece pattern, size_t offset=-1) const noexcept
Find the end of the last occurrence of a substring in this string, or the end of a string.
Definition strexpr.h:3942
constexpr bool prefix_in(str_piece text) const noexcept
Whether this string is the beginning of another string.
Definition strexpr.h:4555
constexpr std::optional< R > strip_prefix_ia(str_piece prefix) const
Get a string without a prefix if it starts with it, insensitive to ASCII characters.
Definition strexpr.h:4714
constexpr bool starts_with_ia_and_oneof(str_piece prefix, str_piece next_symbol) const noexcept
Check if a string begins with the specified case-insensitive ASCII substring followed by one of the s...
Definition strexpr.h:4529
constexpr std::optional< R > strip_suffix_ia(str_piece suffix) const
Get a string without a suffix if it ends with one in case-insensitive ASCII characters.
Definition strexpr.h:4752
constexpr R trimmed_suffix(str_piece suffix) const
Returns a string with the beginning of the string removed if it begins with the specified prefix.
Definition strexpr.h:5082
constexpr K * place(K *ptr) const noexcept
Copy the string to the specified buffer.
Definition strexpr.h:3351
constexpr convert_result< T > to_int() const noexcept
Convert a string to a number of the given type.
Definition strexpr.h:4249
constexpr bool equal_ia(str_piece text) const noexcept
Whether a string is equal to another string, character-by-character-insensitive, of ASCII characters.
Definition strexpr.h:3759
constexpr void as_number(T &t) const
Convert a string to an integer.
Definition strexpr.h:4324
constexpr bool starts_with_and_oneof(str_piece prefix, str_piece next_symbol) const noexcept
Check if a string begins with the specified substring followed by one of the specified characters.
Definition strexpr.h:4474
The concept of a string expression compatible with a given character type.
Definition strexpr.h:525
Concept of "String Expressions".
Definition strexpr.h:507
Base concept of string object.
Definition strexpr.h:311
Checks whether two types are compatible string types.
Definition strexpr.h:172
constexpr expr_if< A > e_if(bool c, const A &a)
Creating a conditional string expression expr_if.
Definition strexpr.h:1544
simstr::expr_stdstr< typename T::value_type, T > operator+(const T &str)
Unary operator + for converting standard strings to string expressions.
Definition strexpr.h:7623
constexpr expr_spaces< uws, N > e_spcw()
Generates a string of N wchar_t spaces.
Definition strexpr.h:1104
expr_fill< K, A, true > e_fill_left(const A &a, size_t width, K symbol=K(' '))
Creates an expression that expands the specified string expression to the specified length given char...
Definition strexpr.h:2699
constexpr empty_expr< uws > eew
Empty string expression of type wchar_t.
Definition strexpr.h:862
constexpr expr_spaces< u8s, N > e_spca()
Generates a string of N char spaces.
Definition strexpr.h:1086
constexpr empty_expr< u32s > eeuu
Empty string expression of type char32_t.
Definition strexpr.h:874
constexpr expr_num< K, T > e_num(T t)
Convert an integer to a string expression.
Definition strexpr.h:1720
HexFlags
Flags for the e_hex function.
Definition strexpr.h:2527
constexpr auto e_hex(T v)
Creates an object that can be converted to a string expression that generates a hexadecimal represent...
Definition strexpr.h:2584
constexpr strexprjoin< A, B > operator+(const A &a, const B &b)
An addition operator for two arbitrary string expressions of the same character type.
Definition strexpr.h:719
constexpr expr_repeat_lit< K, M - 1 > e_repeat(T &&s, size_t l)
Generate a string from l string constants s of type K.
Definition strexpr.h:1218
constexpr expr_literal< typename const_lit< T >::symb_type, static_cast< size_t >(N - 1)> e_t(T &&s)
Converts a string literal to a string expression.
Definition strexpr.h:991
expr_fill< K, A, false > e_fill_right(const A &a, size_t width, K symbol=K(' '))
Creates an expression that expands the specified string expression to the specified length given char...
Definition strexpr.h:2725
constexpr expr_choice< A, B > e_choice(bool c, const A &a, const B &b)
Create a conditional string expression expr_choice.
Definition strexpr.h:1466
constexpr expr_char< K > e_char(K s)
Generates a string of 1 given character.
Definition strexpr.h:918
constexpr empty_expr< u8s > eea
Empty string expression of type char.
Definition strexpr.h:850
constexpr expr_pad< K > e_c(size_t l, K s)
Generates a string of l characters s of type K.
Definition strexpr.h:1149
constexpr empty_expr< u16s > eeu
Empty string expression of type char16_t.
Definition strexpr.h:868
auto e_repl_const_symbols(A &&src, Repl &&... other)
Returns a string expression that generates a string containing the given characters replaced with giv...
Definition strexpr.h:6237
constexpr auto e_int(T v)
Creates an object that is converted to a string expression that generates a string representation of ...
Definition strexpr.h:2383
constexpr empty_expr< u8s > eeb
Empty string expression of type char8_t.
Definition strexpr.h:856
constexpr auto e_repl(A &&w, T &&p, X &&r)
Get a string expression that generates a string with all occurrences of a given substring replaced.
Definition strexpr.h:5744
constexpr auto e_join(const T &s, L &&d)
Get a string expression concatenating the strings in the container into a single string with the give...
Definition strexpr.h:2803
@ Short
without leading zeroes
Definition strexpr.h:2528
Small namespace for standard string methods.
Definition strexpr.h:1600
std::basic_string< K, std::char_traits< K >, A > & append(std::basic_string< K, std::char_traits< K >, A > &str, const E &expr)
Append a string expression to a standard string.
Definition strexpr.h:7162
std::basic_string< K, std::char_traits< K >, A > & prepend(std::basic_string< K, std::char_traits< K >, A > &str, const E &expr)
Insert a string expression at the beginning of a standard string.
Definition strexpr.h:7195
std::basic_string< K, std::char_traits< K >, A > & make_ascii_upper(std::basic_string< K, std::char_traits< K >, A > &str, size_t from=0, size_t to=-1)
Change lowercase ASCII string characters (a-z) to uppercase.
Definition strexpr.h:7561
std::basic_string< K, std::char_traits< K >, A > & change(std::basic_string< K, std::char_traits< K >, A > &str, size_t from, size_t count, const E &expr)
Replace a portion of a standard string with the given string expression.
Definition strexpr.h:7089
std::basic_string< K, std::char_traits< K >, A > & replace(std::basic_string< K, std::char_traits< K >, A > &str, T &&pattern, const E &repl, size_t offset=0, size_t max_count=-1)
A function for searching for substrings in a standard string and replacing the found occurrences with...
Definition strexpr.h:7420
std::basic_string< K, std::char_traits< K >, A > & make_ascii_lower(std::basic_string< K, std::char_traits< K >, A > &str, size_t from=0, size_t to=-1)
Change uppercase ASCII string characters (A-Z) to lowercase.
Definition strexpr.h:7584
std::basic_string< K, std::char_traits< K >, A > & overwrite(std::basic_string< K, std::char_traits< K >, A > &str, const E &expr)
Rewrite the entire string with the given string expression, resizing the string if necessary.
Definition strexpr.h:7259
std::basic_string< K, std::char_traits< K >, A > & insert(std::basic_string< K, std::char_traits< K >, A > &str, size_t from, const E &expr)
Insert a string expression at the specified position in a standard string.
Definition strexpr.h:7230
Library namespace.
Definition sstring.cpp:12
IntConvertResult
Enumeration with possible results of converting a string to an integer.
Definition strexpr.h:2870
@ Overflow
Переполнение, число не помещается в заданный тип
Definition strexpr.h:2873
@ Success
Успешно
Definition strexpr.h:2871
@ NotNumber
Вообще не число
Definition strexpr.h:2874
@ BadSymbolAtTail
Число закончилось не числовым символом
Definition strexpr.h:2872
Some methods for working with standard strings.
Definition strexpr.h:7602
Generate a string based on the original one, replacing all ASCII uppercase letters (A-Z) with lowerca...
Definition strexpr.h:7042
Generate a string based on the original one, replacing all ASCII lowercase letters (a-z) with upperca...
Definition strexpr.h:7019
constexpr e_concat(G &&glue, Arg &&arg, Args &&...args)
Create a string expression concatenating the specified string expressions using the specified delimit...
Definition strexpr.h:6576
constexpr e_subst(const details::subst_params< K, PtLen, NParams > &subst, Args &&...args)
Creates a string expression that substitutes the values of the passed string expressions into the s...
Definition strexpr.h:6798
constexpr e_vsubst(str_src< K > pattern, Args &&...args)
Creates a string expression that substitutes the values of the passed string expressions into the s...
Definition strexpr.h:6897
An "empty" string expression.
Definition strexpr.h:835
Conditional selection string expression.
Definition strexpr.h:1345
Conditional selection string expression.
Definition strexpr.h:1411
Conditional selection string expression.
Definition strexpr.h:1254
Conditional selection string expression.
Definition strexpr.h:1283
A type of string expression that returns N specified characters.
Definition strexpr.h:1120
constexpr expr_replace_symbols(str_t source, const std::vector< std::pair< K, str_t > > &repl)
Expression constructor.
Definition strexpr.h:6321
A string expression that generates a string replacing all occurrences of the given substring to strin...
Definition strexpr.h:5862
constexpr expr_replaced_e(str_src< K > w, str_src< K > p, const E &e)
Constructor.
Definition strexpr.h:5881
A string expression that generates a string replacing all occurrences of the given substring to anoth...
Definition strexpr.h:5756
constexpr expr_replaced(str_src< K > w, str_src< K > p, str_src< K > r)
Constructor.
Definition strexpr.h:5774
A type of string expression that returns N specified characters.
Definition strexpr.h:1060
A type for using std::basic_string and std::basic_string_view as sources in string expressions.
Definition strexpr.h:1569
Base class for converting string expressions to standard strings.
Definition strexpr.h:659
A class that claims to refer to a null-terminated string.
Definition strexpr.h:5290
constexpr my_type to_nts(size_t from)
Get a null-terminated string by shifting the start by the specified number of characters.
Definition strexpr.h:5353
constexpr str_src_nt(T &&v) noexcept
Constructor from a string literal.
Definition strexpr.h:5324
constexpr str_src_nt(T &&p) noexcept
Explicit constructor from C-string.
Definition strexpr.h:5315
constexpr str_src_nt(const std::basic_string< K, std::char_traits< K >, A > &s) noexcept
Constructor from std::basic_string.
Definition strexpr.h:5342
constexpr str_src_nt(const K *p, size_t l) noexcept
Constructor from pointer and length.
Definition strexpr.h:5330
The simplest class of an immutable non-owning string.
Definition strexpr.h:5154
constexpr str_src(const std::basic_string< K, std::char_traits< K >, A > &s) noexcept
Constructor from std::basic_string.
Definition strexpr.h:5183
constexpr bool is_empty() const noexcept
Check if a string is empty.
Definition strexpr.h:5208
constexpr size_t length() const noexcept
Get the length of the string.
Definition strexpr.h:5194
constexpr K operator[](size_t idx) const
Get the character from the specified position. Bounds checking is not performed.
Definition strexpr.h:5237
constexpr my_type & remove_prefix(size_t delta)
Shifts the start of a string by the specified number of characters.
Definition strexpr.h:5248
constexpr str_src(const K *p, size_t l) noexcept
Constructor from pointer and length.
Definition strexpr.h:5173
constexpr bool is_same(str_src< K > other) const noexcept
Check if two objects point to the same string.
Definition strexpr.h:5217
constexpr str_src(const std::basic_string_view< K, std::char_traits< K > > &s) noexcept
Constructor from std::basic_string_view.
Definition strexpr.h:5188
constexpr const symb_type * symbols() const noexcept
Get a pointer to a constant buffer containing string characters.
Definition strexpr.h:5201
constexpr my_type & remove_suffix(size_t delta)
Shortens the string by the specified number of characters.
Definition strexpr.h:5261
constexpr bool is_part_of(str_src< K > other) const noexcept
Check if a string is part of another string.
Definition strexpr.h:5226
constexpr str_src(T &&v) noexcept
Constructor from a string literal.
Definition strexpr.h:5167
Concatenation of a reference to a string expression and the value of the string expression.
Definition strexpr.h:744
Template class for concatenating two string expressions into one using operator +
Definition strexpr.h:682