Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
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 the non-empty pieces as std::string_views.
template<StringLike T>
static std::vector< std::string_view > SplitAsciiWhitespace (const T &str)
 Split at ASCII whitespace: space, tab, line feed, form feed and carriage return (not vertical tab, which IsAsciiWhitespace includes).
template<StringLike T>
static std::string ToLowerAscii (const T &str)
 Returns a copy of str with ASCII A-Z mapped to a-z and every other byte unchanged.
static constexpr char ToLowerAscii (char ch)
 Returns ch with ASCII A-Z mapped to a-z; every other byte is returned unchanged, whatever the process C locale.
static constexpr bool IsAsciiWhitespace (char ch)
 Returns true for the bytes std::isspace accepts in the "C" locale: space, tab, line feed, vertical tab, form feed and carriage return.
static constexpr bool IsAsciiAlnum (char ch)
 Returns true for ASCII letters and digits (A-Z, a-z, 0-9), the bytes std::isalnum accepts in the "C" locale.
static constexpr bool IsAsciiAlpha (char ch)
 Returns true for ASCII letters (A-Z, a-z), the bytes std::isalpha accepts in the "C" locale.
static constexpr bool IsAsciiDigit (char ch)
 Returns true for ASCII digits (0-9), the bytes std::isdigit accepts.
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:261
Parameters
strThe string to search within.
otherStrThe substring to search 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 \c StringLike (have size() and data() methods).
Returns
true If str contains otherStr.

◆ 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:233
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 \c StringLike (have size() and data() methods).
Returns
true If str ends with otherStr.

◆ 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:180
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 \c 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:146
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 \c 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:295
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 \c StringLike (have size() and data() methods).
Returns
The position of the substring if found, or npos if not found.

◆ IsAsciiAlnum()

constexpr bool donner::StringUtils::IsAsciiAlnum ( char ch)
inlinestaticconstexpr

Returns true for ASCII letters and digits (A-Z, a-z, 0-9), the bytes std::isalnum accepts in the "C" locale.

Unlike std::isalnum, bytes above 0x7F are never letters, whatever the process locale, and any char value is a valid argument.

Parameters
chThe character to classify.

◆ IsAsciiAlpha()

constexpr bool donner::StringUtils::IsAsciiAlpha ( char ch)
inlinestaticconstexpr

Returns true for ASCII letters (A-Z, a-z), the bytes std::isalpha accepts in the "C" locale.

Bytes above 0x7F are never letters, whatever the process locale.

Parameters
chThe character to classify.

◆ IsAsciiDigit()

constexpr bool donner::StringUtils::IsAsciiDigit ( char ch)
inlinestaticconstexpr

Returns true for ASCII digits (0-9), the bytes std::isdigit accepts.

Parameters
chThe character to classify.

◆ IsAsciiWhitespace()

constexpr bool donner::StringUtils::IsAsciiWhitespace ( char ch)
inlinestaticconstexpr

Returns true for the bytes std::isspace accepts in the "C" locale: space, tab, line feed, vertical tab, form feed and carriage return.

Unlike std::isspace, bytes above 0x7F are never whitespace, whatever the process locale.

This set includes vertical tab, unlike the WHATWG ASCII whitespace that SplitAsciiWhitespace splits on. Grammars with their own whitespace set, such as CSS or XML, need their own check.

Parameters
chThe character to classify.

◆ 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 the non-empty pieces as std::string_views.

Empty pieces, from leading, trailing, or adjacent separators, are omitted. The views alias str, so str must outlive them.

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 the non-empty pieces as std::string_views.
Definition StringUtils.h:329
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 views into str.

◆ SplitAsciiWhitespace()

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

Split at ASCII whitespace: space, tab, line feed, form feed and carriage return (not vertical tab, which IsAsciiWhitespace includes).

Empty tokens are omitted and the returned views alias the input.

Parameters
strString to split, which must outlive the returned views.
Returns
Non-empty whitespace-separated tokens.

◆ 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:207
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 \c StringLike (have size() and data() methods).
Returns
true If str starts with otherStr.

◆ ToLowerAscii() [1/2]

constexpr char donner::StringUtils::ToLowerAscii ( char ch)
inlinestaticconstexpr

Returns ch with ASCII A-Z mapped to a-z; every other byte is returned unchanged, whatever the process C locale.

Parameters
chThe character to lowercase.

◆ ToLowerAscii() [2/2]

template<StringLike T>
std::string donner::StringUtils::ToLowerAscii ( const T & str)
inlinestatic

Returns a copy of str with ASCII A-Z mapped to a-z and every other byte unchanged.

Unlike std::tolower, the result does not depend on the process C locale, so it is safe for lookup keys that must compare equal across locales and for UTF-8 text.

std::string result = StringUtils::ToLowerAscii("Noto SANS");
// result is "noto sans"
static std::string ToLowerAscii(const T &str)
Returns a copy of str with ASCII A-Z mapped to a-z and every other byte unchanged.
Definition StringUtils.h:379
Parameters
strThe string to lowercase.
Template Parameters
TThe type of the string, must be StringLike (have size() and data() methods).
Returns
The ASCII-lowercased copy.

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

The returned view aliases str, so str must outlive it.

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:451
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: