General Utility Library for C++17 25.4.1
Functions
gul17/trim.h

Detailed Description

Trimming whitespace from strings.

Functions

GUL_EXPORT std::string gul17::trim (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a new std::string.
 
GUL_EXPORT std::string_view gul17::trim_sv (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.
 
GUL_EXPORT std::string gul17::trim_left (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string.
 
GUL_EXPORT std::string_view gul17::trim_left_sv (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim leading whitespace (or a custom set of characters) from a string, returning a view into the original string.
 
GUL_EXPORT std::string gul17::trim_right (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string.
 
GUL_EXPORT std::string_view gul17::trim_right_sv (std::string_view str, std::string_view ws_chars=default_whitespace_characters)
 Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.
 

Function Documentation

◆ trim()

std::string gul17::trim ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim("\n \b trim(), default whitespace\t \r") << "]\n";
// prints "[trim(), default whitespace]"
cout << "[" << trim(".:.:.:trim(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[trim(), custom whitespace]"
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:121
GUL_EXPORT std::string trim(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim leading and trailing whitespace (or a custom set of characters) from a string,...
Definition trim.cc:29
Parameters
strThe string that should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_sv() returns a std::string_view instead of a copied string,
trim_left() and trim_right() trim only one side of the string,
trim_left_sv() and trim_right_sv() trim only one side and return a std::string_view.
Examples
trim.cc.

References gul17::bit_set(), and gul17::trim_sv().

◆ trim_left()

std::string gul17::trim_left ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left("\n \b trim_left(), default whitespace ") << "]\n";
// prints "[trim_left(), default whitespace ]"
cout << "[" << trim_right(".:.:.:trim_right(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right, custom whitespace]"
GUL_EXPORT std::string trim_right(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string.
Definition trim.cc:61
GUL_EXPORT std::string trim_left(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string.
Definition trim.cc:46
Parameters
strThe string from which leading characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_left_sv() returns a std::string_view instead of a copied string,
trim_right() and trim_right_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul17::bit_set(), and gul17::trim_left_sv().

◆ trim_left_sv()

std::string_view gul17::trim_left_sv ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim leading whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left_sv("\n \b trim_left_sv(), default whitespace ") << "]\n";
// prints "[trim_left_sv(), default whitespace ]"
cout << "[" << trim_right_sv(".:.:.:trim_right_sv(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right_sv, custom whitespace]"
GUL_EXPORT std::string_view trim_right_sv(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the ori...
Definition trim.cc:66
GUL_EXPORT std::string_view trim_left_sv(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim leading whitespace (or a custom set of characters) from a string, returning a view into the orig...
Definition trim.cc:51
Parameters
strThe string from which leading characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed std::string_view that points into the input string.
See also
trim_left() returns a copied string instead of a std::string_view,
trim_right() and trim_right_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul17::bit_set().

Referenced by gul17::trim_left().

◆ trim_right()

std::string gul17::trim_right ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left("\n \b trim_left(), default whitespace ") << "]\n";
// prints "[trim_left(), default whitespace ]"
cout << "[" << trim_right(".:.:.:trim_right(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right, custom whitespace]"
Parameters
strThe string from which trailing characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_right_sv() returns a std::string_view instead of a copied string,
trim_left() and trim_left_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul17::bit_set(), and gul17::trim_right_sv().

◆ trim_right_sv()

std::string_view gul17::trim_right_sv ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left_sv("\n \b trim_left_sv(), default whitespace ") << "]\n";
// prints "[trim_left_sv(), default whitespace ]"
cout << "[" << trim_right_sv(".:.:.:trim_right_sv(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right_sv, custom whitespace]"
Parameters
strThe string from which trailing characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed std::string_view that points into the input string.
See also
trim_right() returns a copied string instead of a std::string_view,
trim_left() and trim_left_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul17::bit_set().

Referenced by gul17::trim_right().

◆ trim_sv()

std::string_view gul17::trim_sv ( std::string_view  str,
std::string_view  ws_chars = default_whitespace_characters 
)

Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

std::string str = " string_view ";
auto sv = trim_sv(str); // sv is a string_view that points to the original string
cout << "[" << sv << "]\n";
// prints "[string_view]"
str[5] = 'o'; // modify the original string
cout << "[" << sv << "]\n";
// prints "[strong_view]"
GUL_EXPORT std::string_view trim_sv(std::string_view str, std::string_view ws_chars=default_whitespace_characters)
Trim leading and trailing whitespace (or a custom set of characters) from a string,...
Definition trim.cc:34
Parameters
strThe string that should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed std::string_view that points into the input string.
See also
trim() returns a copied string instead of a std::string_view,
trim_left_sv() and trim_right_sv() trim only one side of the string,
trim_left() and trim_right() trim only one side and return a copied string.
Examples
trim.cc.

References gul17::bit_set().

Referenced by gul17::trim().