Loading...
Searching...
No Matches
STLUtilities Namespace Reference

This file is inspired by the poor quality of string support in STL for what should be trivial capabiltiies, for example setting or appending the ASCII representation of a floating point or integer number to a string. More...

## Functions

int | Tokenize (std::vector< std::string > &result, const char *input, char delimiter)
std::string & | append (std::string &s, float f)
| use std streams API to do float conversion to string, then append it.
std::string & | append (std::string &s, double f)
| use std streams API to do double conversion to string, then append it.
std::string & | append (std::string &s, char c)
| The rest we can handle readily ourselves.
std::string & | append (std::string &s, unsigned char c)
| Similar to signed char case, but this time for unsigned.
std::string & | append (std::string &s, const char *rhs)
| Now append a full C style NUL terminated string to the std::string.
std::string & | append (std::string &s, std::string &rhs)
| Prevent the generic template from picking up std::string.
template<typename T>
std::string & | append (std::string &s, std::vector< T > v, std::string separator="")
| iterate over the provided vector, appending all elements with an optional separator
template<typename T>
std::string & | append (std::string &s, T i)
| This template handles the rest of the cases for integral types.
std::string & | operator<< (std::string &s, char c)
std::string & | operator<< (std::string &s, unsigned char c)
std::string & | operator<< (std::string &s, uint64_t i)
std::string & | operator<< (std::string &s, int64_t i)
template<typename T>
std::string & | operator<< (std::string &s, T val)
template<typename S>
std::string & | append (std::string &s, std::vector< std::string > v, S delimeter, bool itemize=false)
template<typename T, typename S>
std::string & | append (std::string &s, std::vector< T > v, S delimeter, bool itemize=false)

## Detailed Description

This file is inspired by the poor quality of string support in STL for what should be trivial capabiltiies, for example setting or appending the ASCII representation of a floating point or integer number to a string.

This file uses variadic templates to implement a type safe version (subset) of C-library printf.

Therefore, -std=c++0x is a required option on g++

## Function Documentation

## append() [1/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | char | c )

inline

The rest we can handle readily ourselves.

Unlike std::string operator +, this operator treats c as a character and appends the ASCII character c.

Definition at line 75 of file STLUtilities.h.

{
s += c;
return s;
}

## append() [2/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | const char * | rhs )

inline

Now append a full C style NUL terminated string to the std::string.

Definition at line 94 of file STLUtilities.h.

{
s += rhs;
return s;
}

## append() [3/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | double | f )

inline

use std streams API to do double conversion to string, then append it.

Definition at line 62 of file STLUtilities.h.

{
std::ostringstream buffer;
buffer << f;
s += buffer.str();
return s;
}

## append() [4/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | float | f )

inline

use std streams API to do float conversion to string, then append it.

Definition at line 50 of file STLUtilities.h.

{
std::ostringstream buffer;
buffer << f;
s += buffer.str();
return s;
}

## append() [5/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | std::string & | rhs )

inline

Prevent the generic template from picking up std::string.

Definition at line 103 of file STLUtilities.h.

{
s += rhs;
return s;
}

## append() [6/10]

template<typename S>

std::string & STLUtilities::append | ( | std::string & | s,
|  | std::vector< std::string > | v,
|  | S | delimeter,
|  | bool | itemize = false )

Definition at line 183 of file STLUtilities.h.

{
bool showDelimeter = false;
for (std::vector<std::string>::iterator i=v.begin(); i!=v.end(); i++)
{
if (showDelimeter) s << delimeter;
else showDelimeter = true;
if (itemize) s << (i - v.begin()) << ": ";
s << *i;
}
return s;
}

## append() [7/10]

template<typename T, typename S>

std::string & STLUtilities::append | ( | std::string & | s,
|  | std::vector< T > | v,
|  | S | delimeter,
|  | bool | itemize = false )

Definition at line 196 of file STLUtilities.h.

{
bool showDelimeter = false;
for (typename std::vector<T>::iterator i=v.begin(); i!=v.end(); i++)
{
if (showDelimeter) s << delimeter;
else showDelimeter = true;
if (itemize) s << (i - v.begin()) << ": ";
s << *i;
}
return s;
}

## append() [8/10]

template<typename T>

std::string & STLUtilities::append | ( | std::string & | s,
|  | std::vector< T > | v,
|  | std::string | separator = "" )

iterate over the provided vector, appending all elements with an optional separator

Definition at line 113 of file STLUtilities.h.

{
for (typename T::iterator i=v.begin(); i!=v.end(); i++)
{
if (i!=v.begin()) s += separator;
s << *i;
}
return s;
}

## append() [9/10]

template<typename T>

std::string & STLUtilities::append | ( | std::string & | s,
|  | T | i )

This template handles the rest of the cases for integral types.

Not user friendly if you pass in a type T that is for example a std::vector.

Definition at line 128 of file STLUtilities.h.

{
char digits[20];
char *p = digits;
bool negative = false;
if (i<0)
{
negative = true;
i = -i;
}
do
{
*p++ = '0' + i % 10;
i = i/10;
}
while (i);
if (negative) s += '-';
do
{
s += *--p;
}
while (p > digits);
return s;
}

## append() [10/10]

std::string & STLUtilities::append | ( | std::string & | s,
|  | unsigned char | c )

inline

Similar to signed char case, but this time for unsigned.

Definition at line 84 of file STLUtilities.h.

{
s += c;
return s;
}

## operator<<() [1/5]

std::string & STLUtilities::operator<< | ( | std::string & | s,
|  | char | c )

inline

Definition at line 158 of file STLUtilities.h.

{
return append(s, c);
}
STLUtilities::append
std::string & append(std::string &s, float f)
use std streams API to do float conversion to string, then append it.
Definition STLUtilities.h:50

## operator<<() [2/5]

std::string & STLUtilities::operator<< | ( | std::string & | s,
|  | int64_t | i )

inline

Definition at line 173 of file STLUtilities.h.

{
return append(s, i);
}

## operator<<() [3/5]

template<typename T>

std::string & STLUtilities::operator<< | ( | std::string & | s,
|  | T | val )

Definition at line 178 of file STLUtilities.h.

{
return append(s, val);
}

## operator<<() [4/5]

std::string & STLUtilities::operator<< | ( | std::string & | s,
|  | uint64_t | i )

inline

Definition at line 168 of file STLUtilities.h.

{
return append(s, i);
}

## operator<<() [5/5]

std::string & STLUtilities::operator<< | ( | std::string & | s,
|  | unsigned char | c )

inline

Definition at line 163 of file STLUtilities.h.

{
return append(s, c);
}

## Tokenize()

int STLUtilities::Tokenize | ( | std::vector< std::string > & | result,
|  | const char * | input,
|  | char | delimiter )

Definition at line 31 of file STLUtilities.cpp.

{
if (*input=='\0')
{
result.clear();
result.resize(1); // one word, and it is empty
return 0;
}
size_t wordCount = 1;
// since input is non-empty, we know we will have at least
// one word, so we allocate it here, and begin to fill it in
if (result.size()<wordCount) result.resize(1);
else result[0].clear();
std::string *word = &result[0];
while (*input)
{
if (*input==delimiter)
{
// we got a delimeter, and since an empty word following
// a delimeter still counts as a word, we allocate it here
wordCount++;
if (result.size()<wordCount) result.resize(wordCount);
else
{
result[wordCount-1].clear();
}
word = &result[wordCount-1];
}
else
{
// save the char in this word
word->push_back(*input);
}
input++;
}
if (wordCount < result.size()) result.resize(wordCount); // potentially truncate to wordCount elements
return result.size();
}