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<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.
 

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>
static bool donner::StringUtils::Contains ( const T & str,
const U & otherStr )
inlinestatic

Returns true if str contains otherStr.

StringUtils::Contains("Hello world", "ello"); // true
StringUtils::Contains<StringComparison::IgnoreCase>("Hello world", "ELLO"); // true
static bool Contains(const T &str, const U &otherStr)
Returns true if str contains otherStr.
Definition StringUtils.h:248
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 (havesize()anddata() methods).
Returns
true If the strings are equal.

◆ EndsWith()

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

Returns true if str ends with otherStr.

StringUtils::EndsWith("Hello", "llo"); // true
StringUtils::EndsWith<StringComparison::IgnoreCase>("Hello", "LLO"); // true
static bool EndsWith(const T &str, const U &otherStr)
Returns true if str ends with otherStr.
Definition StringUtils.h:220
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 (havesize()anddata() methods).
Returns
true If the strings are equal.

◆ Equals()

template<StringComparison Comparison = StringComparison::Default, StringLike T, StringLike U>
static 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
StringUtils::Equals<StringComparison::IgnoreCase>("Hello", "hello"); // true
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:168
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 (havesize()anddata() methods).
Returns
true If the strings are equal.

◆ EqualsLowercase()

template<StringLike T, StringLike U>
static 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:134
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 (havesize()anddata() methods).
Returns
true If the lowercaseRhs is equal to the lhs, ignoring the case of the lhs.

◆ Split()

template<StringLike T>
static 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", ',')) {
// ...
}
A collection of string utils, such as case-insensitive comparison and StartsWith/EndsWith.
Definition StringUtils.h:113
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:280
Template Parameters
TThe string type to split.
Parameters
strThe string to split.
chThe character to split by.
Template Parameters
TThe type of the string, must be StringLike (have size() and data() methods).
Returns
A vector of the split string views.

◆ StartsWith()

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

Returns true if str starts with otherStr.

StringUtils::StartsWith("Hello", "He"); // true
StringUtils::StartsWith<StringComparison::IgnoreCase>("Hello", "he"); // true
static bool StartsWith(const T &str, const U &otherStr)
Returns true if str starts with otherStr.
Definition StringUtils.h:194
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 (havesize()anddata() methods).
Returns
true If the strings are equal.

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