`
wooce
  • 浏览: 180839 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

字符串类的FindInclude(查找字符串是否包含在一个字符集中的字符)的实现优化

    博客分类:
  • C++
Go 
阅读更多
以下是coremail的cyt所写的实现代码:
int IString::FindInclude(const char * pscCharSet,int nBegin) const
{
if (nBegin>m_length) nBegin=m_length;

int p;
for (;*pscCharSet!='\0';pscCharSet++)
{
p=this->Find(*pscCharSet,nBegin);
if (p!=-1) return p;
}
return -1;
}

是否没优化余地了呢? 非也,我在离开coremail之后,在自己重新实现字符串类的时候在mozilla的代码中找到了同样功能的代码并移植过来了:
static char GetFindInSetFilter(const char *set)
{
    // Calculate filter
    char filter = ~char(0); // All bits set
    while (*set)
    {
        filter &= ~(*set);
        ++set;
    }

    return filter;
}

int TStr::FindCharInSet( const char* aCharSet,int nBegin) const
{
    if ( nBegin<0 )
        nBegin = 0;
    if ( *aCharSet && nBegin>length )
    {
        // Build filter that will be used to filter out characters with
        // bits that none of the terminal chars have. This works very well
        // because searches are often done for chars with only the last
        // 4-6 bits set and normal ascii letters have bit 7 set. Other
        // letters have even higher bits set.

        // Calculate filter
        char filter = GetFindInSetFilter(aCharSet);

        const char* endChar = str + length;
        for (char *charp = str + nBegin; charp < endChar; ++charp)
        {
            char currentChar = *charp;
            // Check if all bits are in the required area
            if (currentChar & filter)
            {
                // They were not. Go on with the next char.
                continue;
            }
            // Test all chars
            const char *charInSet = aCharSet;
            char setChar = *charInSet;
            while (setChar)
            {
                if (setChar == currentChar)
                {
                    // Found it!
                    return charp - str; // The index of the found char
                }
                setChar = *(++charInSet);
            }
        } // end for all chars in the string
    }
    return -1;
}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics