From 7eff71b9a365df1074035210ee3a35ae6def6c03 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 3 Mar 2018 02:34:42 -0800 Subject: [PATCH] ircd::m: Add function to check if and only if id is a valid localpart. --- include/ircd/m/id.h | 1 + ircd/m/id.cc | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/ircd/m/id.h b/include/ircd/m/id.h index 9e5ca661f..91f2f3bf4 100644 --- a/include/ircd/m/id.h +++ b/include/ircd/m/id.h @@ -207,6 +207,7 @@ namespace ircd::m // Checks bool valid(const id::sigil &, const string_view &) noexcept; bool valid_local(const id::sigil &, const string_view &); // Local part is valid + bool valid_local_only(const id::sigil &, const string_view &) noexcept; // No :host void validate(const id::sigil &, const string_view &); // valid() | throws } diff --git a/ircd/m/id.cc b/ircd/m/id.cc index 00ce9cf4f..3b4ea0a75 100644 --- a/ircd/m/id.cc +++ b/ircd/m/id.cc @@ -30,6 +30,7 @@ namespace ircd::m using qi::raw; using qi::attr; using qi::eps; + using qi::eoi; using qi::attr_cast; using karma::lit; @@ -546,6 +547,24 @@ catch(...) return false; } +bool +ircd::m::valid_local_only(const id::sigil &sigil, + const string_view &id) +noexcept try +{ + static const auto test + { + &lit(char(sigil)) > &m::id::parser.prefix > &eoi + }; + + const char *start{begin(id)}; + return qi::parse(start, end(id), test); +} +catch(...) +{ + return false; +} + bool ircd::m::valid_local(const id::sigil &sigil, const string_view &id) @@ -556,8 +575,7 @@ ircd::m::valid_local(const id::sigil &sigil, }; const char *start{id.data()}; - const char *const stop{id.data() + id.size()}; - return qi::parse(start, stop, test); + return qi::parse(start, end(id), test); } bool