Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::StringUtils Class Reference

A collection of string utils, such as case-insensitive comparison and StartsWith/EndsWith. More...

#include "donner/base/StringUtils.h"

Static Public Member Functions

template<StringLike T, StringLike U>
static bool EqualsLowercase (const T &lhs, const U &lowercaseRhs)
 Compare two strings with case-insensitive comparison, fast-path assuming that one of the strings is all-lowercase.
template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static bool Equals (const T &lhs, const U &rhs)
 Returns true if two strings are equal, optionally with a case-insensitive comparison.
template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static bool StartsWith (const T &str, const U &otherStr)
 Returns true if str starts with otherStr.
template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static bool EndsWith (const T &str, const U &otherStr)
 Returns true if str ends with otherStr.
template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static bool Contains (const T &str, const U &otherStr)
 Returns true if str contains otherStr.
template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static size_t Find (const T &str, const U &otherStr)
 Returns the position of otherStr within str, or npos if not found.
template<StringLike T>
static std::vector< std::string_view > Split (const T &str, char ch=' ')
 Splits a string by a given character, returning a range of the split strings as a std::string_view.
template<StringLike T>
static std::string_view TrimWhitespace (const T &str)
 Trims leading and trailing whitespace from a string, returning a view of the trimmed string.

Detailed Description

A collection of string utils, such as case-insensitive comparison and StartsWith/EndsWith.

Member Function Documentation

◆ Contains()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
bool donner::StringUtils::Contains ( const T & str,
const U & otherStr )
inlinestatic

Returns true if str contains otherStr.

StringUtils::Contains("Hello world", "ello"); // true
static bool Contains(const T &str, const U &otherStr)
Returns true if str contains otherStr.
Definition StringUtils.h:251
Parameters
strThe string to check for a suffix.
otherStrThe suffix to check for.
Template Parameters
ComparisonThe comparison type to use, defaults to StringComparison::Default.
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
true If the strings are equal.

◆ EndsWith()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
bool donner::StringUtils::EndsWith ( const T & str,
const U & otherStr )
inlinestatic

Returns true if str ends with otherStr.

StringUtils::EndsWith("Hello", "llo"); // true
static bool EndsWith(const T &str, const U &otherStr)
Returns true if str ends with otherStr.
Definition StringUtils.h:223
Parameters
strThe string to check for a suffix.
otherStrThe suffix to check for.
Template Parameters
ComparisonThe comparison type to use, defaults to StringComparison::Default.
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
true If the strings are equal.

◆ Equals()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
bool donner::StringUtils::Equals ( const T & lhs,
const U & rhs )
inlinestatic

Returns true if two strings are equal, optionally with a case-insensitive comparison.

StringUtils::Equals("Hello", "hello"); // false
static bool Equals(const T &lhs, const U &rhs)
Returns true if two strings are equal, optionally with a case-insensitive comparison.
Definition StringUtils.h:170
Parameters
lhsThe first string to compare.
rhsThe second string to compare.
Template Parameters
ComparisonThe comparison type to use, defaults to StringComparison::Default.
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
true If the strings are equal.

◆ EqualsLowercase()

template<StringLike T, StringLike U>
bool donner::StringUtils::EqualsLowercase ( const T & lhs,
const U & lowercaseRhs )
inlinestatic

Compare two strings with case-insensitive comparison, fast-path assuming that one of the strings is all-lowercase.

StringUtils::EqualsLowercase("Hello", "hello"); // true
StringUtils::EqualsLowercase("Hello", "HELLO"); // invalid, rhs must be lowercase
static bool EqualsLowercase(const T &lhs, const U &lowercaseRhs)
Compare two strings with case-insensitive comparison, fast-path assuming that one of the strings is a...
Definition StringUtils.h:136
Parameters
lhsThe first string to compare, can be any case.
lowercaseRhsstring to compare to, must be lowercase.
Precondition
lowercaseRhs must be an all-lowercase string.
Template Parameters
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
true If the lowercaseRhs is equal to the lhs, ignoring the case of the lhs.

◆ Find()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
size_t donner::StringUtils::Find ( const T & str,
const U & otherStr )
inlinestatic

Returns the position of otherStr within str, or npos if not found.

StringUtils::Find("Hello world", "world"); // returns 6
StringUtils::Find<StringComparison::IgnoreCase>("Hello world", "WORLD"); // returns 6
StringUtils::Find("Hello world", "xyz"); // returns npos
static size_t Find(const T &str, const U &otherStr)
Returns the position of otherStr within str, or npos if not found.
Definition StringUtils.h:285
Parameters
strThe string to search within.
otherStrThe substring to find.
Template Parameters
ComparisonThe comparison type to use, defaults to StringComparison::Default.
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
The position of the substring if found, or npos if not found.

◆ Split()

template<StringLike T>
std::vector< std::string_view > donner::StringUtils::Split ( const T & str,
char ch = ' ' )
inlinestatic

Splits a string by a given character, returning a range of the split strings as a std::string_view.

for (auto&& str : StringUtils::Split("a,b,c", ',')) {
// ...
}
static std::vector< std::string_view > Split(const T &str, char ch=' ')
Splits a string by a given character, returning a range of the split strings as a std::string_view.
Definition StringUtils.h:317
Template Parameters
TThe string type to split, must be StringLike (have size() and data() methods).
Parameters
strThe string to split.
chThe character to split by.
Returns
A vector of the split string views.

◆ StartsWith()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
bool donner::StringUtils::StartsWith ( const T & str,
const U & otherStr )
inlinestatic

Returns true if str starts with otherStr.

StringUtils::StartsWith("Hello", "He"); // true
static bool StartsWith(const T &str, const U &otherStr)
Returns true if str starts with otherStr.
Definition StringUtils.h:197
Parameters
strThe string to check for a prefix.
otherStrThe prefix to check for.
Template Parameters
ComparisonThe comparison type to use, defaults to StringComparison::Default.
TThe type of the first string, must be StringLike (have size() and data() methods). @tparam U The type of the second string, must be \ref StringLike (have size() and data() methods).
Returns
true If the strings are equal.

◆ TrimWhitespace()

template<StringLike T>
std::string_view donner::StringUtils::TrimWhitespace ( const T & str)
inlinestatic

Trims leading and trailing whitespace from a string, returning a view of the trimmed string.

std::string_view result = StringUtils::TrimWhitespace(" hello ");
// result is "hello"
static std::string_view TrimWhitespace(const T &str)
Trims leading and trailing whitespace from a string, returning a view of the trimmed string.
Definition StringUtils.h:343
Parameters
strThe string to trim.
Template Parameters
TThe type of the string, must be StringLike (have size() and data() methods).
Returns
A view of the trimmed string.

The documentation for this class was generated from the following file: