GlitchyByte Lib  0.3.1
GlitchyByte general library to bootstrap development.
Classes | Functions | Variables
gb::strings Namespace Reference

Classes

class  StringNumberParseException
 

Functions

constexpr std::vector< std::string_view > createVectorStringViewFromCArray (int const argc, char const *argv[]) noexcept
 
constexpr std::vector< std::string > createVectorStringFromCArray (int const argc, char const *argv[]) noexcept
 
constexpr std::string replace (std::string_view const str, std::string_view const token, std::string_view const value) noexcept
 
constexpr std::string & replaceInPlace (std::string &str, std::string_view const token, std::string_view const value) noexcept
 
constexpr std::vector< std::string_view > splitWeak (std::string_view const str, std::string_view const delimiter) noexcept
 
constexpr std::vector< std::string > split (std::string_view const &str, std::string_view const &delimiter) noexcept
 
std::string unindent (std::string_view const str) noexcept
 
template<typename T >
std::string fromVector (std::vector< T > const &vector, std::string_view const separator=", ") noexcept
 
constexpr std::string addThousandSeparators (std::string_view const str) noexcept
 
std::string fromTime (std::time_t const &time, std::string const &format) noexcept
 
template<Numeric T>
constexpr std::string fromNumber (T const value) noexcept
 
template<std::integral T>
toNumber (std::string_view const str)
 
template<std::integral T>
constexpr std::string hexFromNumber (T const value, bool const padding=false, bool const uppercase=false) noexcept
 
template<std::integral T>
hexToNumber (std::string_view const str)
 

Variables

constexpr std::errc errcSuccess {}
 

Function Documentation

◆ createVectorStringViewFromCArray()

constexpr std::vector<std::string_view> gb::strings::createVectorStringViewFromCArray ( int const  argc,
char const *  argv[] 
)
inlineconstexprnoexcept

Converts a C string array into a vector of string_views.

The contents of the vector will be valid as long as the original array is valid.

This function is specifically designed to convert main function args into a string vector.

Parameters
argcString array size.
argvArray of C strings.
Returns
A string vector of the arguments.

◆ createVectorStringFromCArray()

constexpr std::vector<std::string> gb::strings::createVectorStringFromCArray ( int const  argc,
char const *  argv[] 
)
inlineconstexprnoexcept

Converts a C string array into a vector of strings.

This function is specifically designed to convert main function args into a string vector.

Parameters
argcString array size.
argvArray of C strings.
Returns
A string vector of the arguments.

◆ replace()

constexpr std::string gb::strings::replace ( std::string_view const  str,
std::string_view const  token,
std::string_view const  value 
)
inlineconstexprnoexcept

Convenience replace of a token in a string.

This creates a new string, so it is only a shortcut, not efficient for replacing many tokens.

Parameters
strString to search for token.
tokenToken to find.
valueValue to replace the token with.
Returns
The replaced string with the token replaced.

◆ replaceInPlace()

constexpr std::string& gb::strings::replaceInPlace ( std::string &  str,
std::string_view const  token,
std::string_view const  value 
)
inlineconstexprnoexcept

Convenience replace of a token in a string.

This modifies the string,

Parameters
strString to search for token.
tokenToken to find.
valueValue to replace the token with.
Returns
The replaced string with the token replaced.

◆ splitWeak()

constexpr std::vector<std::string_view> gb::strings::splitWeak ( std::string_view const  str,
std::string_view const  delimiter 
)
inlineconstexprnoexcept

Splits a string by the delimiter into a vector of strings.

Storage is weak. Meaning, the resulting vector is actually pointing at sections in the original string. This is faster than regular split, but if the original string goes away, the contents of this vector are not valid.

Parameters
strString to split.
delimiterDelimiter to split the string.
Returns
A vector of strings.

◆ split()

constexpr std::vector<std::string> gb::strings::split ( std::string_view const &  str,
std::string_view const &  delimiter 
)
inlineconstexprnoexcept

Splits a string by the delimiter into a vector of strings.

Parameters
strString to split.
delimiterDelimiter to split the string.
Returns
A vector of strings.

◆ unindent()

std::string gb::strings::unindent ( std::string_view const  str)
inlinenoexcept

Unindents a multiline block of text by removing all common spaces or tabs from the beginning of the lines. Empty lines are left alone.

This function is specifically designed to use with raw string literals. And it will remove indentation created to align the string with the rest of the surrounding code.

Only spaces are considered when recognizing indentation.

Parameters
strString to unindent.
Returns
The unindented string.

◆ fromVector()

template<typename T >
std::string gb::strings::fromVector ( std::vector< T > const &  vector,
std::string_view const  separator = ", " 
)
inlinenoexcept

Joins a string representation of all elements into a string. By default each item is separated by a comma and a space.

Template Parameters
TThe type of item.
Parameters
vectorVector of items to join.
separatorSeparator between items.
Returns
A joined string of all items.

◆ addThousandSeparators()

constexpr std::string gb::strings::addThousandSeparators ( std::string_view const  str)
inlineconstexprnoexcept

Adds thousand separators in the given string that must be a numeric representation.

Parameters
strString numeric representation.
Returns
The numeric representation with thousand separators.

◆ fromTime()

std::string gb::strings::fromTime ( std::time_t const &  time,
std::string const &  format 
)
inlinenoexcept

Returns a string representation of the given time.

Parameters
timeTime to convert to string.
formatTime format.
Returns
String representation of the given time.

◆ fromNumber()

template<Numeric T>
constexpr std::string gb::strings::fromNumber ( T const  value)
constexprnoexcept

Converts a number value to a string representation.

Template Parameters
TA numeric type.
Parameters
valueNumber value.
Returns
The string representation of value.

◆ toNumber()

template<std::integral T>
T gb::strings::toNumber ( std::string_view const  str)
inline

Converts a string to its number value representation.

Template Parameters
TA numeric type.
Parameters
strA string.
Returns
The parsed number value.
Exceptions
StringNumberParseExceptionif the string can't be parsed to a numeric value.

◆ hexFromNumber()

template<std::integral T>
constexpr std::string gb::strings::hexFromNumber ( T const  value,
bool const  padding = false,
bool const  uppercase = false 
)
constexprnoexcept

Converts a number value to a string hex representation.

Template Parameters
TAn integral type.
Parameters
valueNumber value.
paddingIf true, the representation will be padded with 0s to the extent of the type. Default is false.
uppercaseIf true, letters will be in uppercase. Default is false.
Returns
The hex string representation of value.

◆ hexToNumber()

template<std::integral T>
T gb::strings::hexToNumber ( std::string_view const  str)
inline

Converts a hex string to its number value representation.

Template Parameters
TAn integral type.
Parameters
strA hex string.
Returns
The parsed number value.
Exceptions
StringNumberParseExceptionif the hex string can't be parsed to an integral value.

Variable Documentation

◆ errcSuccess

constexpr std::errc gb::strings::errcSuccess {}
constexpr

errc success constant.