proj home

Files   Classes   Functions   Hierarchy  

stringconvert.h File Reference

#include <sstream>
#include <typedefs.h>

Include dependency graph for stringconvert.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  stringconvert
 String helper functions. More...

Functions

template<typename T >
string & operator<< (string &str, T const &x)
 String serialize by appending to a string with the same syntax as the operator << (ostream &,T).
template<typename T >
void stringfrom (T &targ, stringc &str)
 Convert from a string.
template<typename T >
void stringto (string &str, T const data)
 Convert to a string.
template<typename T >
stringc stringto (T const data)
 Convert to a string.
boolc isstringdigits (stringc &str)
 Is the string digits only?
boolc isstringinteger (stringc &str)
 Is the string an integer? Not testing the value so 7.0 is not regared as an integer.
boolc isstringzero_or_positive_real (stringc &str)
 e.g.
boolc isstringreal (stringc &str)
 Integers are a subset of the reals.
boolc isstringnegative (stringc &str)
 Looks for negative sign and a real number.
template<typename T >
stringc stringtag (T x, stringc &tag)
 Helper function to wrap data in tags.


Function Documentation

boolc isstringdigits ( stringc str  ) 

Is the string digits only?

Definition at line 10 of file stringconvert.cpp.

Referenced by isstringinteger(), isstringzero_or_positive_real(), and misclib_testcode::stringconverttest::unittest01().

00011 {
00012   if (str.empty())
00013     return false;
00014 
00015   for (size_t i=0; i<str.length(); ++i)
00016   {
00017 //cout << i << ":" << str[i] << "  " << isdigit(str[i]) << endl;
00018     if (isdigit(str[i])==false)
00019       return false;
00020   }
00021 
00022   return true;
00023 }

boolc isstringinteger ( stringc str  ) 

Is the string an integer? Not testing the value so 7.0 is not regared as an integer.

Definition at line 25 of file stringconvert.cpp.

References isstringdigits(), and isstringinteger().

Referenced by isstringinteger(), and misclib_testcode::stringconverttest::unittest01().

00026 {
00027   if (str.empty())
00028     return false;
00029 
00030   if (str[0]=='-')
00031     return isstringinteger(str.substr(1));
00032 
00033   return isstringdigits(str);
00034 }

boolc isstringnegative ( stringc str  ) 

Looks for negative sign and a real number.

Definition at line 76 of file stringconvert.cpp.

References isstringreal().

Referenced by misclib_testcode::stringconverttest::unittest01().

00077 {
00078   if (str.empty())
00079     return false;
00080 
00081   if (str[0] != '-')
00082     return false;
00083 
00084   return isstringreal(str);
00085 }

boolc isstringreal ( stringc str  ) 

Integers are a subset of the reals.

Definition at line 65 of file stringconvert.cpp.

References isstringzero_or_positive_real().

Referenced by isstringnegative(), and misclib_testcode::stringconverttest::unittest01().

00066 {
00067   if (str.empty())
00068     return false;
00069 
00070   if (str[0]=='-')
00071     return isstringzero_or_positive_real(str.substr(1));
00072 
00073   return isstringzero_or_positive_real(str);
00074 }

boolc isstringzero_or_positive_real ( stringc str  ) 

e.g.

float or double >= zero.

Definition at line 36 of file stringconvert.cpp.

References isstringdigits().

Referenced by isstringreal().

00037 {
00038   if (str.empty())
00039     return false;
00040 
00041   if (str[0]=='.')
00042     return isstringdigits(str.substr(1));
00043 
00044   size_t k;
00045   uint kcount=0;
00046   for (size_t i=0; i<str.length(); ++i)
00047   {
00048     if (str[i]=='.')
00049     {
00050       k=i;
00051       ++kcount;
00052       continue;
00053     }
00054 
00055     if (isdigit(str[i])==false)
00056       return false;
00057   }
00058 
00059   if (kcount>1)
00060     return false;
00061 
00062   return true;
00063 }

template<typename T >
string & operator<< ( string &  str,
T const &  x 
) [inline]

String serialize by appending to a string with the same syntax as the operator << (ostream &,T).

For example: string str; ... str << "t=" << t << "\n";

Definition at line 106 of file stringconvert.h.

00107 {
00108   stringstream ss;
00109   ss << x;
00110   str += ss.str(); 
00111 
00112   return str;
00113 }

template<typename T >
void stringfrom ( T targ,
stringc str 
) [inline]

template<typename T >
stringc stringtag ( T  x,
stringc tag 
) [inline]

template<typename T >
stringc stringto ( T const   data  )  [inline]

Convert to a string.

Function form of stringto.

Definition at line 131 of file stringconvert.h.

00132 {
00133   stringstream ss;
00134   ss << data;
00135   return ss.str();
00136 }

template<typename T >
void stringto ( string &  str,
T const   data 
) [inline]

Convert to a string.

string s; stringto(s,36);

Definition at line 123 of file stringconvert.h.

Referenced by mazematrixD2< T >::operator stringc(), mazegameD2state01::operator stringc(), windowscaleD2::operator stringc(), mazedisp03::settings(), stringtag(), and stringtagparsertest::test02().

00124 {
00125   stringstream ss;
00126   ss << data;
00127   str = ss.str();
00128 }


Generated on Fri Mar 4 00:49:43 2011 for Chelton Evans Source by  doxygen 1.5.8