📄 patternmatch.cpp
字号:
#include "proxymain.h"
#include "string.h"
// macro to convert single ASCII char to lowercase
#define CHARLWR(x) ((((x >= 65) && (x <= 90)) || ((x >= 192) && (x <= 220))) ? unsigned char(x+32) : x)
bool patternmatch(const char* str, const char* strpat)
{
UINT i=0,j=0;
UINT ilen, jlen;
UINT beginchar=-1;
char rfrom, rto;
ilen = strlen(str);
jlen = strlen(strpat);
// begin check
while (true)
{
// wildcard?
switch(strpat[j])
{
case '?':
i++;
j++;
break;
case '*':
// if * is last char in pattern return true
if (j == (jlen-1)) return true;
// otherwise try to find next sequence in source string
// which begins with the next char in pattern string
j++;
for (;i<ilen;i++)
{
if (str[i] == strpat[j]) break;
}
// if i = ilen -> char not found -> return false
if (i == ilen) return false;
// now we go on comparing all bytes but if the following sequences
// do NOT match at a certain point we try finding the next beginning
beginchar = j;
break;
case '[':
// possibly beginning character range
// range is in format [{from}-{to}].
// e.g. [0-9]
// if [ appears twice it's NOT the beginning of the range but the
// character '[' as such. if it appears three time it is a range
// beginning with '['.
if (strpat[j+1] == '[')
{
// it is already two ['s, maybe three?
if (strpat[j+2] == '[')
{
// it even three ['s
// set [ as start of range
rfrom = '[';
j++;
}
else
{
// no, only two... perform normal check
j++;
goto jcompare;
}
}
else
// extract 'from' char
rfrom = strpat[j+1];
// extract 'to' char
rto = strpat[j+3];
// check if source char is NOT in this range
if ((str[i] < rfrom) || (str[i] > rto))
return false;
// skip range part
j += 5;
i++;
//rfrom = rfrom;
break;
default:
jcompare:
// compare bytes
if (CHARLWR(str[i]) != CHARLWR(strpat[j]))
{
// check if currently in '*' wildcard mode
if (beginchar == -1)
return false;
else
{
// ok this was no correct sequence...
// search next with same beginning char
for (;i<ilen;i++)
{
if (str[i] == strpat[beginchar]) break;
}
// if i = ilen -> char not found -> return false
if (i == ilen) return false;
// ok now go on trying this sequence
// j must be set back to beginchar (-1 as a j++ will follow below)
j = beginchar-1;
// also do a i-- as i needs to stay the same
i--;
}
}
i++;
j++;
}
// if end of only one string reached -> no match
// if both ends are reached -> match
if ((i >= ilen) && (j >= jlen)) return true;
if (i >= ilen) return false;
if (j >= jlen) return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -