C/C++ 字符串模糊匹配

需求:

  准入授权配置文件有时候分了好几个维度进行配置,例如 company|product|sys这种格式的配置:

1.配置 "sina|weibo|pusher" 表示 sina公司weibo产品pusher系统能够准入,而"sina|weibo|sign"不允许准入

2.配置 "sina|*|pusher” 表示sina公司所有产品的pusher系统都能够准入

3.配置 “*|*|pusher” 表示所有公司的所有产品的pusher系统都能够准入

  …

  类似还有很多场景,好了,简单的东西不扯蛋了.

实现:

  面对这个需求我第一时间想的是如何设计模式串,如何快速实现功能,因为我现在写的是一个C服务,所以我首先出现在我脑海的是一大堆strchr(XXX, ‘*’), strchr(XXX, ‘|’)等等东西,后面发现这个东西没有必要自己造轮子,有现成的函数可以用,那就是fnmatch.

  google了一下,发现fnmatch的资料并不是很多,大部分还都是讲php函数的,所以没办法,只能自己写写测测了.

#include <iostream>
#include <fnmatch.h>
#include <vector>
using namespace std;

int main()
{
    const char* orgin_str = "sina|weibo|pusher";
    char pattern_arr[][20] = {
        {"sina|*|pusher"},
        {"sina|*|*"},
        {"*|weibo|*"},
        //不能被匹配的
        {"sina|pic|*"},
        {"*|*|sign"},
        {"*|weibo|sign"},
        {"*|pic|sign"},
        {"sina|pic|sign"},

        {"*|*|*"}
    };
    static int pattern_arr_size = sizeof(pattern_arr) / sizeof(pattern_arr[0]);

    vector<char *> vec_str;
    for(int i = 0; i < pattern_arr_size; i ++)
    {
        vec_str.push_back(pattern_arr[i]);
    }

    int ret;
    int z = 0;
    while(z < 1){
        for(int i = 0; i < vec_str.size(); i++)
        {       
            ret = fnmatch(vec_str.at(i), orgin_str, FNM_PATHNAME);
            if(FNM_NOMATCH == ret){
                cout<<"sorry I'm failed ["<< vec_str.at(i) <<"]"<<endl;
            }       
        }       
        ++z;    
    }
}

结果:   

  实验一把,结果还不赖,完全满足需求:

  

  需求满足了,我担心的还有一个问题,那就是性能,注释掉cout输出,将while z语句调至1,000,000,重新编译跑一下:

  time ./fnmatch

看来效率还不错,2.1s 进行了100W次匹配,平均2us一次,性能要求也满足了...

参考文献: 

fnmatch源码实现:  http://www.opensource.apple.com/source/sudo/sudo-16/sudo/fnmatch.c

MAN:   http://linux.die.net/man/3/fnmatch


博客地址:Zealot Yin

欢迎转载,转载请注明出处[http://creator.cnblogs.com/]

原文地址:https://www.cnblogs.com/Creator/p/2981186.html