KSSUtil  v14.2.0
C++ general utility library
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

◆ substring_t

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 374 of file substring.hpp.

◆ wsubstring_t

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 379 of file substring.hpp.

Function Documentation

◆ convert() [1/29]

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

◆ convert() [2/29]

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

◆ convert() [3/29]

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

◆ convert() [4/29]

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

◆ convert() [5/29]

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

◆ convert() [6/29]

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

◆ convert() [7/29]

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

◆ convert() [8/29]

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

◆ convert() [9/29]

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

◆ convert() [10/29]

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

◆ convert() [11/29]

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

◆ convert() [12/29]

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

◆ convert() [13/29]

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 > &   
)

◆ convert() [14/29]

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 > &   
)

◆ convert() [15/29]

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 > &   
)

◆ convert() [16/29]

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 > &   
)

◆ convert() [17/29]

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 > &   
)

◆ convert() [18/29]

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 > &   
)

◆ convert() [19/29]

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 > &   
)

◆ convert() [20/29]

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 > &   
)

◆ convert() [21/29]

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 > &   
)

◆ convert() [22/29]

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 > &   
)

◆ convert() [23/29]

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 > &   
)

◆ convert() [24/29]

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 > &   
)

◆ convert() [25/29]

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

Definition at line 55 of file convert.hpp.

◆ convert() [26/29]

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 38 of file convert.hpp.

◆ convert() [27/29]

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

◆ convert() [28/29]

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

◆ convert() [29/29]

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

◆ countOccurrencesOf() [1/2]

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 122 of file stringutil.hpp.

◆ countOccurrencesOf() [2/2]

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 104 of file stringutil.hpp.

◆ endsWith()

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

◆ format()

std::string kss::util::strings::format ( std::string  pattern,
  ... 
)

Perform printf style formatting and return the resulting string.

Exceptions
std::invalid_argumentif the pattern is empty
std::system_errorif there is a problem with an underlying C call

◆ icompare() [1/2]

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

◆ icompare() [2/2]

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

◆ iequal() [1/2]

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)

◆ iequal() [2/2]

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

◆ ltrim() [1/2]

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.

◆ ltrim() [2/2]

std::string& kss::util::strings::ltrim ( std::string &  s,
char  c 
)
noexcept

◆ operator<() [1/4]

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 300 of file substring.hpp.

◆ operator<() [2/4]

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 285 of file substring.hpp.

◆ operator<() [3/4]

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 338 of file substring.hpp.

◆ operator<() [4/4]

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 355 of file substring.hpp.

◆ operator==() [1/4]

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 278 of file substring.hpp.

◆ operator==() [2/4]

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 268 of file substring.hpp.

◆ operator==() [3/4]

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 319 of file substring.hpp.

◆ operator==() [4/4]

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 331 of file substring.hpp.

◆ rtrim() [1/2]

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

◆ rtrim() [2/2]

std::string& kss::util::strings::rtrim ( std::string &  s,
char  c 
)
noexcept

◆ startsWith()

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

Determine if a string starts or ends with another string.

◆ toLower() [1/4]

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

Definition at line 68 of file stringutil.hpp.

◆ toLower() [2/4]

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

Definition at line 74 of file stringutil.hpp.

◆ toLower() [3/4]

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

◆ toLower() [4/4]

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

◆ toUpper() [1/4]

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

Definition at line 56 of file stringutil.hpp.

◆ toUpper() [2/4]

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

Definition at line 62 of file stringutil.hpp.

◆ toUpper() [3/4]

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.

◆ toUpper() [4/4]

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

◆ trim() [1/2]

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

Definition at line 40 of file stringutil.hpp.

◆ trim() [2/2]

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

Definition at line 41 of file stringutil.hpp.

◆ vformat()

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