0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-28 00:14:07 +01:00

ircd:Ⓜ️:user::highlight: Improve highlight matching quality. (Fixes #122)

This commit is contained in:
Jason Volk 2019-08-07 16:23:55 -07:00
parent 32ec94e29b
commit 3fe4baba4f
2 changed files with 73 additions and 5 deletions

View file

@ -14,14 +14,18 @@
/// Interface to user highlighting and counting. /// Interface to user highlighting and counting.
struct ircd::m::user::highlight struct ircd::m::user::highlight
{ {
m::user user;
static conf::item<bool> enable_count; static conf::item<bool> enable_count;
static conf::item<bool> match_mxid_full; static conf::item<bool> match_mxid_full;
static conf::item<bool> match_mxid_local_cs; static conf::item<bool> match_mxid_local_cs;
static conf::item<bool> match_mxid_local_ci; static conf::item<bool> match_mxid_local_ci;
static conf::item<bool> match_at_room; static conf::item<bool> match_at_room;
static bool imatch(const string_view &text, const string_view &arg);
static bool match(const string_view &text, const string_view &arg);
m::user user;
public:
bool match(const string_view &text) const; bool match(const string_view &text) const;
bool has(const event &) const; bool has(const event &) const;
bool has(const event::idx &) const; bool has(const event::idx &) const;

View file

@ -220,18 +220,82 @@ const
// are true only one branch is taken. // are true only one branch is taken.
if(match_mxid_local_ci) if(match_mxid_local_ci)
{ {
if(ircd::ihas(text, user.user_id.localname())) if(imatch(text, user.user_id.localname()))
return true; return true;
} }
else if(match_mxid_local_cs) else if(match_mxid_local_cs)
{ {
if(ircd::has(text, user.user_id.localname())) if(match(text, user.user_id.localname()))
return true; return true;
} }
if(match_mxid_full) if(match_mxid_full)
if(ircd::has(text, user.user_id)) if(match(text, user.user_id))
return true; return true;
return false; return false;
} }
namespace ircd::m
{
static bool user_highlight_match(const string_view &text, const string_view &arg, const size_t &pos);
}
bool
IRCD_MODULE_EXPORT
ircd::m::user::highlight::match(const string_view &text,
const string_view &arg)
{
const auto pos
{
text.find(arg)
};
return user_highlight_match(text, arg, pos);
}
bool
IRCD_MODULE_EXPORT
ircd::m::user::highlight::imatch(const string_view &text,
const string_view &arg)
{
const auto pos
{
ifind(text, arg)
};
return user_highlight_match(text, arg, pos);
}
bool
ircd::m::user_highlight_match(const string_view &text,
const string_view &arg,
const size_t &pos)
{
static constexpr char sp {'\x20'}, colon {':'};
// no match
if(likely(pos == string_view::npos))
return false;
if(pos == 0)
{
// match is at the beginning of the string
assert(pos + size(arg) <= size(text));
if(pos + size(arg) == size(text))
return true;
return text[pos + size(arg)] == sp || text[pos + size(arg)] == colon;
}
if(pos + size(arg) == size(text))
{
// match is at the end of the string
assert(size(arg) < size(text));
return text[pos - 1] == sp;
}
// test if match surrounded by spaces
assert(size(text) >= size(arg) + 2);
return text[pos - 1] == sp && text[pos] == sp;
}