Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
string_utils.h File Reference

String manipulation helpers. More...

#include "Common/Utility/string_utils_table.h"
#include "Common/assertion.h"
#include "Common/exceptions.h"
#include <cstddef>
#include <string>
#include <algorithm>
#include <string_view>
#include <stdexcept>
#include <charconv>
#include <limits>
#include <climits>
#include <type_traits>
#include <format>
#include <concepts>
#include <array>

Go to the source code of this file.

Namespaces

namespace  ph
 The root for all renderer implementations.
 
namespace  ph::string_utils
 Contains various string manipulation helpers.
 
namespace  ph::string_utils::detail_from_to_char
 

Concepts

concept  ph::string_utils::CHasToString
 

Macros

#define PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION(...)
 
#define PH_DEFINE_INLINE_TO_STRING_FORMATTER(...)
 Defines a formatter which calls the toString() method. For example, to define a toString() formatter for the class SomeType, place the macro after class definition:
 
#define PH_DEFINE_INLINE_TO_STRING_FORMATTER_TEMPLATE(...)    PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION(__VA_ARGS__)
 Defines a formatter template which calls the toString() method. For example, to define a toString() formatter for the class template TSomeType, place the macro after class definition:
 

Enumerations

enum class  ph::string_utils::EWhitespace { ph::string_utils::Common , ph::string_utils::Standard }
 

Functions

template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::get_whitespaces ()
 
template<EWhitespace TYPE = EWhitespace::Common>
constexpr bool ph::string_utils::is_whitespace (const char ch)
 
bool ph::string_utils::has_any_of (const std::string_view srcStr, const std::string_view candidates)
 
bool ph::string_utils::has_none_of (const std::string_view srcStr, const std::string_view candidates)
 
std::string_view ph::string_utils::cut_head (const std::string_view srcStr, const std::string_view candidates)
 Remove characters from the beginning.
 
std::string_view ph::string_utils::cut_tail (const std::string_view srcStr, const std::string_view candidates)
 Remove characters from the end.
 
std::string_view ph::string_utils::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 ph::string_utils::trim_head (const std::string_view srcStr)
 Remove white spaces from the beginning.
 
template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::trim_tail (const std::string_view srcStr)
 Remove white spaces from the end.
 
template<EWhitespace TYPE = EWhitespace::Common>
std::string_view ph::string_utils::trim (const std::string_view srcStr)
 Remove white spaces from both ends.
 
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<>())
 Retrieve a token from a string.
 
char ph::string_utils::az_to_AZ (const char ch)
 Convert lower-case characters to upper-case.
 
char ph::string_utils::AZ_to_az (const char ch)
 Convert upper-case characters to lower-case.
 
void ph::string_utils::az_to_AZ (std::string &str)
 Convert lower-case characters to upper-case.
 
void ph::string_utils::AZ_to_az (std::string &str)
 Convert upper-case characters to lower-case.
 
std::string ph::string_utils::repeat (const std::string_view str, const std::size_t n)
 Repeat the input string for N times.
 
void ph::string_utils::erase_all (std::string &str, const char ch)
 Remove all occurrence of a character in the string.
 
void ph::string_utils::detail_from_to_char::throw_from_std_errc_if_has_error (const std::errc errorCode)
 
template<typename T >
ph::string_utils::parse_float (const std::string_view floatStr)
 Returns a float by processing its string representation. Supports float, double, and long double.
 
template<typename T >
ph::string_utils::parse_int (std::string_view intStr)
 Returns an integer by processing its string representation. Supports the following:
 
template<typename NumberType >
NumberType ph::string_utils::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 ph::string_utils::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 ph::string_utils::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 ph::string_utils::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 ph::string_utils::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 & ph::string_utils::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 ph::string_utils::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

String manipulation helpers.

Macro Definition Documentation

◆ PH_DEFINE_INLINE_TO_STRING_FORMATTER

#define PH_DEFINE_INLINE_TO_STRING_FORMATTER ( ...)
Value:
template<>\
PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION(__VA_ARGS__)

Defines a formatter which calls the toString() method. For example, to define a toString() formatter for the class SomeType, place the macro after class definition:

class SomeType { (class definitions) };
#define PH_DEFINE_INLINE_TO_STRING_FORMATTER(...)
Defines a formatter which calls the toString() method. For example, to define a toString() formatter ...
Definition string_utils.h:64
Parameters
...The type to define a formatter for.

◆ PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION

#define PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION ( ...)
Value:
struct std::formatter<__VA_ARGS__> : std::formatter<std::string>\
{\
static_assert(::ph::string_utils::CHasToString<__VA_ARGS__>,\
"type " #__VA_ARGS__ " must have a const method toString() and the result should be "\
"implicitly convertible to std::string"); \
\
/* `parse()` is inherited from the base class */\
\
/* Define `format()` by calling `std::string`'s implementation with custom type's `toString()`*/\
inline auto format(const __VA_ARGS__& value, std::format_context& ctx) const\
{\
return std::formatter<std::string>::format(\
value.toString(), ctx);\
}\
}

◆ PH_DEFINE_INLINE_TO_STRING_FORMATTER_TEMPLATE

#define PH_DEFINE_INLINE_TO_STRING_FORMATTER_TEMPLATE ( ...)     PH_DEFINE_INLINE_TO_STRING_FORMATTER_SPECIALIZATION(__VA_ARGS__)

Defines a formatter template which calls the toString() method. For example, to define a toString() formatter for the class template TSomeType, place the macro after class definition:

template<typename T>
class TSomeType { (class definitions) };
template<typename T>
#define PH_DEFINE_INLINE_TO_STRING_FORMATTER_TEMPLATE(...)
Defines a formatter template which calls the toString() method. For example, to define a toString() f...
Definition string_utils.h:82
Parameters
...The type to define a formatter for.