KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
kss::util::strings Namespace Reference

Items related to string handling. More...

Classes

class  SubString
 Write-through substring. More...
 
class  Tokenizer
 Container like access to a string separated by tokens. More...
 

Typedefs

typedef SubString< char,
std::char_traits< char >
, std::allocator< char > > 
substring_t
 
typedef SubString< wchar_t,
std::char_traits< wchar_t >
, std::allocator< wchar_t > > 
wsubstring_t
 

Functions

template<class T >
convert (const std::string &s, const T &=T())
 
template<>
std::string convert (const std::string &s, const std::string &)
 
template<>
float convert (const std::string &s, const float &)
 
template<>
double convert (const std::string &s, const double &)
 
template<>
long double convert (const std::string &s, const long double &)
 
template<>
int convert (const std::string &s, const int &)
 
template<>
long convert (const std::string &s, const long &)
 
template<>
long long convert (const std::string &s, const long long &)
 
template<>
unsigned convert (const std::string &s, const unsigned &)
 
template<>
unsigned long convert (const std::string &s, const unsigned long &)
 
template<>
unsigned long long convert (const std::string &s, const unsigned long long &)
 
template<>
std::chrono::hours convert (const std::string &s, const std::chrono::hours &)
 
template<>
std::chrono::minutes convert (const std::string &s, const std::chrono::minutes &)
 
template<>
std::chrono::seconds convert (const std::string &s, const std::chrono::seconds &)
 
template<>
std::chrono::milliseconds convert (const std::string &s, const std::chrono::milliseconds &)
 
template<>
std::chrono::microseconds convert (const std::string &s, const std::chrono::microseconds &)
 
template<>
std::chrono::nanoseconds convert (const std::string &s, const std::chrono::nanoseconds &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::hours > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::hours > &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::minutes > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::minutes > &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::seconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::seconds > &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::milliseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::milliseconds > &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::microseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::microseconds > &)
 
template<>
std::chrono::time_point
< std::chrono::system_clock,
std::chrono::nanoseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::hours > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::hours > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::minutes > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::minutes > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::seconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::milliseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::microseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > &)
 
template<>
std::chrono::time_point
< std::chrono::steady_clock,
std::chrono::nanoseconds > 
convert (const std::string &s, const std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > &)
 
std::string format (std::string pattern,...)
 
std::string vformat (const std::string &pattern, va_list ap)
 
std::string & ltrim (std::string &s) noexcept
 
std::string & ltrim (std::string &s, char c) noexcept
 
std::string & rtrim (std::string &s) noexcept
 
std::string & rtrim (std::string &s, char c) noexcept
 
std::string & trim (std::string &s) noexcept
 
std::string & trim (std::string &s, char c) noexcept
 
bool startsWith (const std::string &str, const std::string &prefix) noexcept
 
bool endsWith (const std::string &str, const std::string &suffix) noexcept
 
std::string & toUpper (std::string &s) noexcept
 
std::string toUpper (const std::string &s)
 
std::wstring & toUpper (std::wstring &s) noexcept
 
std::wstring toUpper (const std::wstring &s)
 
std::string & toLower (std::string &s) noexcept
 
std::string toLower (const std::string &s)
 
std::wstring & toLower (std::wstring &s) noexcept
 
std::wstring toLower (const std::wstring &s)
 
bool iequal (const std::string &a, const std::string &b) noexcept
 
bool iequal (const std::wstring &a, const std::wstring &b) noexcept
 
int icompare (const std::string &a, const std::string &b) noexcept
 
int icompare (const std::wstring &a, const std::wstring &b) noexcept
 
template<class Char , class traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
unsigned countOccurrencesOf (const std::basic_string< Char, traits, Alloc > &source, const std::basic_string< Char, traits, Alloc > &substr, bool allowOverlaps=false) noexcept
 
template<class Char , class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
unsigned countOccurrencesOf (const std::basic_string< Char, Traits, Alloc > &source, const Char *substr, bool allowOverlaps=false) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator== (const SubString< Char, Traits, Alloc > &lhs, const std::basic_string< Char, Traits, Alloc > &rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator== (const std::basic_string< Char, Traits, Alloc > &lhs, const SubString< Char, Traits, Alloc > &rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator< (const SubString< Char, Traits, Alloc > &lhs, const std::basic_string< Char, Traits, Alloc > &rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator< (const std::basic_string< Char, Traits, Alloc > &lhs, const SubString< Char, Traits, Alloc > &rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator== (const SubString< Char, Traits, Alloc > &lhs, typename SubString< Char, Traits, Alloc >::const_pointer rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator== (typename SubString< Char, Traits, Alloc >::const_pointer lhs, const SubString< Char, Traits, Alloc > &rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator< (const SubString< Char, Traits, Alloc > &lhs, typename SubString< Char, Traits, Alloc >::const_pointer rhs) noexcept
 
template<class Char , class Traits , class Alloc >
bool operator< (typename SubString< Char, Traits, Alloc >::const_pointer lhs, const SubString< Char, Traits, Alloc > &rhs) noexcept
 

Detailed Description

Items related to string handling.

Typedef Documentation

typedef SubString<char, std::char_traits<char>, std::allocator<char> > kss::util::strings::substring_t

Shorthand for a substring of an std::string.

Definition at line 379 of file substring.hpp.

typedef SubString<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > kss::util::strings::wsubstring_t

Shorthand for a substring of an std::wstring.

Definition at line 384 of file substring.hpp.

Function Documentation

template<class T >
T kss::util::strings::convert ( const std::string &  s,
const T &  = T() 
)

Attempt to convert a string to the type T.

Exceptions
std::system_errorif the conversion failed.
std::invalid_argumentif s is empty

Definition at line 37 of file convert.hpp.

template<>
std::string kss::util::strings::convert ( const std::string &  s,
const std::string &   
)
inline

Definition at line 54 of file convert.hpp.

template<>
float kss::util::strings::convert ( const std::string &  s,
const float &   
)
template<>
double kss::util::strings::convert ( const std::string &  s,
const double &   
)
template<>
long double kss::util::strings::convert ( const std::string &  s,
const long double &   
)
template<>
int kss::util::strings::convert ( const std::string &  s,
const int &   
)
template<>
long kss::util::strings::convert ( const std::string &  s,
const long &   
)
template<>
long long kss::util::strings::convert ( const std::string &  s,
const long long &   
)
template<>
unsigned kss::util::strings::convert ( const std::string &  s,
const unsigned &   
)
template<>
unsigned long kss::util::strings::convert ( const std::string &  s,
const unsigned long &   
)
template<>
unsigned long long kss::util::strings::convert ( const std::string &  s,
const unsigned long long &   
)
template<>
std::chrono::hours kss::util::strings::convert ( const std::string &  s,
const std::chrono::hours &   
)
template<>
std::chrono::minutes kss::util::strings::convert ( const std::string &  s,
const std::chrono::minutes &   
)
template<>
std::chrono::seconds kss::util::strings::convert ( const std::string &  s,
const std::chrono::seconds &   
)
template<>
std::chrono::milliseconds kss::util::strings::convert ( const std::string &  s,
const std::chrono::milliseconds &   
)
template<>
std::chrono::microseconds kss::util::strings::convert ( const std::string &  s,
const std::chrono::microseconds &   
)
template<>
std::chrono::nanoseconds kss::util::strings::convert ( const std::string &  s,
const std::chrono::nanoseconds &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::hours> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::hours > &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::minutes > &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::seconds > &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::milliseconds > &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::microseconds > &   
)
template<>
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::hours> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::hours > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::minutes> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::minutes > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > &   
)
template<>
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> kss::util::strings::convert ( const std::string &  s,
const std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > &   
)
template<class Char , class traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
unsigned kss::util::strings::countOccurrencesOf ( const std::basic_string< Char, traits, Alloc > &  source,
const std::basic_string< Char, traits, Alloc > &  substr,
bool  allowOverlaps = false 
)
noexcept

Count the number of times a substring occurs in a source string. This should work on any object that follows the basic_string API.

This is based on code found at: https://stackoverflow.com/questions/6067460/c-how-to-calculate-the-number-time-a-string-occurs-in-a-data

Parameters
sourceThe string being searched.
substrThe sub-string we are looking for.
allowOverlapsIf true then overlapping substrings are counted separately.
Returns
The number of times the substring is found.

Definition at line 102 of file stringutil.hpp.

template<class Char , class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
unsigned kss::util::strings::countOccurrencesOf ( const std::basic_string< Char, Traits, Alloc > &  source,
const Char *  substr,
bool  allowOverlaps = false 
)
inlinenoexcept

Definition at line 120 of file stringutil.hpp.

bool kss::util::strings::endsWith ( const std::string &  str,
const std::string &  suffix 
)
noexcept
std::string kss::util::strings::format ( std::string  pattern,
  ... 
)

Perform printf style formatting and return the resulting string.

int kss::util::strings::icompare ( const std::string &  a,
const std::string &  b 
)
noexcept
int kss::util::strings::icompare ( const std::wstring &  a,
const std::wstring &  b 
)
noexcept
bool kss::util::strings::iequal ( const std::string &  a,
const std::string &  b 
)
noexcept

Case insensitive string comparison.

Returns
true (iequal) if the two string are equal except for case
<0,0,or >0 (icompare) based on the string comparison. (i.e. like strcmp)
bool kss::util::strings::iequal ( const std::wstring &  a,
const std::wstring &  b 
)
noexcept
std::string& kss::util::strings::ltrim ( std::string &  s)
noexcept

Trim whitespace or a specific repeating character from a string. The modified string is also returned. Note that there are versions to trim only the left (l), the right (r) or both.

Returns
the modified string.
std::string& kss::util::strings::ltrim ( std::string &  s,
char  c 
)
noexcept
template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator< ( const SubString< Char, Traits, Alloc > &  lhs,
const std::basic_string< Char, Traits, Alloc > &  rhs 
)
noexcept

Definition at line 290 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator< ( const std::basic_string< Char, Traits, Alloc > &  lhs,
const SubString< Char, Traits, Alloc > &  rhs 
)
noexcept

Definition at line 305 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator< ( const SubString< Char, Traits, Alloc > &  lhs,
typename SubString< Char, Traits, Alloc >::const_pointer  rhs 
)
noexcept

Definition at line 343 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator< ( typename SubString< Char, Traits, Alloc >::const_pointer  lhs,
const SubString< Char, Traits, Alloc > &  rhs 
)
noexcept

Definition at line 360 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator== ( const SubString< Char, Traits, Alloc > &  lhs,
const std::basic_string< Char, Traits, Alloc > &  rhs 
)
noexcept

Compare a substring to a string of equivalent type.

Definition at line 273 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator== ( const std::basic_string< Char, Traits, Alloc > &  lhs,
const SubString< Char, Traits, Alloc > &  rhs 
)
inlinenoexcept

Definition at line 283 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator== ( const SubString< Char, Traits, Alloc > &  lhs,
typename SubString< Char, Traits, Alloc >::const_pointer  rhs 
)
noexcept

Compare a substring to a const pointer of equivalent type. Note that the pointer must be NULL-terminated.

Definition at line 324 of file substring.hpp.

template<class Char , class Traits , class Alloc >
bool kss::util::strings::operator== ( typename SubString< Char, Traits, Alloc >::const_pointer  lhs,
const SubString< Char, Traits, Alloc > &  rhs 
)
noexcept

Definition at line 336 of file substring.hpp.

std::string& kss::util::strings::rtrim ( std::string &  s)
noexcept
std::string& kss::util::strings::rtrim ( std::string &  s,
char  c 
)
noexcept
bool kss::util::strings::startsWith ( const std::string &  str,
const std::string &  prefix 
)
noexcept

Determine if a string starts or ends with another string.

std::string& kss::util::strings::toLower ( std::string &  s)
noexcept
std::string kss::util::strings::toLower ( const std::string &  s)
inline

Definition at line 66 of file stringutil.hpp.

std::wstring& kss::util::strings::toLower ( std::wstring &  s)
noexcept
std::wstring kss::util::strings::toLower ( const std::wstring &  s)
inline

Definition at line 72 of file stringutil.hpp.

std::string& kss::util::strings::toUpper ( std::string &  s)
noexcept

Convert strings to upper or lowercase. This can be done either in place or by creating new strings.

Returns
either a reference to the, now modified, string or a new string.
Exceptions
std::bad_allocif a new string could not be allocated.
std::string kss::util::strings::toUpper ( const std::string &  s)
inline

Definition at line 54 of file stringutil.hpp.

std::wstring& kss::util::strings::toUpper ( std::wstring &  s)
noexcept
std::wstring kss::util::strings::toUpper ( const std::wstring &  s)
inline

Definition at line 60 of file stringutil.hpp.

std::string& kss::util::strings::trim ( std::string &  s)
inlinenoexcept

Definition at line 38 of file stringutil.hpp.

std::string& kss::util::strings::trim ( std::string &  s,
char  c 
)
inlinenoexcept

Definition at line 39 of file stringutil.hpp.

std::string kss::util::strings::vformat ( const std::string &  pattern,
va_list  ap 
)