salty-ircd/source/ircd/helpers.d

49 lines
921 B
D
Raw Normal View History

2017-03-19 22:43:52 +01:00
module ircd.helpers;
import std.range;
//Based on std.path.globMatch (https://github.com/dlang/phobos/blob/v2.073.2/std/path.d#L3164)
//License: Boost License 1.0 (http://www.boost.org/LICENSE_1_0.txt)
//Copyright (c) Lars T. Kyllingstad, Walter Bright
@safe pure
bool wildcardMatch(string input, string pattern)
{
foreach (ref pi; 0 .. pattern.length)
{
const pc = pattern[pi];
switch (pc)
{
case '*':
if (pi + 1 == pattern.length)
2017-04-10 02:56:31 +02:00
{
2017-03-19 22:43:52 +01:00
return true;
2017-04-10 02:56:31 +02:00
}
2017-03-19 22:43:52 +01:00
for (; !input.empty; input.popFront())
{
auto p = input.save;
if (wildcardMatch(p, pattern[pi + 1 .. pattern.length]))
2017-04-10 02:56:31 +02:00
{
2017-03-19 22:43:52 +01:00
return true;
2017-04-10 02:56:31 +02:00
}
2017-03-19 22:43:52 +01:00
}
return false;
case '?':
if (input.empty)
2017-04-10 02:56:31 +02:00
{
2017-03-19 22:43:52 +01:00
return false;
2017-04-10 02:56:31 +02:00
}
2017-03-19 22:43:52 +01:00
input.popFront();
break;
default:
2017-04-10 02:56:31 +02:00
if (input.empty || pc != input.front)
{
2017-03-19 22:43:52 +01:00
return false;
2017-04-10 02:56:31 +02:00
}
2017-03-19 22:43:52 +01:00
input.popFront();
break;
}
}
return input.empty;
}