RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RST::String Namespace Reference

String helpers: case, trimming, splitting, joining, replacing, parsing and hashing. More...

Namespaces

namespace  Literals
 String hashes as literals, to switch on strings: case "start"_hash:.
 

Functions

Characters

ASCII only: unlike <cctype>, they ignore the locale and accept any char.

constexpr bool isSpace (char c) noexcept
 
constexpr bool isDigit (char c) noexcept
 
constexpr bool isUpper (char c) noexcept
 
constexpr bool isLower (char c) noexcept
 
constexpr bool isAlpha (char c) noexcept
 
constexpr bool isAlnum (char c) noexcept
 
constexpr bool isHexDigit (char c) noexcept
 
constexpr char toLower (char c) noexcept
 
constexpr char toUpper (char c) noexcept
 
Searching and comparing

The IgnoreCase variants ignore the case of ASCII letters.

constexpr bool contains (std::string_view str, std::string_view needle) noexcept
 
constexpr bool contains (std::string_view str, char c) noexcept
 
constexpr bool equalsIgnoreCase (std::string_view lhs, std::string_view rhs) noexcept
 
constexpr bool startsWithIgnoreCase (std::string_view str, std::string_view prefix) noexcept
 
constexpr bool endsWithIgnoreCase (std::string_view str, std::string_view suffix) noexcept
 
constexpr std::size_t findIgnoreCase (std::string_view str, std::string_view needle, std::size_t from=0) noexcept
 Position of the first needle at or after from, or std::string_view::npos.
 
constexpr bool containsIgnoreCase (std::string_view str, std::string_view needle) noexcept
 
constexpr int compareIgnoreCase (std::string_view lhs, std::string_view rhs) noexcept
 Three-way comparison like strcmp: negative, zero or positive.
 
Conversions
template<typename T >
std::optional< T > parseNumber (std::string_view text, int base=10) noexcept
 Parses text as a number, or returns std::nullopt if it is not entirely one.
 
constexpr std::optional< bool > parseBool (std::string_view text) noexcept
 Parses true, yes, on, 1 or false, no, off, 0, ignoring case.
 
Hashing
constexpr std::uint64_t hash (std::string_view str) noexcept
 64-bit FNV-1a hash, usable at compile time.
 
Joining

Numbers are formatted with std::format: join({1, 2, 3}, ", ") gives 1, 2, 3.

template<typename Container >
std::string join (const Container &parts, std::string_view separator="")
 Joins the elements of parts, strings or numbers, with separator between them.
 
template<typename T >
std::string join (std::initializer_list< T > parts, std::string_view separator="")
 Joins a braced list, such as join({4, 5, 6}, "-").
 
std::string joinString (const std::vector< std::string > &stringList, char delimiter='\0')
 Joins stringList with a one-character delimiter; the default adds none.
 
Replacing

An empty from matches nothing.

std::size_t replaceAll (std::string &str, std::string_view from, std::string_view to)
 Replaces every from in str with to and returns the number of replacements.
 
std::string replaceAllCopy (std::string_view str, std::string_view from, std::string_view to)
 Returns a copy of str with every from replaced by to.
 
bool replaceFirst (std::string &str, std::string_view from, std::string_view to)
 Replaces the first from in str with to; returns false if there is none.
 
Splitting

Every character of delimiters separates two parts, and empty parts are skipped.

std::vector< std::string > splitString (std::string_view str, std::string_view delimiters)
 Splits str into strings.
 
std::vector< std::string_view > splitStringView (std::string_view str, std::string_view delimiters)
 Splits str into views, without copying: str must outlive them.
 
std::vector< std::string > splitStringWithQuotes (std::string_view str, std::string_view delimiters, char quote='\"')
 Splits str, keeping the delimiters found between two quote characters and removing the quotes.
 
Case conversion
void toLower (std::string &str) noexcept
 Lowercases the ASCII letters of str in place.
 
std::string toLowerCopy (std::string_view str)
 Returns a copy of str with its ASCII letters lowercased.
 
void toUpper (std::string &str) noexcept
 Uppercases the ASCII letters of str in place.
 
std::string toUpperCopy (std::string_view str)
 Returns a copy of str with its ASCII letters uppercased.
 

Trimming

These functions remove chars, whitespace by default, from both ends of a string, or only from the left (l) or right (r) end.

View functions return a view without copying; the others trim in place or return a Copy.

constexpr std::string_view Whitespace = " \t\n\r\f\v"
 Characters trimmed by default: space, tab, line feed, carriage return, form feed and vertical tab.
 
constexpr std::string_view ltrimView (std::string_view str, std::string_view chars=Whitespace) noexcept
 
constexpr std::string_view rtrimView (std::string_view str, std::string_view chars=Whitespace) noexcept
 
constexpr std::string_view trimView (std::string_view str, std::string_view chars=Whitespace) noexcept
 
void trim (std::string &str, std::string_view chars=Whitespace)
 
void ltrim (std::string &str, std::string_view chars=Whitespace)
 
void rtrim (std::string &str, std::string_view chars=Whitespace)
 
std::string trimCopy (std::string_view str, std::string_view chars=Whitespace)
 
std::string ltrimCopy (std::string_view str, std::string_view chars=Whitespace)
 
std::string rtrimCopy (std::string_view str, std::string_view chars=Whitespace)
 

Detailed Description

String helpers: case, trimming, splitting, joining, replacing, parsing and hashing.

In-place functions such as trim() come with a Copy variant such as trimCopy().

Function Documentation

◆ isSpace()

constexpr bool RST::String::isSpace ( char  c)
constexprnoexcept

◆ isDigit()

constexpr bool RST::String::isDigit ( char  c)
constexprnoexcept

◆ isUpper()

constexpr bool RST::String::isUpper ( char  c)
constexprnoexcept

◆ isLower()

constexpr bool RST::String::isLower ( char  c)
constexprnoexcept

◆ isAlpha()

constexpr bool RST::String::isAlpha ( char  c)
constexprnoexcept

◆ isAlnum()

constexpr bool RST::String::isAlnum ( char  c)
constexprnoexcept

◆ isHexDigit()

constexpr bool RST::String::isHexDigit ( char  c)
constexprnoexcept

◆ toLower() [1/2]

constexpr char RST::String::toLower ( char  c)
constexprnoexcept

◆ toUpper() [1/2]

constexpr char RST::String::toUpper ( char  c)
constexprnoexcept

◆ contains() [1/2]

constexpr bool RST::String::contains ( std::string_view  str,
std::string_view  needle 
)
constexprnoexcept

◆ contains() [2/2]

constexpr bool RST::String::contains ( std::string_view  str,
char  c 
)
constexprnoexcept

◆ equalsIgnoreCase()

constexpr bool RST::String::equalsIgnoreCase ( std::string_view  lhs,
std::string_view  rhs 
)
constexprnoexcept

◆ startsWithIgnoreCase()

constexpr bool RST::String::startsWithIgnoreCase ( std::string_view  str,
std::string_view  prefix 
)
constexprnoexcept

◆ endsWithIgnoreCase()

constexpr bool RST::String::endsWithIgnoreCase ( std::string_view  str,
std::string_view  suffix 
)
constexprnoexcept

◆ findIgnoreCase()

constexpr std::size_t RST::String::findIgnoreCase ( std::string_view  str,
std::string_view  needle,
std::size_t  from = 0 
)
constexprnoexcept

Position of the first needle at or after from, or std::string_view::npos.

◆ containsIgnoreCase()

constexpr bool RST::String::containsIgnoreCase ( std::string_view  str,
std::string_view  needle 
)
constexprnoexcept

◆ compareIgnoreCase()

constexpr int RST::String::compareIgnoreCase ( std::string_view  lhs,
std::string_view  rhs 
)
constexprnoexcept

Three-way comparison like strcmp: negative, zero or positive.

◆ parseNumber()

template<typename T >
std::optional< T > RST::String::parseNumber ( std::string_view  text,
int  base = 10 
)
noexcept

Parses text as a number, or returns std::nullopt if it is not entirely one.

Surrounding whitespace and a leading + are allowed. Integers accept any base from 2 to 36, with an optional 0x prefix in base 16; floating-point numbers are base 10 only.

◆ parseBool()

constexpr std::optional< bool > RST::String::parseBool ( std::string_view  text)
constexprnoexcept

Parses true, yes, on, 1 or false, no, off, 0, ignoring case.

◆ hash()

constexpr std::uint64_t RST::String::hash ( std::string_view  str)
constexprnoexcept

64-bit FNV-1a hash, usable at compile time.

◆ join() [1/2]

template<typename Container >
std::string RST::String::join ( const Container &  parts,
std::string_view  separator = "" 
)

Joins the elements of parts, strings or numbers, with separator between them.

◆ join() [2/2]

template<typename T >
std::string RST::String::join ( std::initializer_list< T >  parts,
std::string_view  separator = "" 
)

Joins a braced list, such as join({4, 5, 6}, "-").

◆ joinString()

std::string RST::String::joinString ( const std::vector< std::string > &  stringList,
char  delimiter = '\0' 
)

Joins stringList with a one-character delimiter; the default adds none.

◆ replaceAll()

std::size_t RST::String::replaceAll ( std::string &  str,
std::string_view  from,
std::string_view  to 
)

Replaces every from in str with to and returns the number of replacements.

◆ replaceAllCopy()

std::string RST::String::replaceAllCopy ( std::string_view  str,
std::string_view  from,
std::string_view  to 
)

Returns a copy of str with every from replaced by to.

◆ replaceFirst()

bool RST::String::replaceFirst ( std::string &  str,
std::string_view  from,
std::string_view  to 
)

Replaces the first from in str with to; returns false if there is none.

◆ splitString()

std::vector< std::string > RST::String::splitString ( std::string_view  str,
std::string_view  delimiters 
)

Splits str into strings.

◆ splitStringView()

std::vector< std::string_view > RST::String::splitStringView ( std::string_view  str,
std::string_view  delimiters 
)

Splits str into views, without copying: str must outlive them.

◆ splitStringWithQuotes()

std::vector< std::string > RST::String::splitStringWithQuotes ( std::string_view  str,
std::string_view  delimiters,
char  quote = '\"' 
)

Splits str, keeping the delimiters found between two quote characters and removing the quotes.

◆ toLower() [2/2]

void RST::String::toLower ( std::string &  str)
noexcept

Lowercases the ASCII letters of str in place.

◆ toLowerCopy()

std::string RST::String::toLowerCopy ( std::string_view  str)

Returns a copy of str with its ASCII letters lowercased.

◆ toUpper() [2/2]

void RST::String::toUpper ( std::string &  str)
noexcept

Uppercases the ASCII letters of str in place.

◆ toUpperCopy()

std::string RST::String::toUpperCopy ( std::string_view  str)

Returns a copy of str with its ASCII letters uppercased.

◆ ltrimView()

constexpr std::string_view RST::String::ltrimView ( std::string_view  str,
std::string_view  chars = Whitespace 
)
constexprnoexcept

◆ rtrimView()

constexpr std::string_view RST::String::rtrimView ( std::string_view  str,
std::string_view  chars = Whitespace 
)
constexprnoexcept

◆ trimView()

constexpr std::string_view RST::String::trimView ( std::string_view  str,
std::string_view  chars = Whitespace 
)
constexprnoexcept

◆ trim()

void RST::String::trim ( std::string &  str,
std::string_view  chars = Whitespace 
)

◆ ltrim()

void RST::String::ltrim ( std::string &  str,
std::string_view  chars = Whitespace 
)

◆ rtrim()

void RST::String::rtrim ( std::string &  str,
std::string_view  chars = Whitespace 
)

◆ trimCopy()

std::string RST::String::trimCopy ( std::string_view  str,
std::string_view  chars = Whitespace 
)
Examples
quickstart.cpp.

◆ ltrimCopy()

std::string RST::String::ltrimCopy ( std::string_view  str,
std::string_view  chars = Whitespace 
)

◆ rtrimCopy()

std::string RST::String::rtrimCopy ( std::string_view  str,
std::string_view  chars = Whitespace 
)

Variable Documentation

◆ Whitespace

constexpr std::string_view RST::String::Whitespace = " \t\n\r\f\v"
inlineconstexpr

Characters trimmed by default: space, tab, line feed, carriage return, form feed and vertical tab.