23 struct two {
char x[2]; };
25 template <
typename C>
static one test( decltype(&C::string) ) ;
26 template <
typename C>
static two test(...);
29 enum {
value =
sizeof(test<T>(0)) ==
sizeof(char) };
50 std::deque<std::string>
split(
const std::string& s,
const char delimiter) {
51 std::deque<std::string> tokens;
59 size_t k = s.find(delimiter, i);
61 if(k == std::string::npos) {
62 tokens.push_back(s.substr(i,std::string::npos));
66 tokens.push_back(s.substr(i,k-i));
84 std::array<std::string, N>
split(
const std::string& s,
const char delimiter) {
85 std::array<std::string, N> out;
87 auto q =
split(s, delimiter);
91 std::cerr <<
"*** String in split<N> has " << q.size() <<
" not " <<
N <<
" elements: " << s << std::endl;
113 if constexpr (std::is_pointer<T>::value) {
114 std::ostringstream address;
115 address << (
void const *)x;
116 return address.str();
118 else if constexpr(std::is_same<T,char>::value) {
119 return std::string(1,x);
125 return std::to_string(x);
129 std::string
str(
const std::string& x){
134 template<
typename... T,
size_t... I >
135 std::string
str(
const std::tuple<T...>& x, std::index_sequence<I...> idx){
141 return "<" + ( (std::to_string(std::get<I>(x)) +
",") + ...) +
">";
143 template<
typename... T>
144 std::string
str(
const std::tuple<T...>& x ){
150 return str(x, std::make_index_sequence<
sizeof...(T)>() );
153 template<
typename A,
typename B >
154 std::string
str(
const std::pair<A,B> x){
160 return "<" +
str(x.first) +
"," +
str(x.second) +
">";
163 template<
typename T,
size_t N>
164 std::string
str(
const std::array<T, N>& a ){
170 std::string out =
"<";
176 out.erase(out.size()-1);
183 std::string
str(
const std::vector<T>& a,
const std::string _sep=
","){
189 std::string out =
"[";
190 for(
const auto& x : a) {
191 out +=
str(x) + _sep;
194 out.erase(out.size()-1);
199 template<
typename T,
typename U>
200 std::string
str(
const std::map<T,U>& a ){
206 std::string out =
"{";
208 out +=
str(x.first) +
":" +
str(x.second) +
",";
211 out.erase(out.size()-1);
218 std::string
str(
const std::set<T>& a ){
224 std::string out =
"{";
229 out.erase(out.size()-1);
237 std::string
str(
const std::atomic<T>& a ){
243 return str(a.load());
247 template<
typename... Args>
248 std::string
str(std::string _sep, Args... args){
250 std::string out =
"";
251 ((out +=
str(args)+_sep), ...);
254 out.erase(out.size()-_sep.size());
275 for(
auto& r :
split(s,
',')) {
276 auto [x, y] = split<2>(r,
':');
277 m[string_to<typename T::key_type>(x)] = string_to<typename T::mapped_type>(y);
282 auto [x,y] = split<2>(s,
':');
283 return {string_to<typename T::first_type>(x), string_to<typename T::second_type>(y)};
287 for(
auto& x :
split(s,
',')) {
288 v.push_back(string_to<typename T::value_type>(x));
294 for(
auto& x :
split(s,
',')) {
295 ret.insert( string_to<typename T::key_type>(x));
305 template<> std::string
string_to(
const std::string s) {
return s; }
306 template<>
int string_to(
const std::string s) {
return std::stoi(s); }
307 template<>
long string_to(
const std::string s) {
return std::stol(s); }
308 template<>
unsigned long string_to(
const std::string s) {
return std::stoul(s); }
309 template<>
double string_to(
const std::string s) {
return std::stod(s); }
310 template<>
float string_to(
const std::string s) {
return std::stof(s); }
311 template<>
bool string_to(
const std::string s) { assert(s.size()==1);
return s==
"1"; }
std::deque< std::string > split(const std::string &s, const char delimiter)
Split is returns a deque of s split up at the character delimiter. It handles these special cases: sp...
Definition: str.h:50
T string_to(const std::string s)
Fleet includes this templated function to allow us to convert strings to a variety of formats...
Definition: str.h:270
std::string str(T x)
A pythonesque string function. This gets specialized in many ways.
Definition: str.h:110
Check if a type is contained in parameter pack // *.
Definition: Miscellaneous.h:137