From 296fd9183be847960c0d71be68a2f47dc3aad665 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 14 Sep 2017 13:01:06 -0700 Subject: [PATCH] ircd::util: Add ctype() template for testing string ranges. --- include/ircd/string_view.h | 8 ++++++++ include/ircd/util.h | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/ircd/string_view.h b/include/ircd/string_view.h index 5e948f102..929dae691 100644 --- a/include/ircd/string_view.h +++ b/include/ircd/string_view.h @@ -32,6 +32,7 @@ namespace ircd template struct vector_view; template struct byte_view; template<> struct byte_view; + template 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 +auto +ircd::ctype(const string_view &s) +{ + return ctype(std::begin(s), std::end(s)); +} diff --git a/include/ircd/util.h b/include/ircd/util.h index feaded6c2..90d72fa59 100644 --- a/include/ircd/util.h +++ b/include/ircd/util.h @@ -1037,5 +1037,20 @@ until(it_a a, } +/// Convenience loop to test std::is* on a character sequence +template +ssize_t +ctype(const char *begin, + const char *const &end) +{ + size_t i(0); + for(; begin != end; ++begin, ++i) + if(!test(static_cast(*begin))) + return i; + + return -1; +} + + } // namespace util } // namespace ircd