0.2.0
Loading...
Searching...
No Matches
StringUtil.hpp File Reference

Utility functions for working with std::strings. More...

Go to the source code of this file.

Concepts

concept  NAV::str::StdString
 Concept limiting the type to std::string and std::wstring, but also allowing convertible types like const char*.
 

Enumerations

enum  NAV::str::CaseSensitivity {
  NAV::str::RespectCase ,
  NAV::str::IgnoreCase
}
 Enum for case sensitive tasks. More...
 

Functions

static void NAV::str::ltrim (std::string &s)
 Trim from start (in place)
 
static void NAV::str::ltrim (std::string_view &sv)
 Trim from start (in place)
 
static std::string NAV::str::ltrim_copy (std::string s)
 Trim from start (copying)
 
static std::string_view NAV::str::ltrim_copy (std::string_view sv)
 Trim from start (copying)
 
static bool NAV::str::replace (std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
 Replaces the first occurrence of a search pattern with another sequence.
 
static void NAV::str::replaceAll (std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
 Replaces all occurrence of a search pattern with another sequence.
 
static std::string NAV::str::replaceAll_copy (std::string str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
 Replaces all occurrence of a search pattern with another sequence.
 
static void NAV::str::rtrim (std::string &s)
 Trim from end (in place)
 
static void NAV::str::rtrim (std::string_view &sv)
 Trim from end (in place)
 
static std::string NAV::str::rtrim_copy (std::string s)
 Trim from end (copying)
 
static std::string_view NAV::str::rtrim_copy (std::string_view sv)
 Trim from end (copying)
 
static std::vector< std::string > NAV::str::split (const std::string &str, char delimiter)
 Splits a string into parts at a delimiter.
 
static std::vector< std::string > NAV::str::split (const std::string &str, const std::string &delimiter)
 Splits a string into parts at a delimiter.
 
static std::vector< std::string > NAV::str::split_wo_empty (const std::string &str, char delimiter)
 Splits a string into parts at a delimiter and removes empty entries.
 
static std::vector< std::string > NAV::str::split_wo_empty (const std::string &str, const std::string &delimiter)
 Splits a string into parts at a delimiter and removes empty entries.
 
template<StdString String>
double NAV::str::stod (const String &str, double default_value, std::size_t *pos=nullptr) noexcept
 Interprets a value in the string str.
 
template<StdString String>
float NAV::str::stof (const String &str, float default_value, std::size_t *pos=nullptr) noexcept
 Interprets a value in the string str.
 
template<StdString String>
int NAV::str::stoi (const String &str, int default_value, std::size_t *pos=nullptr, int base=10) noexcept
 Interprets a value in the string str.
 
template<StdString String>
int64_t NAV::str::stol (const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
 Interprets a value in the string str.
 
template<StdString String>
long double NAV::str::stold (const String &str, long double default_value, std::size_t *pos=nullptr) noexcept
 Interprets a value in the string str.
 
template<StdString String>
int64_t NAV::str::stoll (const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
 Interprets a value in the string str.
 
static void NAV::str::trim (std::string &s)
 Trim from both ends (in place)
 
static void NAV::str::trim (std::string_view &sv)
 Trim from both ends (in place)
 
static std::string NAV::str::trim_copy (std::string s)
 Trim from both ends (copying)
 
static std::string_view NAV::str::trim_copy (std::string_view sv)
 Trim from both ends (copying)
 

Detailed Description

Utility functions for working with std::strings.

Author
T. Topp (topp@.nosp@m.ins..nosp@m.uni-s.nosp@m.tutt.nosp@m.gart..nosp@m.de)
Date
2020-09-16

Enumeration Type Documentation

◆ CaseSensitivity

Enum for case sensitive tasks.

Enumerator
RespectCase 

Respect the case.

IgnoreCase 

Ignore case.

Function Documentation

◆ ltrim() [1/2]

static void NAV::str::ltrim ( std::string & s)
inlinestatic

Trim from start (in place)

Parameters
[in,out]sThe string to trim

◆ ltrim() [2/2]

static void NAV::str::ltrim ( std::string_view & sv)
inlinestatic

Trim from start (in place)

Parameters
[in,out]svThe string view to trim

◆ ltrim_copy() [1/2]

static std::string NAV::str::ltrim_copy ( std::string s)
inlinestatic

Trim from start (copying)

Parameters
[in]sThe string to trim
Returns
The trimmed string

◆ ltrim_copy() [2/2]

static std::string_view NAV::str::ltrim_copy ( std::string_view sv)
inlinestatic

Trim from start (copying)

Parameters
[in]svThe string view to trim
Returns
The trimmed string

◆ replace()

static bool NAV::str::replace ( std::string & str,
const std::string & from,
const std::string & to,
CaseSensitivity cs = RespectCase )
inlinestatic

Replaces the first occurrence of a search pattern with another sequence.

Parameters
[in,out]strString to search in and return value
[in]fromString pattern to search for
[in]toReplacement string
[in]csCase sensitivity
Returns
True if something was replaced

◆ replaceAll()

static void NAV::str::replaceAll ( std::string & str,
const std::string & from,
const std::string & to,
CaseSensitivity cs = RespectCase )
inlinestatic

Replaces all occurrence of a search pattern with another sequence.

Parameters
[in,out]strString to search in and return value
[in]fromString pattern to search for
[in]toReplacement string
[in]csCase sensitivity

◆ replaceAll_copy()

static std::string NAV::str::replaceAll_copy ( std::string str,
const std::string & from,
const std::string & to,
CaseSensitivity cs = RespectCase )
inlinestatic

Replaces all occurrence of a search pattern with another sequence.

Parameters
[in,out]strString to search in and return value
[in]fromString pattern to search for
[in]toReplacement string
[in]csCase sensitivity
Returns
The string with the replacements

◆ rtrim() [1/2]

static void NAV::str::rtrim ( std::string & s)
inlinestatic

Trim from end (in place)

Parameters
[in,out]sThe string to trim

◆ rtrim() [2/2]

static void NAV::str::rtrim ( std::string_view & sv)
inlinestatic

Trim from end (in place)

Parameters
[in,out]svThe string view to trim

◆ rtrim_copy() [1/2]

static std::string NAV::str::rtrim_copy ( std::string s)
inlinestatic

Trim from end (copying)

Parameters
[in]sThe string to trim
Returns
The trimmed string

◆ rtrim_copy() [2/2]

static std::string_view NAV::str::rtrim_copy ( std::string_view sv)
inlinestatic

Trim from end (copying)

Parameters
[in]svThe string view to trim
Returns
The trimmed string

◆ split() [1/2]

static std::vector< std::string > NAV::str::split ( const std::string & str,
char delimiter )
inlinestatic

Splits a string into parts at a delimiter.

Parameters
[in]strString to split
[in]delimiterCharacter to split at
Returns
List with splitted parts

◆ split() [2/2]

static std::vector< std::string > NAV::str::split ( const std::string & str,
const std::string & delimiter )
inlinestatic

Splits a string into parts at a delimiter.

Parameters
[in]strString to split
[in]delimiterSequence of characters to split at
Returns
List with splitted parts

◆ split_wo_empty() [1/2]

static std::vector< std::string > NAV::str::split_wo_empty ( const std::string & str,
char delimiter )
inlinestatic

Splits a string into parts at a delimiter and removes empty entries.

Parameters
[in]strString to split
[in]delimiterCharacter to split at
Returns
List with splitted parts

◆ split_wo_empty() [2/2]

static std::vector< std::string > NAV::str::split_wo_empty ( const std::string & str,
const std::string & delimiter )
inlinestatic

Splits a string into parts at a delimiter and removes empty entries.

Parameters
[in]strString to split
[in]delimiterSequence of characters to split at
Returns
List with splitted parts

◆ stod()

template<StdString String>
double NAV::str::stod ( const String & str,
double default_value,
std::size_t * pos = nullptr )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
Returns
Value corresponding to the content of str

◆ stof()

template<StdString String>
float NAV::str::stof ( const String & str,
float default_value,
std::size_t * pos = nullptr )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
Returns
Value corresponding to the content of str

◆ stoi()

template<StdString String>
int NAV::str::stoi ( const String & str,
int default_value,
std::size_t * pos = nullptr,
int base = 10 )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
basethe number base
Returns
Value corresponding to the content of str

◆ stol()

template<StdString String>
int64_t NAV::str::stol ( const String & str,
int64_t default_value,
std::size_t * pos = nullptr,
int base = 10 )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
basethe number base
Returns
Value corresponding to the content of str

◆ stold()

template<StdString String>
long double NAV::str::stold ( const String & str,
long double default_value,
std::size_t * pos = nullptr )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
Returns
Value corresponding to the content of str

◆ stoll()

template<StdString String>
int64_t NAV::str::stoll ( const String & str,
int64_t default_value,
std::size_t * pos = nullptr,
int base = 10 )
noexcept

Interprets a value in the string str.

Template Parameters
Stringstd::string or std::wstring and also allowing convertible types like const char*
Parameters
strthe string to convert
default_valuedefault value to take if an invalid argument is given
posaddress of an integer to store the number of characters processed
basethe number base
Returns
Value corresponding to the content of str

◆ trim() [1/2]

static void NAV::str::trim ( std::string & s)
inlinestatic

Trim from both ends (in place)

Parameters
[in,out]sThe string to trim

◆ trim() [2/2]

static void NAV::str::trim ( std::string_view & sv)
inlinestatic

Trim from both ends (in place)

Parameters
[in,out]svThe string view to trim

◆ trim_copy() [1/2]

static std::string NAV::str::trim_copy ( std::string s)
inlinestatic

Trim from both ends (copying)

Parameters
[in]sThe string to trim
Returns
The trimmed string

◆ trim_copy() [2/2]

static std::string_view NAV::str::trim_copy ( std::string_view sv)
inlinestatic

Trim from both ends (copying)

Parameters
[in]svThe string view to trim
Returns
The trimmed string