0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-01 03:18:54 +01:00
construct/ircd/globular.cc
2020-04-26 17:02:21 -07:00

87 lines
1.9 KiB
C++

// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 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.
//
// globular_iequals
//
bool
ircd::globular_iequals::operator()(const string_view &a, const string_view &b)
const noexcept
{
size_t ap(0), bp(0);
while(ap < a.size() && bp < b.size())
{
const auto ca(tolower(a.at(ap))), cb(tolower(b.at(bp)));
const auto globa(ca == '*'), globb(cb == '*');
const auto wilda(ca == '?'), wildb(cb == '?');
if(!globa && !globb && !wilda && !wildb && ca != cb)
return false;
if((globa && ap + 1 >= a.size()) || (globb && bp + 1 >= b.size()))
break;
if(globa && cb == tolower(a.at(ap + 1)))
ap += 2;
if(globb && ca == tolower(b.at(bp + 1)))
bp += 2;
if(globa && globb)
++ap, ++bp;
if(!globa)
++ap;
if(!globb)
++bp;
}
if(ap < a.size() && !b.empty() && b.back() == '*')
return true;
if(bp < b.size() && !a.empty() && a.back() == '*')
return true;
return iequals(a.substr(ap), b.substr(bp));
}
bool
ircd::globular_imatch::operator()(const string_view &b)
const noexcept
{
size_t ap(0), bp(0);
while(ap < a.size() && bp < b.size())
{
const auto ca(tolower(a.at(ap))), cb(tolower(b.at(bp)));
const auto globa(ca == '*');
const auto wilda(ca == '?');
if(!globa && !wilda && ca != cb)
return false;
if(globa && ap + 1 >= a.size())
break;
if(globa && cb == tolower(a.at(ap + 1)))
ap += 2;
if(!globa)
++ap;
++bp;
}
if(bp < b.size() && !a.empty() && a.back() == '*')
return true;
return iequals(a.substr(ap), b.substr(bp));
}