Public Types | Static Public Member Functions | List of all members
G::Str Class Reference

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

#include <gstr.h>

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'. More...
 
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'. More...
 
static unsigned int replaceAll (std::string &s, const char *from, const char *to)
 A c-string overload, provided for performance reasons. More...
 
static void removeAll (std::string &, char)
 Removes all occurrences of the character from the string. More...
 
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. More...
 
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. More...
 
static void trim (std::string &s, const std::string &ws)
 Trims both ends of s, taking off any of the 'ws' characters. More...
 
static std::string trimmed (const std::string &s, const std::string &ws)
 Returns a trim()med version of s. More...
 
static bool isNumeric (const std::string &s, bool allow_minus_sign=false)
 Returns true if every character is a decimal digit. More...
 
static bool isPrintableAscii (const std::string &s)
 Returns true if every character is a 7-bit, non-control character (ie. More...
 
static bool isUShort (const std::string &s)
 Returns true if the string can be converted into an unsigned short without throwing an exception. More...
 
static bool isUInt (const std::string &s)
 Returns true if the string can be converted into an unsigned integer without throwing an exception. More...
 
static bool isULong (const std::string &s)
 Returns true if the string can be converted into an unsigned long without throwing an exception. More...
 
static std::string fromBool (bool b)
 Converts boolean 'b' to a string. More...
 
static std::string fromDouble (double d)
 Converts double 'd' to a string. More...
 
static std::string fromInt (int i)
 Converts int 'i' to a string. More...
 
static std::string fromLong (long l)
 Converts long 'l' to a string. More...
 
static std::string fromShort (short s)
 Converts short 's' to a string. More...
 
static std::string fromUInt (unsigned int ui)
 Converts unsigned int 'ui' to a string. More...
 
static std::string fromULong (unsigned long ul)
 Converts unsigned long 'ul' to a string. More...
 
static std::string fromUShort (unsigned short us)
 Converts unsigned short 'us' to a string. More...
 
static bool toBool (const std::string &s)
 Converts string 's' to a bool. More...
 
static double toDouble (const std::string &s)
 Converts string 's' to a double. More...
 
static int toInt (const std::string &s)
 Converts string 's' to an int. More...
 
static long toLong (const std::string &s)
 Converts string 's' to a long. More...
 
static short toShort (const std::string &s)
 Converts string 's' to a short. More...
 
static unsigned int toUInt (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned int. More...
 
static unsigned long toULong (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned long. More...
 
static unsigned short toUShort (const std::string &s, bool limited=false)
 Converts string 's' to an unsigned short. More...
 
static void toUpper (std::string &s)
 Replaces all lowercase characters in string 's' by uppercase characters. More...
 
static void toLower (std::string &s)
 Replaces all uppercase characters in string 's' by lowercase characters. More...
 
static std::string upper (const std::string &s)
 Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters. More...
 
static std::string lower (const std::string &s)
 Returns a copy of 's' in which all uppercase characters have been replaced by lowercase characters. More...
 
static std::string toPrintableAscii (char c, char escape= '\\')
 Returns a 7-bit printable representation of the given input character. More...
 
static std::string toPrintableAscii (const std::string &in, char escape= '\\')
 Returns a 7-bit printable representation of the given input string. More...
 
static std::string toPrintableAscii (const std::wstring &in, wchar_t escape=L'\\')
 Returns a 7-bit printable representation of the given wide input string. More...
 
static std::string printable (const std::string &in, char escape= '\\')
 Returns a printable represention of the given input string. More...
 
static void escape (std::string &, const std::string &specials, char escape= '\\')
 Prefixes each occurrence of one of the special characters with the escape character. More...
 
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. More...
 
static std::string readLineFrom (std::istream &stream, const std::string &eol=std::string())
 Reads a line from the stream using the given line terminator. More...
 
static void readLineFrom (std::istream &stream, const std::string &eol, std::string &result, bool pre_erase_result=true)
 An overload which avoids string copying. More...
 
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. More...
 
static void splitIntoTokens (const std::string &in, Strings &out, const std::string &ws)
 Splits the string into 'ws'-delimited tokens. More...
 
static void splitIntoTokens (const std::string &in, StringArray &out, const std::string &ws)
 Overload for vector<string>. More...
 
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. More...
 
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>. More...
 
static std::string join (const Strings &strings, const std::string &sep)
 Concatenates a set of strings. More...
 
static std::string join (const StringArray &strings, const std::string &sep)
 Concatenates a set of strings. More...
 
static Strings keys (const StringMap &string_map)
 Extracts the keys from a map of strings. More...
 
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. More...
 
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. More...
 
static bool tailMatch (const std::string &in, const std::string &ending)
 Returns true if the given string has the given ending. More...
 
static std::string ws ()
 A convenience function returning standard whitespace characters. More...
 

Detailed Description

A static class which provides string helper functions.

Definition at line 42 of file gstr.h.

Member Typedef Documentation

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

Definition at line 45 of file gstr.h.

Member Function Documentation

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().

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.

Referenced by G::Pam::Error::Error(), and GNet::MonitorImp::findCertificate().

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.

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.

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 834 of file gstr.cpp.

Referenced by GAuth::SaslServerBasic::apply(), GAuth::SaslServerPamImp::apply(), Main::Configuration::listeningInterfaces(), GSmtp::ExecutableVerifier::verify(), and GSmtp::NetworkVerifier::verify().

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

Referenced by GSmtp::FactoryParser::check(), GNet::Resolver::resolve(), and GNet::AddressImp::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.

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.

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

Concatenates a set of strings.

Definition at line 806 of file gstr.cpp.

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

Extracts the keys from a map of strings.

Definition at line 822 of file gstr.cpp.

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.

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 507 of file gstr.cpp.

Referenced by GAuth::SaslServerBasic::apply(), GPop::ServerProtocol::apply(), GSmtp::ServerProtocol::apply(), GSmtp::ClientProtocol::apply(), GSmtp::ClientProtocolReply::error(), GSmtp::RequestClient::onReceive(), GSmtp::SpamClient::onReceive(), GSmtp::Client::onReceive(), and GSsl::Certificate::str().

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 the standard "string" header 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 536 of file gstr.cpp.

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 543 of file gstr.cpp.

References G_ASSERT.

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.

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.

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.

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

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.

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 765 of file gstr.cpp.

Referenced by GSmtp::VerifierStatus::parse(), GNet::AddressImp::validString(), Main::CommandLine::value(), and GNet::AddressImp::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 770 of file gstr.cpp.

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 714 of file gstr.cpp.

Referenced by GAuth::SaslServerBasic::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 718 of file gstr.cpp.

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 842 of file gstr.cpp.

Referenced by GAuth::SaslServerBasic::apply(), GAuth::SaslServerPamImp::apply(), Main::Configuration::clientInterface(), Main::Configuration::listeningInterfaces(), GSmtp::FactoryParser::parse(), GSmtp::ExecutableVerifier::verify(), and GSmtp::NetworkVerifier::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 850 of file gstr.cpp.

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

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.

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.

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.

Referenced by GSmtp::ClientProtocolReply::ClientProtocolReply().

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.

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.

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

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

Definition at line 521 of file gstr.cpp.

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 514 of file gstr.cpp.

std::string G::Str::toPrintableAscii ( const std::wstring &  in,
wchar_t  escape = L'\\' 
)
static

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

Definition at line 528 of file gstr.cpp.

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.

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.

Referenced by GSmtp::ProcessorFactory::newProcessor(), GNet::Resolver::resolve(), Main::CommandLine::value(), GNet::AddressImp::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.

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().

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.

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.

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

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().

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.

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.

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.

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

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 633 of file gstr.cpp.

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

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

A convenience function returning standard whitespace characters.

Definition at line 829 of file gstr.cpp.


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