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) };
44 std::deque<std::string>
split(
const std::string& s,
const char delimiter) {
45 std::deque<std::string> tokens;
53 size_t k = s.find(delimiter, i);
55 if(k == std::string::npos) {
56 tokens.push_back(s.substr(i,std::string::npos));
60 tokens.push_back(s.substr(i,k-i));
78 std::array<std::string, N>
split(
const std::string& s,
const char delimiter) {
79 std::array<std::string, N> out;
81 auto q =
split(s, delimiter);
85 std::cerr <<
"*** String in split<N> has " << q.size() <<
" not " <<
N <<
" elements: " << s << std::endl;
107 if constexpr (std::is_pointer<T>::value) {
108 std::ostringstream address;
109 address << (
void const *)x;
110 return address.str();
112 else if constexpr(std::is_same<T,char>::value) {
113 return std::string(1,x);
119 return std::to_string(x);
123 std::string
str(
const std::string& x){
128 template<
typename... T,
size_t... I >
129 std::string
str(
const std::tuple<T...>& x, std::index_sequence<I...> idx){
135 return "<" + ( (std::to_string(std::get<I>(x)) +
",") + ...) +
">";
137 template<
typename... T>
138 std::string
str(
const std::tuple<T...>& x ){
144 return str(x, std::make_index_sequence<
sizeof...(T)>() );
147 template<
typename A,
typename B >
148 std::string
str(
const std::pair<A,B> x){
154 return "<" +
str(x.first) +
"," +
str(x.second) +
">";
157 template<
typename T,
size_t N>
158 std::string
str(
const std::array<T, N>& a ){
164 std::string out =
"<";
170 out.erase(out.size()-1);
177 std::string
str(
const std::vector<T>& a,
const std::string _sep=
","){
183 std::string out =
"[";
184 for(
const auto& x : a) {
185 out +=
str(x) + _sep;
188 out.erase(out.size()-1);
193 template<
typename T,
typename U>
194 std::string
str(
const std::map<T,U>& a ){
200 std::string out =
"{";
202 out +=
str(x.first) +
":" +
str(x.second) +
",";
205 out.erase(out.size()-1);
212 std::string
str(
const std::set<T>& a ){
218 std::string out =
"{";
223 out.erase(out.size()-1);
231 std::string
str(
const std::atomic<T>& a ){
237 return str(a.load());
241 template<
typename... Args>
242 std::string
str(std::string _sep, Args... args){
244 std::string out =
"";
245 ((out +=
str(args)+_sep), ...);
248 out.erase(out.size()-_sep.size());
269 for(
auto& r :
split(s,
',')) {
270 auto [x, y] = split<2>(r,
':');
271 m[string_to<typename T::key_type>(x)] = string_to<typename T::mapped_type>(y);
276 auto [x,y] = split<2>(s,
':');
277 return {string_to<typename T::first_type>(x), string_to<typename T::second_type>(y)};
281 for(
auto& x :
split(s,
',')) {
282 v.push_back(string_to<typename T::value_type>(x));
288 for(
auto& x :
split(s,
',')) {
289 ret.insert( string_to<typename T::key_type>(x));
299 template<> std::string
string_to(
const std::string s) {
return s; }
300 template<>
int string_to(
const std::string s) {
return std::stoi(s); }
301 template<>
long string_to(
const std::string s) {
return std::stol(s); }
302 template<>
unsigned long string_to(
const std::string s) {
return std::stoul(s); }
303 template<>
double string_to(
const std::string s) {
return std::stod(s); }
304 template<>
float string_to(
const std::string s) {
return std::stof(s); }
305 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:44
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:264
std::string str(T x)
A pythonesque string function. This gets specialized in many ways.
Definition: str.h:104
Check if a type is contained in parameter pack // *.
Definition: Miscellaneous.h:137