-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.hpp
126 lines (104 loc) · 3 KB
/
String.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <concepts>
#include <string_view>
#ifndef FLAG_STDRANGES
#include <ranges>
#endif
#include <Optimizations.hpp>
using namespace std::string_literals;
#ifndef NDEBUG
#define _printf printf
#define _fprintf(fp, ...) {fprintf(fp, __VA_ARGS__);fflush(fp);}
#else
#define _fprintf(...)
#define _printf(...)
#endif
#define _perror(...) fprintf(stderr, __VA_ARGS__)
namespace str {
template <typename T> concept Stringable = requires (T a) { { std::string(a) }; };
template <typename T> concept ToStringable = requires (T a) { { std::to_string(a) }; };
//#ifndef FLAG_STDRANGES
//decltype(auto) split(const std::string_view &s, const std::string_view &sep=" "s) {
// return s | std::views::split(sep);
//}
//#else
//decltype(auto) split(const std::string_view &s, const std::string_view &sep=" "s) {
// std::vector<std::string> vs;
// size_t start_s = 0;
// for(size_t i = 0; i < s.size() - sep.size(); ++i) {
// if(s.substr(i, sep.size()) == sep) {
// vs.emplace_back(s.substr(start_s, i - start_s));
// start_s = i + sep.size();
// i = start_s - 1;
// }
// }
// if(start_s != s.size()) {
// vs.emplace_back(s.substr(start_s, s.size() - start_s));
// }
// return vs;
//}
//#endif
template <typename ContainerT>
decltype(auto) join(const ContainerT &iterable, const std::string_view &joinstr=", "s)
requires Stringable<typename ContainerT::value_type> || ToStringable<typename ContainerT::value_type>
{
using T = typename ContainerT::value_type;
std::string s;
size_t i = 0;
for(const auto &it : iterable) {
if constexpr(Stringable<T>) {
s += it;
} else if constexpr(ToStringable<T>) {
s += std::to_string(it);
}
if(i + 1 != iterable.size()) {
s += joinstr;
}
++i;
}
return s;
}
template <typename... Ts>
void _unroll_(Ts...){}
template <typename T>
std::string convert_to_string(const T &s) requires Stringable<T> {
return s;
}
template <typename T>
std::string convert_to_string(const T &s) requires ToStringable<T> {
return std::to_string(s);
}
std::string convert_to_string(const char *s) {
return std::string(s);
}
std::string convert_to_string(char *s) {
return std::string(s);
}
template <typename T>
std::string convert_to_string(const std::vector<T> &vs) {
return str::join(vs, " "s);
}
template <typename... Elem>
void perror(const Elem & ...s) {
// unpacking order in initializer lists is defined
std::vector<std::string> v = { convert_to_string(s)... };
std::cerr << str::join(v, " "s) << std::endl;
}
template <typename... Elem>
void print(const Elem & ...s) {
// unpacking order in initializer lists is defined
std::vector<std::string> v = { convert_to_string(s)... };
std::cout << str::join(v, " "s) << std::endl;
}
template <typename... Elem>
INLINE void pdebug(const Elem & ...s) {
#ifndef NDEBUG
std::vector<std::string> v = { convert_to_string(s)... };
std::cout << str::join(v, " "s) << std::endl;
#endif
}
} // namespace str