18 #ifndef _STLUTILITIES_H 19 #define _STLUTILITIES_H 50 inline std::string &
append(std::string &s,
float f)
52 std::ostringstream buffer;
62 inline std::string &
append(std::string &s,
double f)
64 std::ostringstream buffer;
75 inline std::string &
append(std::string &s,
char c)
84 inline std::string &
append(std::string &s,
unsigned char c)
94 inline std::string &
append(std::string &s,
const char *rhs)
103 inline std::string &
append(std::string &s, std::string &rhs)
113 template<
typename T> std::string &
append(std::string &s, std::vector<T> v, std::string separator=
"")
115 for (
typename T::iterator i=v.begin(); i!=v.end(); i++)
117 if (i!=v.begin()) s += separator;
128 template<
typename T> std::string &
append(std::string &s, T i)
132 bool negative =
false;
147 if (negative) s +=
'-';
158 inline std::string &operator <<(std::string &s,
char c)
163 inline std::string &operator <<(std::string &s,
unsigned char c)
168 inline std::string &operator <<(std::string &s, uint64_t i)
173 inline std::string &operator <<(std::string &s, int64_t i)
178 template<
typename T> std::string &operator <<(std::string &s, T val)
183 template<
typename S> std::string &
append(std::string &s, std::vector<std::string> v, S delimeter,
bool itemize =
false)
185 bool showDelimeter =
false;
186 for (std::vector<std::string>::iterator i=v.begin(); i!=v.end(); i++)
188 if (showDelimeter) s << delimeter;
189 else showDelimeter =
true;
190 if (itemize) s << (i - v.begin()) <<
": ";
196 template<
typename T,
typename S> std::string &
append(std::string &s, std::vector<T> v, S delimeter,
bool itemize =
false)
198 bool showDelimeter =
false;
199 for (
typename std::vector<T>::iterator i=v.begin(); i!=v.end(); i++)
201 if (showDelimeter) s << delimeter;
202 else showDelimeter =
true;
203 if (itemize) s << (i - v.begin()) <<
": ";
217 int Tokenize(std::vector<std::string> &result,
const char *input,
char delimiter);
230 #if defined(__GXX_EXPERIMENTAL_CXX0X__) 243 inline void fprintf(std::ostream &stream,
const char* s)
247 if (*s ==
'%' && *++s !=
'%')
248 throw std::runtime_error(
"invalid format string: missing arguments");
253 template<
typename T,
typename... Args>
254 void fprintf(std::ostream &stream,
const char* s,
const T& value,
const Args&... args)
258 if (*s ==
'%' && *++s !=
'%')
260 bool leftJustify =
false;
261 bool zeroPad =
false;
278 while (*s && isdigit(*s))
281 fieldWidth += (*s -
'0');
289 while (*s && isdigit(*s))
292 precision += (*s -
'0');
304 stream << std::setw(fieldWidth) << (leftJustify ? std::left : std::right) << value;
310 stream << std::setw(fieldWidth) << std::setfill(fillChar) << (leftJustify ? std::left : std::right) << std::hex << value;
323 stream << std::setw(fieldWidth) << std::setfill(fillChar) << (leftJustify ? std::left : std::right) << std::dec << value;
326 throw std::runtime_error(
"Unrecognized printf conversion character");
331 fprintf(stream, s, args...);
336 throw std::runtime_error(
"extra arguments provided to printf");
339 template<
typename T,
typename... Args>
340 void printf(
const char* s,
const T& value,
const Args&... args)
342 fprintf(std::cout, s, value, args...);
345 template<
typename... Args>
346 void sprintf(std::string &buffer,
const char *fmt,
const Args&... args)
348 std::ostringstream stream;
350 fprintf((std::ostream &) stream, fmt, args...);
354 buffer = stream.str();
std::string & append(std::string &s, float f)
use std streams API to do float conversion to string, then append it.
This file is inspired by the poor quality of string support in STL for what should be trivial capabil...