ircd::util: Add blackwhite::list tool.
This commit is contained in:
parent
ab7dd15618
commit
91fafda7bb
|
@ -0,0 +1,55 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2023 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// 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);
|
||||
}
|
|
@ -67,6 +67,7 @@ namespace ircd
|
|||
#include "all.h"
|
||||
#include "compare_exchange.h"
|
||||
#include "bitset.h"
|
||||
#include "blackwhite.h"
|
||||
|
||||
// Unsorted section
|
||||
namespace ircd {
|
||||
|
|
34
ircd/util.cc
34
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
|
||||
|
|
Loading…
Reference in New Issue