G::Str Class Reference

A static class which provides string helper functions. More...

#include <gstr.h>

List of all members.

Public Types

typedef std::string::size_type size_type

Static Public Member Functions

static bool replace (std::string &s, const std::string &from, const std::string &to, size_type *pos_p=NULL)
 Replaces 'from' with 'to', starting at offset '*pos_p'.
static unsigned int replaceAll (std::string &s, const std::string &from, const std::string &to)
 Does a global replace on string 's', replacing all occurences of sub-string 'from' with 'to'.
static unsigned int replaceAll (std::string &s, const char *from, const char *to)
 A c-string overload, provided for performance reasons.
static void removeAll (std::string &, char)
 Removes all occurrences of the character from the string.
static void trimLeft (std::string &s, const std::string &ws, size_type limit=0U)
 Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
static void trimRight (std::string &s, const std::string &ws, size_type limit=0U)
 Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
static void trim (std::string &s, const std::string &ws)
 Trims both ends of s, taking off any of the 'ws' characters.
static std::string trimmed (const std::string &s, const std::string &ws)
 Returns a trim()med version of s.
static bool isNumeric (const std::string &s, bool allow_minus_sign=false)
 Returns true if every character is a decimal digit.
static bool isPrintableAscii (const std::string &s)
 Returns true if every character is a 7-bit, non-control character (ie.
static bool isUShort (const std::string &s)
 Returns true if the string can be converted into an unsigned short without throwing an exception.
static bool isUInt (const std::string &s)
 Returns true if the string can be converted into an unsigned integer without throwing an exception.
static bool isULong (const std::string &s)
 Returns true if the string can be converted into an unsigned long without throwing an exception.
static std::string fromBool (bool b)
 Converts boolean 'b' to a string.
static std::string fromDouble (double d)
 Converts double 'd' to a string.
static std::string fromInt (int i)
 Converts int 'i' to a string.
static std::string fromLong (long l)
 Converts long 'l' to a string.
static std::string fromShort (short s)
 Converts short 's' to a string.
static std::string fromUInt (unsigned int ui)
 Converts unsigned int 'ui' to a string.
static std::string fromULong (unsigned long ul)
 Converts unsigned long 'ul' to a string.
static std::string fromUShort (unsigned short us)
 Converts unsigned short 'us' to a string.
static bool toBool (const std::string &s)
 Converts string 's' to a bool.
static double toDouble (const std::string &s)
 Converts string 's' to a double.
static int toInt (const std::string &s)
 Converts string 's' to an int.
static long toLong (const std::string &s)
 Converts string 's' to a long.
static short toShort (const std::string &s)
 Converts string 's' to a short.
static unsigned int toUInt (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned int.
static unsigned long toULong (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned long.
static unsigned short toUShort (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned short.
static void toUpper (std::string &s)
 Replaces all lowercase characters in string 's' by uppercase characters.
static void toLower (std::string &s)
 Replaces all uppercase characters in string 's' by lowercase characters.
static std::string upper (const std::string &s)
 Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters.
static std::string lower (const std::string &s)
 Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters.
static std::string toPrintableAscii (char c, char escape= '\\')
 Returns a 7-bit printable representation of the given input character.
static std::string toPrintableAscii (const std::string &in, char escape= '\\')
 Returns a 7-bit printable representation of the given input string.
static std::string printable (const std::string &in, char escape= '\\')
 Returns a printable represention of the given input string.
static void escape (std::string &, const std::string &specials, char escape= '\\')
 Prefixes each occurrence of one of the special characters with the escape character.
static std::string escaped (const std::string &, const std::string &specials, char escape= '\\')
 Prefixes each occurrence of one of the special characters with the escape character.
static std::string readLineFrom (std::istream &stream, const std::string &eol=std::string())
 Reads a line from the stream using the given line terminator.
static void readLineFrom (std::istream &stream, const std::string &eol, std::string &result, bool pre_erase_result=true)
 An overload which avoids string copying.
static std::string wrap (std::string text, const std::string &prefix_first_line, const std::string &prefix_subsequent_lines, size_type width=70U)
 Does word-wrapping.
static void splitIntoTokens (const std::string &in, Strings &out, const std::string &ws)
 Splits the string into 'ws'-delimited tokens.
static void splitIntoTokens (const std::string &in, StringArray &out, const std::string &ws)
 Overload for vector<string>.
static void splitIntoFields (const std::string &in, Strings &out, const std::string &seperators, char escape= '\0', bool discard_bogus_escapes=true)
 Splits the string into fields.
static void splitIntoFields (const std::string &in, StringArray &out, const std::string &seperators, char escape= '\0', bool discard_bogus_escapes=true)
 Overload for vector<string>.
static std::string join (const Strings &strings, const std::string &sep)
 Concatenates a set of strings.
static std::string join (const StringArray &strings, const std::string &sep)
 Concatenates a set of strings.
static Strings keys (const StringMap &string_map)
 Extracts the keys from a map of strings.
static std::string head (const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
 Returns the first part of the string up to just before the given position.
static std::string tail (const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
 Returns the last part of the string after the given position.
static bool tailMatch (const std::string &in, const std::string &ending)
 Returns true if the given string has the given ending.
static std::string ws ()
 A convenience function returning standard whitespace characters.


Detailed Description

A static class which provides string helper functions.

Definition at line 41 of file gstr.h.


Member Typedef Documentation

typedef std::string::size_type G::Str::size_type

Definition at line 44 of file gstr.h.


Member Function Documentation

bool G::Str::replace ( std::string &  s,
const std::string &  from,
const std::string &  to,
size_type pos_p = NULL 
) [static]

Replaces 'from' with 'to', starting at offset '*pos_p'.

Returns true if a substitution was made, and adjusts '*pos_p' by to.length().

Definition at line 55 of file gstr.cpp.

Referenced by replaceAll().

unsigned int G::Str::replaceAll ( std::string &  s,
const std::string &  from,
const std::string &  to 
) [static]

Does a global replace on string 's', replacing all occurences of sub-string 'from' with 'to'.

Returns the number of substitutions made. Consider using in a while loop if 'from' is more than one character.

Definition at line 78 of file gstr.cpp.

References replace().

Referenced by GSmtp::ClientProtocolReply::ClientProtocolReply(), G::Executable::Executable(), and filter_run().

unsigned int G::Str::replaceAll ( std::string &  s,
const char *  from,
const char *  to 
) [static]

A c-string overload, provided for performance reasons.

Definition at line 86 of file gstr.cpp.

References replace().

void G::Str::removeAll ( std::string &  s,
char  c 
) [static]

Removes all occurrences of the character from the string.

Definition at line 105 of file gstr.cpp.

void G::Str::trimLeft ( std::string &  s,
const std::string &  ws,
size_type  limit = 0U 
) [static]

Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.

Definition at line 111 of file gstr.cpp.

Referenced by GSmtp::ClientProtocolReply::ClientProtocolReply(), and trim().

void G::Str::trimRight ( std::string &  s,
const std::string &  ws,
size_type  limit = 0U 
) [static]

Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.

Definition at line 122 of file gstr.cpp.

Referenced by trim().

void G::Str::trim ( std::string &  s,
const std::string &  ws 
) [static]

Trims both ends of s, taking off any of the 'ws' characters.

Definition at line 133 of file gstr.cpp.

References trimLeft(), and trimRight().

Referenced by GNet::ResolverImp::onData(), and trimmed().

std::string G::Str::trimmed ( const std::string &  s,
const std::string &  ws 
) [static]

Returns a trim()med version of s.

Definition at line 138 of file gstr.cpp.

References s, and trim().

bool G::Str::isNumeric ( const std::string &  s,
bool  allow_minus_sign = false 
) [static]

Returns true if every character is a decimal digit.

Empty strings return true.

Definition at line 152 of file gstr.cpp.

Referenced by GNet::Resolver::resolve().

bool G::Str::isPrintableAscii ( const std::string &  s  )  [static]

Returns true if every character is a 7-bit, non-control character (ie.

0x20<=c<0x7f). Empty strings return true.

Definition at line 167 of file gstr.cpp.

Referenced by GNet::ResolverImp::onData().

bool G::Str::isUShort ( const std::string &  s  )  [static]

Returns true if the string can be converted into an unsigned short without throwing an exception.

Definition at line 173 of file gstr.cpp.

References toUShort().

bool G::Str::isUInt ( const std::string &  s  )  [static]

Returns true if the string can be converted into an unsigned integer without throwing an exception.

Definition at line 190 of file gstr.cpp.

References toUInt().

Referenced by GNet::Resolver::resolve(), and GNet::Address::wildcards().

bool G::Str::isULong ( const std::string &  s  )  [static]

Returns true if the string can be converted into an unsigned long without throwing an exception.

Definition at line 207 of file gstr.cpp.

References toULong().

std::string G::Str::fromBool ( bool  b  )  [static]

Converts boolean 'b' to a string.

Definition at line 224 of file gstr.cpp.

std::string G::Str::fromDouble ( double  d  )  [static]

Converts double 'd' to a string.

Definition at line 229 of file gstr.cpp.

std::string G::Str::fromInt ( int  i  )  [static]

Converts int 'i' to a string.

Definition at line 236 of file gstr.cpp.

std::string G::Str::fromLong ( long  l  )  [static]

Converts long 'l' to a string.

Definition at line 243 of file gstr.cpp.

std::string G::Str::fromShort ( short  s  )  [static]

Converts short 's' to a string.

Definition at line 250 of file gstr.cpp.

std::string G::Str::fromUInt ( unsigned int  ui  )  [static]

Converts unsigned int 'ui' to a string.

Definition at line 257 of file gstr.cpp.

Referenced by GNet::Address::wildcards().

std::string G::Str::fromULong ( unsigned long  ul  )  [static]

Converts unsigned long 'ul' to a string.

Definition at line 264 of file gstr.cpp.

std::string G::Str::fromUShort ( unsigned short  us  )  [static]

Converts unsigned short 'us' to a string.

Definition at line 271 of file gstr.cpp.

bool G::Str::toBool ( const std::string &  s  )  [static]

Converts string 's' to a bool.

Exception: InvalidFormat

Definition at line 278 of file gstr.cpp.

References lower().

double G::Str::toDouble ( const std::string &  s  )  [static]

Converts string 's' to a double.

Exception: Overflow Exception: InvalidFormat

Definition at line 296 of file gstr.cpp.

References result.

int G::Str::toInt ( const std::string &  s  )  [static]

Converts string 's' to an int.

Exception: Overflow Exception: InvalidFormat

Definition at line 310 of file gstr.cpp.

References toLong().

long G::Str::toLong ( const std::string &  s  )  [static]

Converts string 's' to a long.

Exception: Overflow Exception: InvalidFormat

Definition at line 321 of file gstr.cpp.

References result.

Referenced by toInt(), and toShort().

short G::Str::toShort ( const std::string &  s  )  [static]

Converts string 's' to a short.

Exception: Overflow Exception: InvalidFormat

Definition at line 335 of file gstr.cpp.

References toLong().

unsigned int G::Str::toUInt ( const std::string &  s,
bool  limited = false 
) [static]

Converts string 's' to an unsigned int.

If 'limited' is true then very large numeric strings are limited to the maximum value of the numeric type, without an Overflow exception.

Exception: Overflow Exception: InvalidFormat

Definition at line 346 of file gstr.cpp.

References toULong().

Referenced by GSmtp::ClientProtocolReply::ClientProtocolReply(), isUInt(), GNet::Resolver::resolve(), Main::CommandLine::value(), GNet::Address::wildcards(), and G::GetOpt::wrapDefault().

unsigned long G::Str::toULong ( const std::string &  s,
bool  limited = false 
) [static]

Converts string 's' to an unsigned long.

If 'limited' is true then very large numeric strings are limited to the maximum value of the numeric type, without an Overflow exception.

Exception: Overflow Exception: InvalidFormat

Definition at line 362 of file gstr.cpp.

References result.

Referenced by isULong(), toUInt(), and toUShort().

unsigned short G::Str::toUShort ( const std::string &  s,
bool  limited = false 
) [static]

Converts string 's' to an unsigned short.

If 'limited' is true then very large numeric strings are limited to the maximum value of the numeric type, without an Overflow exception.

Exception: Overflow Exception: InvalidFormat

Definition at line 381 of file gstr.cpp.

References toULong().

Referenced by isUShort().

void G::Str::toUpper ( std::string &  s  )  [static]

Replaces all lowercase characters in string 's' by uppercase characters.

Definition at line 422 of file gstr.cpp.

Referenced by GSmtp::ClientProtocolReply::textContains(), and upper().

void G::Str::toLower ( std::string &  s  )  [static]

Replaces all uppercase characters in string 's' by lowercase characters.

Definition at line 404 of file gstr.cpp.

Referenced by lower().

std::string G::Str::upper ( const std::string &  s  )  [static]

Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters.

Definition at line 426 of file gstr.cpp.

References toUpper().

Referenced by GSmtp::SaslClient::preferred(), GSmtp::NetworkVerifier::verify(), and GSmtp::ExecutableVerifier::verify().

std::string G::Str::lower ( const std::string &  s  )  [static]

Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters.

Definition at line 408 of file gstr.cpp.

References toLower().

Referenced by toBool().

std::string G::Str::toPrintableAscii ( char  c,
char  escape = '\\' 
) [static]

Returns a 7-bit printable representation of the given input character.

Definition at line 503 of file gstr.cpp.

References result.

Referenced by G::hostname().

std::string G::Str::toPrintableAscii ( const std::string &  in,
char  escape = '\\' 
) [static]

Returns a 7-bit printable representation of the given input string.

Definition at line 496 of file gstr.cpp.

References result.

std::string G::Str::printable ( const std::string &  in,
char  escape = '\\' 
) [static]

Returns a printable represention of the given input string.

Typically used to prevent escape sequences getting into log files.

Definition at line 489 of file gstr.cpp.

References result.

Referenced by GSmtp::ServerProtocol::apply(), GSmtp::SaslServer::apply(), GPop::ServerProtocol::apply(), GSmtp::ClientProtocol::apply(), GSmtp::ClientProtocolReply::error(), GSmtp::SpamClient::onReceive(), GSmtp::Client::onReceive(), and GSmtp::RequestClient::onReceive().

void G::Str::escape ( std::string &  s,
const std::string &  specials,
char  escape = '\\' 
) [static]

Prefixes each occurrence of one of the special characters with the escape character.

Definition at line 40 of file gstr.cpp.

Referenced by escaped().

std::string G::Str::escaped ( const std::string &  s_in,
const std::string &  specials,
char  escape = '\\' 
) [static]

Prefixes each occurrence of one of the special characters with the escape character.

Definition at line 33 of file gstr.cpp.

References escape(), and s.

std::string G::Str::readLineFrom ( std::istream &  stream,
const std::string &  eol = std::string() 
) [static]

Reads a line from the stream using the given line terminator.

The line terminator is not part of the returned string. The terminator defaults to the newline.

Note that alternatives in the standard library such as std::istream::getline() or std::getline(stream,string) in <string> are limited to a single character as the terminator.

The stream's fail bit is set if (1) an empty string was returned because the stream was already at eof or (2) the string overflowed. Therefore, ignoring overflow, if the stream ends in an incomplete line that line fragment is returned with the stream's eof flag set but the fail bit reset and the next attempted read will return an empty string with the fail bit set. If the stream ends with a complete line then the last line is returned with eof and fail bits reset and the next attempted read will return an empty string with eof and fail bits set.

If we don't worry too much about the 'bad' state and note that the boolean tests on a stream test its 'fail' flag (not eof) we can use a read loop like "while(s.good()){read(s);if(s)...}".

Definition at line 511 of file gstr.cpp.

References result.

void G::Str::readLineFrom ( std::istream &  stream,
const std::string &  eol,
std::string &  result,
bool  pre_erase_result = true 
) [static]

An overload which avoids string copying.

Definition at line 518 of file gstr.cpp.

References G_ASSERT.

std::string G::Str::wrap ( std::string  text,
const std::string &  prefix_first_line,
const std::string &  prefix_subsequent_lines,
size_type  width = 70U 
) [static]

Does word-wrapping.

The return value is a string with embedded newlines.

Definition at line 608 of file gstr.cpp.

References ws().

Referenced by G::GetOpt::usageSummary().

void G::Str::splitIntoTokens ( const std::string &  in,
Strings out,
const std::string &  ws 
) [static]

Splits the string into 'ws'-delimited tokens.

The behaviour is like strtok() in that adjacent delimiters count as one and leading and trailing delimiters are ignored. Ths output array is cleared first.

Definition at line 689 of file gstr.cpp.

Referenced by GSmtp::SaslServer::apply(), G::Executable::Executable(), and G::Md5::hmac().

void G::Str::splitIntoTokens ( const std::string &  in,
StringArray out,
const std::string &  ws 
) [static]

Overload for vector<string>.

Definition at line 693 of file gstr.cpp.

void G::Str::splitIntoFields ( const std::string &  in,
Strings out,
const std::string &  seperators,
char  escape = '\0',
bool  discard_bogus_escapes = true 
) [static]

Splits the string into fields.

Duplicated, leading and trailing separator characters are all significant. Ths output array is cleared first.

If a non-null escape character is given then any escaped separator is not used for splitting. If the 'discard...' parameter is true then escapes will never appear in the output except for where there were originally double escapes. This is the preferred behaviour but it can create problems if doing nested splitting -- the escapes are lost by the time the sub-strings are split.

Definition at line 740 of file gstr.cpp.

Referenced by GSmtp::VerifierStatus::parse(), GNet::AddressImp::validString(), Main::CommandLine::value(), and GNet::Address::wildcards().

void G::Str::splitIntoFields ( const std::string &  in,
StringArray out,
const std::string &  seperators,
char  escape = '\0',
bool  discard_bogus_escapes = true 
) [static]

Overload for vector<string>.

Definition at line 745 of file gstr.cpp.

std::string G::Str::join ( const Strings strings,
const std::string &  sep 
) [static]

Concatenates a set of strings.

Definition at line 774 of file gstr.cpp.

References first, and result.

Referenced by G::Executable::displayString(), GSmtp::SaslServerImp::mechanisms(), GSmtp::SaslClient::preferred(), and GSmtp::NetworkVerifier::verify().

std::string G::Str::join ( const StringArray strings,
const std::string &  sep 
) [static]

Concatenates a set of strings.

Definition at line 781 of file gstr.cpp.

References first, and result.

G::Strings G::Str::keys ( const StringMap string_map  )  [static]

Extracts the keys from a map of strings.

Definition at line 797 of file gstr.cpp.

References result.

std::string G::Str::head ( const std::string &  in,
std::string::size_type  pos,
const std::string &  default_ = std::string() 
) [static]

Returns the first part of the string up to just before the given position.

The character at pos is not returned. Returns the supplied default if pos is npos.

Definition at line 809 of file gstr.cpp.

Referenced by GSmtp::SaslServer::apply(), GSmtp::NetworkVerifier::verify(), and GSmtp::ExecutableVerifier::verify().

std::string G::Str::tail ( const std::string &  in,
std::string::size_type  pos,
const std::string &  default_ = std::string() 
) [static]

Returns the last part of the string after the given position.

The character at pos is not returned. Returns the supplied default if pos is npos.

Definition at line 817 of file gstr.cpp.

Referenced by GSmtp::SaslServer::apply(), GSmtp::FactoryParser::parse(), GSmtp::NetworkVerifier::verify(), and GSmtp::ExecutableVerifier::verify().

bool G::Str::tailMatch ( const std::string &  in,
const std::string &  ending 
) [static]

Returns true if the given string has the given ending.

Definition at line 825 of file gstr.cpp.

Referenced by G::DirectoryList::readType().

std::string G::Str::ws (  )  [static]

A convenience function returning standard whitespace characters.

Definition at line 804 of file gstr.cpp.

Referenced by wrap().


The documentation for this class was generated from the following files:

Generated on Fri Apr 18 15:56:13 2008 for E-MailRelay by  doxygen 1.5.5