LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Namespaces | Functions
LogCabin::Core::StringUtil Namespace Reference

Namespaces

namespace  anonymous_namespace{StringUtil.cc}

Functions

std::string format (const char *format,...)))
 A safe version of sprintf.
std::string flags (int value, std::initializer_list< std::pair< int, const char * >> flags)
 Format an ORed group of flags as a string.
bool isPrintable (const char *str)
 Determine whether a null-terminated string is printable.
bool isPrintable (const void *data, size_t length)
 Determine whether some data is a printable, null-terminated string.
std::string join (const std::vector< std::string > &components, const std::string &glue)
void replaceAll (std::string &haystack, const std::string &needle, const std::string &replacement)
 For strings, replace all occurrences of 'needle' in 'haystack' with 'replacement'.
std::vector< std::string > split (const std::string &subject, char delimiter)
 Split a string into multiple components by a character.
bool startsWith (const std::string &haystack, const std::string &needle)
 Return true if haystack begins with needle.
bool endsWith (const std::string &haystack, const std::string &needle)
 Return true if haystack ends with needle.
std::string trim (const std::string &s)
 Return a copy of the given string except with no leading or trailing whitespace.
template<typename T >
std::string toString (const T &t)
 Return a string returned from the given object's stream operator.

Function Documentation

std::string LogCabin::Core::StringUtil::format ( const char *  format,
  ... 
)

A safe version of sprintf.

Definition at line 70 of file StringUtil.cc.

std::string LogCabin::Core::StringUtil::flags ( int  value,
std::initializer_list< std::pair< int, const char * >>  flags 
)

Format an ORed group of flags as a string.

Parameters:
valueThe ORed options.
flagsMaps option identifiers to their string names, such as {{FOO, "FOO"}, {BAR, "BAR"}}.
Returns:
String such as "FOO|BAR".

Definition at line 51 of file StringUtil.cc.

bool LogCabin::Core::StringUtil::isPrintable ( const char *  str)

Determine whether a null-terminated string is printable.

Parameters:
strA null-terminated string.
Returns:
True if all the bytes of str before its null terminator are nice to display in a single line of text.

Definition at line 100 of file StringUtil.cc.

bool LogCabin::Core::StringUtil::isPrintable ( const void *  data,
size_t  length 
)

Determine whether some data is a printable, null-terminated string.

Parameters:
dataThe first byte.
lengthThe number of bytes of 'data'.
Returns:
True if the last byte of data is a null terminator and all the bytes of data before that are nice to display in a single line of text.

Definition at line 106 of file StringUtil.cc.

std::string LogCabin::Core::StringUtil::join ( const std::vector< std::string > &  components,
const std::string &  glue 
)

Definition at line 116 of file StringUtil.cc.

void LogCabin::Core::StringUtil::replaceAll ( std::string &  haystack,
const std::string &  needle,
const std::string &  replacement 
)

For strings, replace all occurrences of 'needle' in 'haystack' with 'replacement'.

If this isn't what you're looking for, the standard algorithm std::replace might help you.

Definition at line 128 of file StringUtil.cc.

std::vector< std::string > LogCabin::Core::StringUtil::split ( const std::string &  subject,
char  delimiter 
)

Split a string into multiple components by a character.

Parameters:
subjectThe string to split.
delimiterThe character to split the string by.
Returns:
The components of 'subject', not including 'delimiter'.
  • If two delimiters occur in a row in 'subject', a corresponding empty string will appear in the returned vector.
  • If a delimiter occurs at the start of 'subject', a corresponding empty string will appear at the start of the returned vector.
  • If a delimiter occurs at the end of 'subject', no corresponding empty string will appear at the end of the returned vector.

Definition at line 143 of file StringUtil.cc.

bool LogCabin::Core::StringUtil::startsWith ( const std::string &  haystack,
const std::string &  needle 
)

Return true if haystack begins with needle.

Definition at line 154 of file StringUtil.cc.

bool LogCabin::Core::StringUtil::endsWith ( const std::string &  haystack,
const std::string &  needle 
)

Return true if haystack ends with needle.

Definition at line 160 of file StringUtil.cc.

std::string LogCabin::Core::StringUtil::trim ( const std::string &  original)

Return a copy of the given string except with no leading or trailing whitespace.

Definition at line 169 of file StringUtil.cc.

template<typename T >
std::string LogCabin::Core::StringUtil::toString ( const T &  t)

Return a string returned from the given object's stream operator.

This is useful when you're dealing with strings, but the object you want to print only has a stream operator.

Definition at line 117 of file StringUtil.h.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines