📄 mstring.c
字号:
if(m_cnt == 0)
{
b_idx++;
}
else
{
b_idx = b_idx - (m_cnt - 1);
}
p_idx = pat;
m_cnt = 0;
}
} while(b_idx < b_end);
/* if we make it here we didn't find what we were looking for */
return 0;
}
/****************************************************************
*
* Function: make_skip(char *, int)
*
* Purpose: Create a Boyer-Moore skip table for a given pattern
*
* Parameters:
* ptrn => pattern
* plen => length of the data in the pattern buffer
*
* Returns:
* int * - the skip table
*
****************************************************************/
int *make_skip(char *ptrn, int plen)
{
int *skip = (int *) malloc(256 * sizeof(int));
int *sptr = &skip[256];
while(sptr-- != skip)
*sptr = plen + 1;
while(plen != 0)
skip[(unsigned char) *ptrn++] = plen--;
return skip;
}
/****************************************************************
*
* Function: make_shift(char *, int)
*
* Purpose: Create a Boyer-Moore shift table for a given pattern
*
* Parameters:
* ptrn => pattern
* plen => length of the data in the pattern buffer
*
* Returns:
* int * - the shift table
*
****************************************************************/
int *make_shift(char *ptrn, int plen)
{
int *shift = (int *) malloc(plen * sizeof(int));
int *sptr = shift + plen - 1;
char *pptr = ptrn + plen - 1;
char c = ptrn[plen - 1];
*sptr = 1;
while(sptr-- != shift)
{
char *p1 = ptrn + plen - 2, *p2, *p3;
do
{
while(p1 >= ptrn && *p1-- != c);
p2 = ptrn + plen - 2;
p3 = p1;
while(p3 >= ptrn && *p3-- == *p2-- && p2 >= pptr);
}
while(p3 >= ptrn && p2 >= pptr);
*sptr = shift + plen - sptr + p2 - p3;
pptr--;
}
return shift;
}
/****************************************************************
*
* Function: mSearch(char *, int, char *, int)
*
* Purpose: Determines if a string contains a (non-regex)
* substring.
*
* Parameters:
* buf => data buffer we want to find the data in
* blen => data buffer length
* ptrn => pattern to find
* plen => length of the data in the pattern buffer
* skip => the B-M skip array
* shift => the B-M shift array
*
* Returns:
* Integer value, 1 on success (str constains substr), 0 on
* failure (substr not in str)
*
****************************************************************/
int mSearch(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift)
{
int b_idx = plen;
#ifdef DEBUG
int cmpcnt = 0;
#endif
if(plen == 0)
return 1;
while(b_idx <= blen)
{
int p_idx = plen, skip_stride, shift_stride;
while(buf[--b_idx] == ptrn[--p_idx])
{
#ifdef DEBUG
cmpcnt++;
#endif
if(p_idx == 0)
{
#ifdef DEBUG
fprintf(stdout, "match: compares = %d.\n", cmpcnt);
#endif
return 1;
}
}
skip_stride = skip[(unsigned char) buf[b_idx]];
shift_stride = shift[p_idx];
b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;
}
#ifdef DEBUG
fprintf(stdout, "no match: compares = %d.\n", cmpcnt);
#endif
return 0;
}
/****************************************************************
*
* Function: mSearchCI(char *, int, char *, int)
*
* Purpose: Determines if a string contains a (non-regex)
* substring matching is case insensitive
*
* Parameters:
* buf => data buffer we want to find the data in
* blen => data buffer length
* ptrn => pattern to find
* plen => length of the data in the pattern buffer
* skip => the B-M skip array
* shift => the B-M shift array
*
* Returns:
* Integer value, 1 on success (str constains substr), 0 on
* failure (substr not in str)
*
****************************************************************/
int mSearchCI(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift)
{
int b_idx = plen;
#ifdef DEBUG
int cmpcnt = 0;
#endif
if(plen == 0)
return 1;
while(b_idx <= blen)
{
int p_idx = plen, skip_stride, shift_stride;
while((unsigned char) ptrn[--p_idx] == toupper((unsigned char) buf[--b_idx]))
{
#ifdef DEBUG
cmpcnt++;
#endif
if(p_idx == 0)
{
#ifdef DEBUG
fprintf(stdout, "match: compares = %d.\n", cmpcnt);
#endif
return 1;
}
}
skip_stride = skip[toupper((unsigned char) buf[b_idx])];
shift_stride = shift[p_idx];
b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;
}
#ifdef DEBUG
fprintf(stdout, "no match: compares = %d.\n", cmpcnt);
#endif
return 0;
}
/****************************************************************
*
* Function: mSearchREG(char *, int, char *, int)
*
* Purpose: Determines if a string contains a (regex)
* substring.
*
* Parameters:
* buf => data buffer we want to find the data in
* blen => data buffer length
* ptrn => pattern to find
* plen => length of the data in the pattern buffer
* skip => the B-M skip array
* shift => the B-M shift array
*
* Returns:
* Integer value, 1 on success (str constains substr), 0 on
* failure (substr not in str)
*
****************************************************************/
int mSearchREG(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift)
{
int b_idx = plen;
int literal = 0;
#ifdef DEBUG
int cmpcnt = 0;
#endif
if(plen == 0)
return 1;
while(b_idx <= blen)
{
int p_idx = plen, skip_stride, shift_stride;
while(buf[--b_idx] == ptrn[--p_idx]
|| (ptrn[p_idx] == '?' && !literal)
|| (ptrn[p_idx] == '*' && !literal)
|| (ptrn[p_idx] == '\\' && !literal))
{
#ifdef DEBUG
cmpcnt++;
#endif
if(literal)
literal = 0;
if(!literal && ptrn[p_idx] == '\\')
literal = 1;
if(ptrn[p_idx] == '*')
{
while(p_idx != 0 && ptrn[--p_idx] == '*'); /* fool-proof */
if(p_idx == 0)
{
#ifdef DEBUG
fprintf(stdout, "match: compares = %d.\n", cmpcnt);
#endif
return 1;
}
while(b_idx != 0)
{
int ret = 0;
/*
* *ooooooch* here we go recurent.. I smell lots of
* headaches here. The algorytm is simple though: if
* substring which we haven't matched matches
* subsctring-to-match before asterick we return `match'
* otherwise we consider non-matched symbol a part of
* `*', move one symbol left and try to match it again.
*/
if(ret == (mSearchREG(&buf[b_idx--], b_idx,
&ptrn[p_idx], p_idx,
skip, &shift[p_idx])) != 0)
{
printf("recurs: %i\n", ret);
return ret;
}
}
}
if(p_idx == 0)
{
#ifdef DEBUG
fprintf(stdout, "match: compares = %d.\n", cmpcnt);
#endif
return 1;
}
if(b_idx == 0)
break;
}
skip_stride = skip[(unsigned char) buf[b_idx]];
shift_stride = shift[p_idx];
b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;
}
#ifdef DEBUG
fprintf(stdout, "no match: compares = %d.\n", cmpcnt);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -