diff --git a/include/ircd/util/blackwhite.h b/include/ircd/util/blackwhite.h new file mode 100644 index 000000000..ccca77cbf --- /dev/null +++ b/include/ircd/util/blackwhite.h @@ -0,0 +1,55 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2023 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_UTIL_BLACKWHITE_H + +namespace ircd { inline namespace util +{ + namespace blackwhite + { + struct list; + } +}} + +class ircd::util::blackwhite::list +{ + string_view black; + string_view white; + char delim; + + bool match(const string_view &) const; + + public: + bool operator()(const string_view &) const; + + list(const char delim = ' ', + const string_view &black = {}, + const string_view &white = {}); +}; + +inline +ircd::util::blackwhite::list::list(const char delim, + const string_view &black, + const string_view &white) +:black{black} +,white{white} +,delim{delim} +{} + +inline bool +ircd::util::blackwhite::list::operator()(const string_view &s) +const +{ + if(!this->black && !this->white) + return true; + + return match(s); +} diff --git a/include/ircd/util/util.h b/include/ircd/util/util.h index 633c2bdd3..dfd0229e1 100644 --- a/include/ircd/util/util.h +++ b/include/ircd/util/util.h @@ -67,6 +67,7 @@ namespace ircd #include "all.h" #include "compare_exchange.h" #include "bitset.h" +#include "blackwhite.h" // Unsorted section namespace ircd { diff --git a/ircd/util.cc b/ircd/util.cc index 01ab8718c..07c5591c8 100644 --- a/ircd/util.cc +++ b/ircd/util.cc @@ -23,6 +23,40 @@ ircd::util::size(std::ostream &s) return ret; } +/////////////////////////////////////////////////////////////////////////////// +// +// util/blackwhite.h +// + +bool +ircd::util::blackwhite::list::match(const string_view &a) +const +{ + const auto matcher + { + [&a](const string_view &b) noexcept + { + return a != b; + } + }; + + const bool black + { + this->black && !this->white? + !tokens(this->black, delim, matcher): + false + }; + + const bool white + { + !black && this->white? + !tokens(this->white, delim, matcher): + false + }; + + return !black && (white || !this->white); +} + /////////////////////////////////////////////////////////////////////////////// // // util/env.h