Skip to content

Util

This header file collects various utility functions.

INCLUDE FILE
#include <tpau-cpp-kernal/Util.h>

function clean_type_name

Get the clean name of the type, without any compiler-specific prefixes.

DEFINITION
std::string_view  clean_type_name ( std::string_view  raw_name );
PARAMETERDESCRIPTION
raw_name

The raw type name.

RETURNSDESCRIPTION
std::string_view

The clean type name.

function default_output_filename

Get a default output file name based on input file name and extension.

It discards the directory part of the input file name and replaces the extension.

DEFINITION
std::filesystem::path  default_output_filename ( const std::filesystem::path & input_filename,
std::string_view  extension );
PARAMETERDESCRIPTION
input_filename

The input file name.

extension

The extension to use for the output file name.

RETURNSDESCRIPTION
std::filesystem::path

The default output file name.

function get_with_default

Get the value associated with a key in an unordered map, or return a default-constructed value if the key is not found.

DEFINITION
template <typename K, typename V>
V  get_with_default ( const std::unordered_map< K, V > & map,
const K & key,
V  default_value = {} );
PARAMETERDESCRIPTION
map

The unordered map to search.

key

The key to look for.

default_value

The default value to return if the key is not found.

RETURNSDESCRIPTION
V

The value associated with the key, or a default-constructed value if the key is not found.

function join

Join a vector of symbols into a string with a separator between the symbols.

DEFINITION
std::string  join ( const std::vector< Symbol > & symbols,
std::string_view  separator = ", " );
PARAMETERDESCRIPTION
symbols

The symbols to join.

separator

The separator to use between the symbols.

RETURNSDESCRIPTION
std::string

The joined string.

function join

Join a range of strings into a string with a separator between the strings.

DEFINITION
template <std::ranges::input_rangeR> requires std::same_as<std::iter_value_t<R>, std::string>
std::string  join ( const R & strings,
std::string_view  separator );
PARAMETERDESCRIPTION
strings

The range of strings to join.

separator

The separator to use between the strings.

RETURNSDESCRIPTION
std::string

The joined string.

function replace_extension

Replace the extension of a file name with a new extension.

DEFINITION
std::filesystem::path  replace_extension ( const std::filesystem::path & file_name,
std::string_view  extension );
PARAMETERDESCRIPTION
file_name

The file name to replace the extension of.

extension

The new extension to use.

RETURNSDESCRIPTION
std::filesystem::path

The file name with the replaced extension.

function sorted

Sort a range of elements and return the sorted collection.

DEFINITION
template <std::ranges::input_rangeR, typename T, class Comp>
std::vector< T >  sorted ( const R & collection,
Comp  comp = {} );
PARAMETERDESCRIPTION
collection

The range of elements to sort.

comp

The comparison function to use for sorting. If not provided, the default comparison operator will be used.

RETURNSDESCRIPTION
std::vector< T >

The sorted collection.

function sorted

Sort a range of elements specified by iterators and return the sorted collection.

DEFINITION
template <std::input_iteratorI, typename T, class Comp>
std::vector< T >  sorted ( I  first,
I  last,
Comp  comp = {} );
PARAMETERDESCRIPTION
first

The iterator pointing to the first element in the range.

last

The iterator pointing to one past the last element in the range.

comp

The comparison function to use for sorting. If not provided, the default comparison operator will be used.

RETURNSDESCRIPTION
std::vector< T >

The sorted collection.

function split

Split a string into a vector of strings based on a set of delimiters.

DEFINITION
std::vector< std::string >  split ( std::string_view  str,
std::string_view  delimiters = " \t\n\r\f\v",
bool  keep_empty = false );
PARAMETERDESCRIPTION
str

The string to split.

delimiters

The set of delimiters to use for splitting.

keep_empty

If true, consecutive delimiters will result in empty strings in the output.

RETURNSDESCRIPTION
std::vector< std::string >

The vector of split strings.

function strerror_string

Get a string representation of a system error code.

DEFINITION
std::string  strerror_string ( std::optional< int >  errnum = {} );
PARAMETERDESCRIPTION
errnum

The error code to get the string representation of. If not provided, the current value of errno will be used.

RETURNSDESCRIPTION
std::string

The string representation of the error code.

function trim

Trim characters from the beginning and end of a string.

DEFINITION
std::string_view  trim ( std::string_view  str,
std::string_view  characters = " \t\n\r\f\v" );
PARAMETERDESCRIPTION
str

The string to trim.

characters

The set of characters to trim.

RETURNSDESCRIPTION
std::string_view

The trimmed string.