准入授權配置採用 company|product|sys 格式,支持通配符 *:
sina|weibo|pusher 精確匹配sina|*|pusher 匹配該公司所有產品的 pusher*|*|pusher 匹配所有公司所有產品的 pusher避免手寫 strchr 解析,直接使用 POSIX fnmatch(<fnmatch.h>)。
#include <iostream>
#include <fnmatch.h>
#include <vector>
using namespace std;
int main() {
const char* origin = "sina|weibo|pusher";
char patterns[][20] = {
"sina|*|pusher", "sina|*|*", "*|weibo|*",
"sina|pic|*", "*|*|sign", "*|weibo|sign",
"*|pic|sign", "sina|pic|sign", "*|*|*"
};
vector<char*> vec;
for (auto &p : patterns) vec.push_back(p);
for (auto pat : vec) {
int ret = fnmatch(pat, origin, FNM_PATHNAME);
if (ret == FNM_NOMATCH)
cout << "failed [" << pat << "]" << endl;
else
cout << "matched [" << pat << "]" << endl;
}
}
100 萬次匹配耗時 2.1 秒,平均 2μs/次,滿足高性能需求。