字符串通配符匹配算法

/**
 * match char
 *
 * @param c1 first character
 * @param c2 second character
 * @param ignoreCase ignore case
 * @return true means match, otherwise not match
 */
bool MatchChar(char c1, char c2, bool ignoreCase)
{
    return c1 == c2 || (ignoreCase && tolower(c1) == tolower(c2));
}

/**
 * match file name
 * <p>
 *    support special character
 *    * : any character
 * </p>
 *
 * @param filename file name
 * @param pattern pattern string
 * @param ignoreCase ignore case
 * @return
 */
bool WildCharMatch(IN const char* src, IN const char* pattern, IN const bool ignoreCase)
{
    if (!src || !pattern)
    {
        return false;
    }

    bool result;
    while (*src)
    {
        if (*pattern == '*')
        {
            // If pattern current character '*'
            // If there are multiple '*', skip over
            while ((*pattern == '*') || (*pattern == '?'))
            {
                pattern++;
            }

            // If there is no character after '*', then match correctly.
            if (!*pattern)
            {
                return true;
            }

            while (*src)
            {
                // find a character in src that is identical to the character in pattern
                if (MatchChar(*src, *pattern, ignoreCase))
                {
                    // If found, match the remaining string.
                    result = WildCharMatch(src, pattern, ignoreCase);
                    if (result)
                    {
                        return true;
                    }
                }
                src++;
            }
            return false;
        }
        else
        {
            // If the current character in pattern is not '*'
            // Match current characters
            if (MatchChar(*src, *pattern, ignoreCase) || ('?' == *pattern))
            {
                // SRC, pattern advance one, continue to match.
                return WildCharMatch(++src, ++pattern, ignoreCase);
            }
            
            return false;
        }
    }

    // If src is over, see if pattern ends.
    if (*pattern)
    {
        // pattern didn't end
        // If pattern has the last character and is '*'
        if (*pattern == '*' && *(pattern + 1) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值