18#ifndef _STLUTILITIES_H
19#define _STLUTILITIES_H
50inline std::string &
append(std::string &s,
float f)
52 std::ostringstream buffer;
62inline std::string &
append(std::string &s,
double f)
64 std::ostringstream buffer;
75inline std::string &
append(std::string &s,
char c)
84inline std::string &
append(std::string &s,
unsigned char c)
94inline std::string &
append(std::string &s,
const char *rhs)
103inline std::string &
append(std::string &s, std::string &rhs)
113template<
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;
128template<
typename T> std::string &
append(std::string &s, T i)
132 bool negative =
false;
147 if (negative) s +=
'-';
158inline std::string &operator <<(std::string &s,
char c)
163inline std::string &operator <<(std::string &s,
unsigned char c)
168inline std::string &operator <<(std::string &s, uint64_t i)
173inline std::string &operator <<(std::string &s, int64_t i)
178template<
typename T> std::string &operator <<(std::string &s, T val)
183template<
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()) <<
": ";
196template<
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()) <<
": ";
217int Tokenize(std::vector<std::string> &result,
const char *input,
char delimiter);
230#if defined(__GXX_EXPERIMENTAL_CXX0X__)
243inline void fprintf(std::ostream &stream,
const char* s)
247 if (*s ==
'%' && *++s !=
'%')
248 throw std::runtime_error(
"invalid format string: missing arguments");
253template<
typename T,
typename... Args>
254void 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");
339template<
typename T,
typename... Args>
340void printf(
const char* s,
const T& value,
const Args&... args)
342 fprintf(std::cout, s, value, args...);
345template<
typename... Args>
346void 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();
This file is inspired by the poor quality of string support in STL for what should be trivial capabil...
std::string & append(std::string &s, float f)
use std streams API to do float conversion to string, then append it.