0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-14 00:34:18 +01:00

ircd: Add stringops for counting starting and ending occurrences.

This commit is contained in:
Jason Volk 2017-10-03 04:00:57 -07:00
parent 8c4925419a
commit 0399ddf3e3

View file

@ -68,11 +68,17 @@ namespace ircd
bool endswith(const string_view &str, const char &val);
template<class It> bool endswith_any(const string_view &str, const It &begin, const It &end);
// count occurrences of val at end of string
size_t endswith_count(const string_view &str, const char &val);
// test string startswith delim; or any of the delims in iterable
bool startswith(const string_view &str, const string_view &val);
bool startswith(const string_view &str, const char &val);
template<class It> bool startswith_any(const string_view &str, const It &begin, const It &end);
// count occurrences of val at start of string
size_t startswith_count(const string_view &str, const char &val);
// test string is surrounded by val (ex. surrounded by quote characters)
bool surrounds(const string_view &str, const string_view &val);
bool surrounds(const string_view &str, const char &val);
@ -191,6 +197,15 @@ ircd::surrounds(const string_view &str,
return startswith(str, val) && endswith(str, val);
}
/// Count occurrences of val at end of string
inline size_t
ircd::startswith_count(const string_view &str,
const char &v)
{
const auto pos(str.find_first_not_of(v));
return pos == string_view::npos? str.size() : str.size() - pos - 1;
}
/// Test if a string starts with any of the values in the iterable
template<class It>
bool
@ -221,6 +236,15 @@ ircd::startswith(const string_view &str,
return pos == 0;
}
/// Count occurrences of val at end of string
inline size_t
ircd::endswith_count(const string_view &str,
const char &v)
{
const auto pos(str.find_last_not_of(v));
return pos == string_view::npos? str.size() : str.size() - pos - 1;
}
/// Test if a string ends with any of the values in iterable
template<class It>
bool