refactor match_pattern implementation (#102)

* refactor match_pattern implementation

* add match_pattern escaped wildcards support

* add match_pattern global function test
This commit is contained in:
sunxyw
2022-04-28 21:50:48 +08:00
committed by GitHub
parent 0df3fd7bed
commit cd42545c7d
2 changed files with 34 additions and 13 deletions

View File

@@ -99,19 +99,9 @@ function unicode_decode(string $str): ?string
*/
function match_pattern(string $pattern, string $subject): bool
{
if (empty($pattern) && empty($subject)) {
return true;
}
if (mb_strpos($pattern, '*') === 0 && mb_substr($pattern, 1, 1) !== '' && empty($subject)) {
return false;
}
if (mb_strpos($pattern, mb_substr($subject, 0, 1)) === 0) {
return match_pattern(mb_substr($pattern, 1), mb_substr($subject, 1));
}
if (mb_strpos($pattern, '*') === 0) {
return match_pattern(mb_substr($pattern, 1), $subject) || match_pattern($pattern, mb_substr($subject, 1));
}
return false;
$pattern = str_replace(['\*', '\\\\.*'], ['.*', '\*'], preg_quote($pattern, '/'));
$pattern = '/^' . $pattern . '$/i';
return preg_match($pattern, $subject) === 1;
}
/**