Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
ph::string_utils Namespace Reference

Contains various string manipulation helpers. More...

Namespaces

namespace  detail_from_to_char
 
namespace  table
 

Concepts

concept  CHasToString
 

Enumerations

enum class  EWhitespace { Common , Standard }
 

Functions

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view get_whitespaces ()
 
template<EWhitespace TYPE = EWhitespace::Common>
constexpr bool is_whitespace (const char ch)
 
bool has_any_of (const std::string_view srcStr, const std::string_view candidates)
 
bool has_none_of (const std::string_view srcStr, const std::string_view candidates)
 
std::string_view cut_head (const std::string_view srcStr, const std::string_view candidates)
 Remove characters from the beginning.
 
std::string_view cut_tail (const std::string_view srcStr, const std::string_view candidates)
 Remove characters from the end.
 
std::string_view cut_ends (const std::string_view srcStr, const std::string_view candidates)
 Remove characters from both ends.
 
template<EWhitespace TYPE = EWhitespace::Common>
std::string_view trim_head (const std::string_view srcStr)
 Remove white spaces from the beginning.
 
template<EWhitespace TYPE = EWhitespace::Common>
std::string_view trim_tail (const std::string_view srcStr)
 Remove white spaces from the end.
 
template<EWhitespace TYPE = EWhitespace::Common>
std::string_view trim (const std::string_view srcStr)
 Remove white spaces from both ends.
 
std::string_view next_token (std::string_view srcStr, std::string_view *const out_remainingStr=nullptr, const std::string_view tokenSeparators=get_whitespaces<>())
 Retrieve a token from a string.
 
char az_to_AZ (const char ch)
 Convert lower-case characters to upper-case.
 
char AZ_to_az (const char ch)
 Convert upper-case characters to lower-case.
 
void az_to_AZ (std::string &str)
 Convert lower-case characters to upper-case.
 
void AZ_to_az (std::string &str)
 Convert upper-case characters to lower-case.
 
std::string repeat (const std::string_view str, const std::size_t n)
 Repeat the input string for N times.
 
void erase_all (std::string &str, const char ch)
 Remove all occurrence of a character in the string.
 
template<typename T >
parse_float (const std::string_view floatStr)
 Returns a float by processing its string representation. Supports float, double, and long double.
 
template<typename T >
parse_int (std::string_view intStr)
 Returns an integer by processing its string representation. Supports the following:
 
template<typename NumberType >
NumberType parse_number (const std::string_view numberStr)
 Returns a number by processing its string representation. Accepts all types supported by parse_float(std::string_view) and parse_int(std::string_view).
 
template<typename T >
std::size_t stringify_float (const T value, char *const out_buffer, const std::size_t bufferSize)
 Converts a float to string.
 
template<std::integral T>
std::size_t stringify_int_alphabetic (const T value, char *const out_buffer, const std::size_t bufferSize, const int base)
 Converts an integer to base [2, 62] string.
 
template<std::integral T>
std::size_t stringify_int (const T value, char *const out_buffer, const std::size_t bufferSize, const int base=10)
 Converts an integer to string.
 
template<typename NumberType >
std::size_t stringify_number (const NumberType value, char *const out_buffer, const std::size_t bufferSize)
 Converts a number to string. Accepts all types supported by stringify_float(T, char*, std::size_t) and stringify_int(T, char*, std::size_t). The written string is not null terminated.
 
template<typename NumberType >
std::string & stringify_number (const NumberType value, std::string &out_str, const std::size_t maxChars=64)
 Converts a number to string. Similar to stringify_number(NumberType, char*, std::size_t), except that this variant writes to std::string and the resulting string is guaranteed to be null terminated (by calling std::string::c_str()).
 
template<typename NumberType >
std::string stringify_number (const NumberType value, const std::size_t maxChars=64)
 Converts a number to string. Similar to stringify_number(NumberType, std::string&, std::size_t), except that this variant creates a new string.
 

Detailed Description

Contains various string manipulation helpers.

Enumeration Type Documentation

◆ EWhitespace

enum class ph::string_utils::EWhitespace
strong
Enumerator
Common 

Smaller set of whitespace characters that are often seen (see table::common_whitespaces).

Standard 

Complete set of whitespace characters (see table::standard_whitespaces).

Function Documentation

◆ AZ_to_az() [1/2]

char ph::string_utils::AZ_to_az ( const char ch)
inline

Convert upper-case characters to lower-case.

Characters that are not English alphabets, or being lower-case already, will be preserved.

◆ AZ_to_az() [2/2]

void ph::string_utils::AZ_to_az ( std::string & str)
inline

Convert upper-case characters to lower-case.

Characters that are not English alphabets, or being lower-case already, will be preserved.

Parameters
[in,out]strString that is going to be converted in-place.

◆ az_to_AZ() [1/2]

char ph::string_utils::az_to_AZ ( const char ch)
inline

Convert lower-case characters to upper-case.

Characters that are not English alphabets, or being upper-case already, will be preserved.

◆ az_to_AZ() [2/2]

void ph::string_utils::az_to_AZ ( std::string & str)
inline

Convert lower-case characters to upper-case.

Characters that are not English alphabets, or being upper-case already, will be preserved.

Parameters
[in,out]strString that is going to be converted in-place.

◆ cut_ends()

std::string_view ph::string_utils::cut_ends ( const std::string_view srcStr,
const std::string_view candidates )
inline

Remove characters from both ends.

Characters in srcStr will be removed from both ends if they match any of the character in candidates. The process stops once a mismatch is encountered.

Parameters
srcStrString that is going to be cut.
candidatesThe character set used to remove characters from srcStr.
Returns
The cut string.

◆ cut_head()

std::string_view ph::string_utils::cut_head ( const std::string_view srcStr,
const std::string_view candidates )
inline

Remove characters from the beginning.

Characters in srcStr will be removed from the beginning if they match any of the character in candidates. The process stops once a mismatch is encountered.

Parameters
srcStrString that is going to be cut.
candidatesThe character set used to remove characters from srcStr.
Returns
The cut string.

◆ cut_tail()

std::string_view ph::string_utils::cut_tail ( const std::string_view srcStr,
const std::string_view candidates )
inline

Remove characters from the end.

Characters in srcStr will be removed from the end if they match any of the character in candidates. The process stops once a mismatch is encountered.

Parameters
srcStrString that is going to be cut.
candidatesThe character set used to remove characters from srcStr.
Returns
The cut string.

◆ erase_all()

void ph::string_utils::erase_all ( std::string & str,
const char ch )
inline

Remove all occurrence of a character in the string.

◆ get_whitespaces()

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::get_whitespaces ( )
inline

◆ has_any_of()

bool ph::string_utils::has_any_of ( const std::string_view srcStr,
const std::string_view candidates )
inline

◆ has_none_of()

bool ph::string_utils::has_none_of ( const std::string_view srcStr,
const std::string_view candidates )
inline

◆ is_whitespace()

template<EWhitespace TYPE = EWhitespace::Common>
bool ph::string_utils::is_whitespace ( const char ch)
inlineconstexpr

◆ next_token()

std::string_view ph::string_utils::next_token ( std::string_view srcStr,
std::string_view *const out_remainingStr = nullptr,
const std::string_view tokenSeparators = get_whitespaces<>() )
inline

Retrieve a token from a string.

Parameters
srcStrThe string that token is going to be retrieved from.
[out]out_remainingStrIf not null, stores the string with the retrieved token and its separator removed. Pointing to srcStr is valid, e.g., next_token(str, &str).
tokenSeparatorsCharactors that separate the tokens. Defaults to whitespace characters.

◆ parse_float()

template<typename T >
T ph::string_utils::parse_float ( const std::string_view floatStr)
inline

Returns a float by processing its string representation. Supports float, double, and long double.

◆ parse_int()

template<typename T >
T ph::string_utils::parse_int ( std::string_view intStr)
inline

Returns an integer by processing its string representation. Supports the following:

  1. Supports all signed and unsigned standard integer types (including bool).
  2. Supports both base 10 (no prefix) and base 16 (0x prefix) inputs.

◆ parse_number()

template<typename NumberType >
NumberType ph::string_utils::parse_number ( const std::string_view numberStr)
inline

Returns a number by processing its string representation. Accepts all types supported by parse_float(std::string_view) and parse_int(std::string_view).

◆ repeat()

std::string ph::string_utils::repeat ( const std::string_view str,
const std::size_t n )
inline

Repeat the input string for N times.

◆ stringify_float()

template<typename T >
std::size_t ph::string_utils::stringify_float ( const T value,
char *const out_buffer,
const std::size_t bufferSize )
inline

Converts a float to string.

Supports all built-in floating point types (e.g., float, double, and long double). The function expects a large enough bufferSize determined by the caller. The written string is not null terminated. By default, the stringified float guarantees round-trip conversion–feeding the converted string s from value to parse_float() will result in the same value.

Parameters
out_bufferThe buffer for storing the string.
bufferSizeSize of out_buffer.
Returns
Number of characters written to out_buffer.
Note
No dynamic memory allocation is performed.

◆ stringify_int()

template<std::integral T>
std::size_t ph::string_utils::stringify_int ( const T value,
char *const out_buffer,
const std::size_t bufferSize,
const int base = 10 )
inline

Converts an integer to string.

Supports all signed and unsigned standard integer types (including bool). The function expects a large enough bufferSize determined by the caller. The written string is not null terminated.

Parameters
out_bufferThe buffer for storing the string.
bufferSizeSize of out_buffer.
Returns
Number of characters written to out_buffer.
Note
No dynamic memory allocation is performed.

◆ stringify_int_alphabetic()

template<std::integral T>
std::size_t ph::string_utils::stringify_int_alphabetic ( const T value,
char *const out_buffer,
const std::size_t bufferSize,
const int base )
inline

Converts an integer to base [2, 62] string.

Supports all signed and unsigned standard integer types (including bool). The function expects a large enough bufferSize determined by the caller. The written string is not null terminated.

Parameters
out_bufferThe buffer for storing the string.
bufferSizeSize of out_buffer.
Returns
Number of characters written to out_buffer.
Note
No dynamic memory allocation is performed.

◆ stringify_number() [1/3]

template<typename NumberType >
std::size_t ph::string_utils::stringify_number ( const NumberType value,
char *const out_buffer,
const std::size_t bufferSize )
inline

Converts a number to string. Accepts all types supported by stringify_float(T, char*, std::size_t) and stringify_int(T, char*, std::size_t). The written string is not null terminated.

Returns
Number of characters written to out_buffer.

◆ stringify_number() [2/3]

template<typename NumberType >
std::string ph::string_utils::stringify_number ( const NumberType value,
const std::size_t maxChars = 64 )
inline

Converts a number to string. Similar to stringify_number(NumberType, std::string&, std::size_t), except that this variant creates a new string.

Parameters
out_strThe string to append the result to.
Returns
A new string that stores the number.

◆ stringify_number() [3/3]

template<typename NumberType >
std::string & ph::string_utils::stringify_number ( const NumberType value,
std::string & out_str,
const std::size_t maxChars = 64 )
inline

Converts a number to string. Similar to stringify_number(NumberType, char*, std::size_t), except that this variant writes to std::string and the resulting string is guaranteed to be null terminated (by calling std::string::c_str()).

Parameters
out_strThe string to append the result to.
Returns
out_str for convenience.

◆ trim()

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::trim ( const std::string_view srcStr)
inline

Remove white spaces from both ends.

Parameters
srcStrString that is going to be trimmed.
Returns
The trimmed string.

◆ trim_head()

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::trim_head ( const std::string_view srcStr)
inline

Remove white spaces from the beginning.

Parameters
srcStrString that is going to be trimmed.
Returns
The trimmed string.

◆ trim_tail()

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::trim_tail ( const std::string_view srcStr)
inline

Remove white spaces from the end.

Parameters
srcStrString that is going to be trimmed.
Returns
The trimmed string.