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.
 
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. More...
 
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. 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/2]

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/2]

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 }