string-fuzzy matching in C + +

Source: Internet
Author: User
Tags include

Demand:

Access authorization profiles are sometimes configured in several dimensions, such as Company|product|sys configuration in this format:

1. Configuration "Sina|weibo|pusher" means that Sina company Weibo product Pusher system can access, and "Sina|weibo|sign" does not allow access

2. Configuration "Sina|*|pusher" means that the pusher system of all products of Sina Company can access

3. The configuration "*|*|pusher" means that all pusher systems of all products of the company are able to access

...

There are a lot of scenes like this, okay, simple things don't pull the eggs.

Realize:

In the face of this demand I think the first time is how to design the pattern string, how to quickly implement the function, because I now write a C service, so I first out of my mind is a lot of strchr (XXX, ' * '), STRCHR (xxx, ' | ') And so on, later found that this thing is not necessary to build their own wheels, there are ready-made functions can be used, that is fnmatch.

Google a bit, found that Fnmatch is not a lot of information, most of them are talking about PHP functions, so there is no way, can only write their own test.

#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|*"},//cannot be matched {"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_st  
            r.at (i), orgin_str, fnm_pathname); if (Fnm_nomatch = = ret) {cout<< ' Sorry I 'M failed ["<< vec_str.at (i) <<"] "<<endl;     
    }} ++z; }  
}

Results:

Experiment one, the results are not bad, fully meet the needs:

The requirements are met, and I'm worried about a problem, which is performance, commenting out the cout output, and then tuning the while Z statement to 1,000,000 and recompiling and running:

Time./fnmatch

Seems to be a good efficiency, 2.1s 100W time matching, the average 2us once, performance requirements are met ...

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.