39 namespace stringutil {
49 inline std::string
form(
const char * format, ... )
50 __attribute__ ((format (printf, 1, 2)));
61 inline std::
string form( const
char * format, ... ) {
66 va_start( ap, format );
69 int numprinted = vasprintf( &buf, format, ap );
70 if ( numprinted >= 0 ) {
79 va_start( ap1, format );
80 buf =
new char[vsnprintf( NULL, 0, format, ap ) + 1];
81 vsprintf( buf, format, ap1 );
101 inline std::string
numstring(
char n,
int w = 0 ) {
return form(
"%*hhd", w, n ); }
102 inline std::string
numstring(
unsigned char n,
int w = 0 ) {
return form(
"%*hhu", w, n ); }
103 inline std::string
numstring(
short n,
int w = 0 ) {
return form(
"%*hd", w, n ); }
104 inline std::string
numstring(
unsigned short n,
int w = 0 ) {
return form(
"%*hu", w, n ); }
105 inline std::string
numstring(
int n,
int w = 0 ) {
return form(
"%*d", w, n ); }
106 inline std::string
numstring(
unsigned n,
int w = 0 ) {
return form(
"%*u", w, n ); }
107 inline std::string
numstring(
long n,
int w = 0 ) {
return form(
"%*ld", w, n ); }
108 inline std::string
numstring(
unsigned long n,
int w = 0 ) {
return form(
"%*lu", w, n ); }
109 inline std::string
numstring(
long long n,
int w = 0 ) {
return form(
"%*lld", w, n ); }
110 inline std::string
numstring(
unsigned long long n,
int w = 0 ) {
return form(
"%*llu", w, n ); }
122 inline std::string
hexstring(
char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
123 inline std::string
hexstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hhx", w, n ); }
124 inline std::string
hexstring(
short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
125 inline std::string
hexstring(
unsigned short n,
int w = 10 ){
return form(
"%#0*hx", w, n ); }
126 inline std::string
hexstring(
int n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
127 inline std::string
hexstring(
unsigned n,
int w = 10 ){
return form(
"%#0*x", w, n ); }
128 inline std::string
hexstring(
long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
129 inline std::string
hexstring(
unsigned long n,
int w = 10 ){
return form(
"%#0*lx", w, n ); }
130 inline std::string
hexstring(
long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
131 inline std::string
hexstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llx", w, n ); }
143 inline std::string
octstring(
char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
144 inline std::string
octstring(
unsigned char n,
int w = 4 ) {
return form(
"%#0*hho", w, n ); }
145 inline std::string
octstring(
short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
146 inline std::string
octstring(
unsigned short n,
int w = 5 ) {
return form(
"%#0*ho", w, n ); }
147 inline std::string
octstring(
int n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
148 inline std::string
octstring(
unsigned n,
int w = 5 ) {
return form(
"%#0*o", w, n ); }
149 inline std::string
octstring(
long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
150 inline std::string
octstring(
unsigned long n,
int w = 5 ) {
return form(
"%#0*lo", w, n ); }
151 inline std::string
octstring(
long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
152 inline std::string
octstring(
unsigned long long n,
int w = 0 ) {
return form(
"%#0*llo", w, n ); }
157 template<
typename _It>
161 inline short strtonum(
const std::string &
str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
163 inline int strtonum(
const std::string &
str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
165 inline long strtonum(
const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
167 inline long long strtonum(
const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
170 inline unsigned short strtonum(
const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
172 inline unsigned strtonum(
const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
174 inline unsigned long strtonum(
const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
176 inline unsigned long long strtonum(
const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
181 template<
typename _It>
182 inline _It
strtonum(
const std::string & str, _It & i ) {
return i = strtonum<_It>(
str ); }
208 extern std::string
getline( std::istream & str,
bool trim =
false );
214 extern std::string
getline( std::istream & str,
const Trim trim_r );
246 extern unsigned split(
const std::string line_r,
247 std::vector<std::string> & words_r,
248 const std::string & sep_t =
" \t",
249 const bool singlesep_r =
false );
254 extern std::string
join(
const std::vector<std::string> & words_r,
255 const std::string & sep_r =
" " );
266 inline std::list<std::string>
splitToLines(
const std::string text_r,
const std::string & sep_r =
"\n" )
268 std::vector<std::string> lines;
270 std::list<std::string> ret;
271 for (
unsigned i = 0; i < lines.size(); ++i ) {
272 ret.push_back( lines[i] );
294 extern std::string
stripFirstWord( std::string & value,
const bool ltrim_first =
false );
299 extern std::string
ltrim(
const std::string & s );
300 extern std::string
rtrim(
const std::string & s );
301 inline std::string
trim(
const std::string & s,
const Trim trim_r =
TRIM ) {
318 extern std::string
toLower(
const std::string & s );
319 extern std::string
toUpper(
const std::string & s );
324 extern std::ostream &
dumpOn( std::ostream & str,
const std::list<std::string> & l,
const bool numbered =
false );
325 extern std::ostream &
dumpOn( std::ostream & str,
const std::vector<std::string> & l,
const bool numbered =
false );
331 #endif // stringutil_h