0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-02 08:48:18 +02:00

ircd::util: Add ctype() template for testing string ranges.

This commit is contained in:
Jason Volk 2017-09-14 13:01:06 -07:00
parent ccbd507c35
commit 296fd9183b
2 changed files with 23 additions and 0 deletions

View file

@ -32,6 +32,7 @@ namespace ircd
template<class T> struct vector_view;
template<class T = string_view> struct byte_view;
template<> struct byte_view<string_view>;
template<int (&test)(int) = std::isprint> auto ctype(const string_view &s);
bool operator!(const string_view &);
}
@ -241,3 +242,10 @@ ircd::operator!(const string_view &str)
{
return str.empty();
}
template<int (&test)(int)>
auto
ircd::ctype(const string_view &s)
{
return ctype<test>(std::begin(s), std::end(s));
}

View file

@ -1037,5 +1037,20 @@ until(it_a a,
}
/// Convenience loop to test std::is* on a character sequence
template<int (&test)(int) = std::isprint>
ssize_t
ctype(const char *begin,
const char *const &end)
{
size_t i(0);
for(; begin != end; ++begin, ++i)
if(!test(static_cast<unsigned char>(*begin)))
return i;
return -1;
}
} // namespace util
} // namespace ircd