libStatGen Software 1
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. More...
 
std::string & append (std::string &s, double f)
 use std streams API to do double conversion to string, then append it. More...
 
std::string & append (std::string &s, char c)
 The rest we can handle readily ourselves. More...
 
std::string & append (std::string &s, unsigned char c)
 Similar to signed char case, but this time for unsigned. More...
 
std::string & append (std::string &s, const char *rhs)
 Now append a full C style NUL terminated string to the std::string. More...
 
std::string & append (std::string &s, std::string &rhs)
 Prevent the generic template from picking up std::string. More...
 
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 More...
 
template<typename T >
std::string & append (std::string &s, T i)
 This template handles the rest of the cases for integral types. More...
 
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.

76{
77 s += c;
78 return s;
79}

◆ 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.

95{
96 s += rhs;
97 return s;
98}

◆ 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.

63{
64 std::ostringstream buffer;
65 buffer << f;
66 s += buffer.str();
67 return s;
68}

◆ 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.

51{
52 std::ostringstream buffer;
53 buffer << f;
54 s += buffer.str();
55 return s;
56}

◆ 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.

104{
105 s += rhs;
106 return s;
107}

◆ append() [6/10]

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

Definition at line 183 of file STLUtilities.h.

184{
185 bool showDelimeter = false;
186 for (std::vector<std::string>::iterator i=v.begin(); i!=v.end(); i++)
187 {
188 if (showDelimeter) s << delimeter;
189 else showDelimeter = true;
190 if (itemize) s << (i - v.begin()) << ": ";
191 s << *i;
192 }
193 return s;
194}

◆ append() [7/10]

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

Definition at line 196 of file STLUtilities.h.

197{
198 bool showDelimeter = false;
199 for (typename std::vector<T>::iterator i=v.begin(); i!=v.end(); i++)
200 {
201 if (showDelimeter) s << delimeter;
202 else showDelimeter = true;
203 if (itemize) s << (i - v.begin()) << ": ";
204 s << *i;
205 }
206 return s;
207}

◆ 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.

114{
115 for (typename T::iterator i=v.begin(); i!=v.end(); i++)
116 {
117 if (i!=v.begin()) s += separator;
118 s << *i;
119 }
120 return s;
121}

◆ append() [9/10]

template<typename T >
std::string & STLUtilities::append ( std::string &  s,
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.

129{
130 char digits[20];
131 char *p = digits;
132 bool negative = false;
133
134 if (i<0)
135 {
136 negative = true;
137 i = -i;
138 }
139
140 do
141 {
142 *p++ = '0' + i % 10;
143 i = i/10;
144 }
145 while (i);
146
147 if (negative) s += '-';
148
149 do
150 {
151 s += *--p;
152 }
153 while (p > digits);
154
155 return s;
156}

◆ 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.

85{
86 s += c;
87 return s;
88}

◆ operator<<() [1/5]

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

Definition at line 158 of file STLUtilities.h.

159{
160 return append(s, c);
161}
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.

174{
175 return append(s, i);
176}

◆ operator<<() [3/5]

template<typename T >
std::string & STLUtilities::operator<< ( std::string &  s,
val 
)

Definition at line 178 of file STLUtilities.h.

179{
180 return append(s, val);
181}

◆ operator<<() [4/5]

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

Definition at line 168 of file STLUtilities.h.

169{
170 return append(s, i);
171}

◆ operator<<() [5/5]

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

Definition at line 163 of file STLUtilities.h.

164{
165 return append(s, c);
166}

◆ Tokenize()

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

Definition at line 31 of file STLUtilities.cpp.

32{
33 if (*input=='\0')
34 {
35 result.clear();
36 result.resize(1); // one word, and it is empty
37 return 0;
38 }
39
40 size_t wordCount = 1;
41
42 // since input is non-empty, we know we will have at least
43 // one word, so we allocate it here, and begin to fill it in
44 if (result.size()<wordCount) result.resize(1);
45 else result[0].clear();
46
47 std::string *word = &result[0];
48
49 while (*input)
50 {
51 if (*input==delimiter)
52 {
53 // we got a delimeter, and since an empty word following
54 // a delimeter still counts as a word, we allocate it here
55 wordCount++;
56 if (result.size()<wordCount) result.resize(wordCount);
57 else
58 {
59 result[wordCount-1].clear();
60 }
61 word = &result[wordCount-1];
62 }
63 else
64 {
65 // save the char in this word
66 word->push_back(*input);
67 }
68 input++;
69 }
70
71 if (wordCount < result.size()) result.resize(wordCount); // potentially truncate to wordCount elements
72
73 return result.size();
74}