C/C ++ string fuzzy match

Source: Internet
Author: User

Requirements:

The Access Authorization configuration file is sometimes configured in several dimensions, for example, in the format of "company | product | sys:

1. configuring "Sina | Weibo | pusher" indicates that the pusher System of Weibo products of Sina can be accessed, while "Sina | Weibo | sign" does not allow access.

2. configuring "Sina | * | pusher" indicates that the pusher system of all Sina products can be accessed.

3. configuring "* | pusher" indicates that pusher systems of all products of all companies can be accessed.

...

There are many similar scenarios. Well, simple things won't be broken.

 

Implementation:

In the face of this demand, I first thought about how to design the mode string and how to quickly implement the function, because I am writing a C service, so what I first came to my mind was a bunch of things, such as strchr (XXX, '*'), strchr (XXX, '|') and so on. Later I found that this things didn't have to be made on their own, there are ready-made functions that can be used, that is, fnmatch.

Google once found that there are not many fnmatch materials, most of them are about PhP functions, so there is no way, you can only write your 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 | *"}, // {"Sina | PIC | *"}, {"* | sign"}, {"* | Weibo | sign"} that cannot be matched "}, {"* | 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 ;}}

 

Result:

Experiment, the results are not good, fully meet the needs:

When the requirements are met, I am worried about another problem, that is, performance. comment out the cout output, adjust the while Z statement to 1,000,000, and re-compile and run it:

Time./fnmatch

It seems that the efficiency is not bad. 2.1 matching times were performed in S, with an average of 2 us, and the performance requirements were also met...

 

References: 

Fnmatch source code implementation: http://www.opensource.apple.com/source/sudo/sudo-16/sudo/fnmatch.c

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

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.