Algorithm_001_字节匹配_暴力


    static INT MatchBytesViolent(IN const UCHAR* pText, IN INT iTextBytes, IN const UCHAR* pPattern, IN INT iPatternBytes, IN BOOL bCheckParameter = TRUE); // 内存字节匹配;暴力匹配(步长1递增逐个比较);

INT DiySystem::MatchBytesViolent(IN const UCHAR* pText, IN INT iTextBytes, IN const UCHAR* pPattern, IN INT iPatternBytes, IN BOOL bCheckParameter /*= TRUE*/)
{
    //需要挨个比较所有字符

    if (bCheckParameter)
    {
        if (!pText || iTextBytes <= 0 || !pPattern || iPatternBytes <= 0 || iPatternBytes > iTextBytes || IsBadReadPtr(pText, iTextBytes) || IsBadReadPtr(pPattern, iPatternBytes))
        {
            return -1;
        }
    }

    for (INT i = 0, j = 0; i < iTextBytes - iPatternBytes; i++)
    {
        j = 0;
        for (INT k = 0; k < iPatternBytes; k++)
        {
            if (pText[i + k] != pPattern[k])
            {
                break;
            }
            j++;
        }
        if (iPatternBytes == j)
        {
            return i;
        }
    }
    return -1;
}

猜你喜欢

转载自www.cnblogs.com/dailycode/p/9643774.html