Move glob to hpp with GLOB_IMPLEMENTATION

This commit is contained in:
nikola 2024-07-02 12:03:36 +02:00
parent a01cfd8f25
commit bddcd66e28
3 changed files with 125 additions and 121 deletions

View File

@ -13,4 +13,125 @@ enum class Glob_Result
// https://github.com/tsoding/glob.h
Glob_Result glob(const char *pattern, const char *text);
#endif
#endif // GLOB_HPP
#ifdef GLOB_IMPLEMENTATION
Glob_Result glob(const char *pattern, const char *text)
{
while (*pattern != '\0' && *text != '\0')
{
switch (*pattern)
{
case '?':
{
pattern += 1;
text += 1;
}
break;
case '*':
{
Glob_Result result = glob(pattern + 1, text);
if (result != Glob_Result::UNMATCHED)
return result;
text += 1;
}
break;
case '[':
{
bool matched = false;
bool negate = false;
pattern += 1; // skipping [
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unclosed [
if (*pattern == '!')
{
negate = true;
pattern += 1;
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unclosed [
}
char prev = *pattern;
matched |= prev == *text;
pattern += 1;
while (*pattern != ']' && *pattern != '\0')
{
switch (*pattern)
{
case '-':
{
pattern += 1;
switch (*pattern)
{
case ']':
matched |= '-' == *text;
break;
case '\0':
return Glob_Result::SYNTAX_ERROR; // unclosed [
default:
{
matched |= prev <= *text && *text <= *pattern;
prev = *pattern;
pattern += 1;
}
}
}
break;
default:
{
prev = *pattern;
matched |= prev == *text;
pattern += 1;
}
}
}
if (*pattern != ']')
return Glob_Result::SYNTAX_ERROR; // unclosed [
if (negate)
matched = !matched;
if (!matched)
return Glob_Result::UNMATCHED;
pattern += 1;
text += 1;
}
break;
case '\\':
pattern += 1;
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unfinished escape
// fallthrough
default:
{
if (*pattern == *text)
{
pattern += 1;
text += 1;
}
else
{
return Glob_Result::UNMATCHED;
}
}
}
}
if (*text == '\0')
{
while (*pattern == '*')
pattern += 1;
if (*pattern == '\0')
return Glob_Result::MATCHED;
}
return Glob_Result::UNMATCHED;
}
#endif // GLOB_IMPLEMENTATION

View File

@ -1,9 +1,11 @@
#include <cstring>
#include "func.hpp"
#include "glob.hpp"
#include "buffer.hpp"
#define GLOB_IMPLEMENTATION
#include "glob.hpp"
#define CRYPTORAND_IMPLEMENTATION
#include "cryptorand.hpp"

View File

@ -1,119 +0,0 @@
#include "glob.hpp"
Glob_Result glob(const char *pattern, const char *text)
{
while (*pattern != '\0' && *text != '\0')
{
switch (*pattern)
{
case '?':
{
pattern += 1;
text += 1;
}
break;
case '*':
{
Glob_Result result = glob(pattern + 1, text);
if (result != Glob_Result::UNMATCHED)
return result;
text += 1;
}
break;
case '[':
{
bool matched = false;
bool negate = false;
pattern += 1; // skipping [
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unclosed [
if (*pattern == '!')
{
negate = true;
pattern += 1;
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unclosed [
}
char prev = *pattern;
matched |= prev == *text;
pattern += 1;
while (*pattern != ']' && *pattern != '\0')
{
switch (*pattern)
{
case '-':
{
pattern += 1;
switch (*pattern)
{
case ']':
matched |= '-' == *text;
break;
case '\0':
return Glob_Result::SYNTAX_ERROR; // unclosed [
default:
{
matched |= prev <= *text && *text <= *pattern;
prev = *pattern;
pattern += 1;
}
}
}
break;
default:
{
prev = *pattern;
matched |= prev == *text;
pattern += 1;
}
}
}
if (*pattern != ']')
return Glob_Result::SYNTAX_ERROR; // unclosed [
if (negate)
matched = !matched;
if (!matched)
return Glob_Result::UNMATCHED;
pattern += 1;
text += 1;
}
break;
case '\\':
pattern += 1;
if (*pattern == '\0')
return Glob_Result::SYNTAX_ERROR; // unfinished escape
// fallthrough
default:
{
if (*pattern == *text)
{
pattern += 1;
text += 1;
}
else
{
return Glob_Result::UNMATCHED;
}
}
}
}
if (*text == '\0')
{
while (*pattern == '*')
pattern += 1;
if (*pattern == '\0')
return Glob_Result::MATCHED;
}
return Glob_Result::UNMATCHED;
}