-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutil.cpp
More file actions
52 lines (43 loc) · 1.34 KB
/
stringutil.cpp
File metadata and controls
52 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stringutil.h"
#include <iostream>
#include <string>
#include <regex>
#include <QDebug>
#include <QList>
#include <QString>
using namespace std;
std::regex StringUtil::wildchardRex(string pattern){
std::string regstr = pattern;
replaceAll(regstr,"\\","\\\\");
replaceAll(regstr,".","\\.");
replaceAll(regstr,"^","\\^");
replaceAll(regstr,"$","\\$");
replaceAll(regstr,"*","[\\w\\W]*");
return std::regex(regstr);
}
bool StringUtil::matchWildcard(QString pattern, QString parentString) {
QList<std::regex> regList;
QList<QString> filters = pattern.split(";");
for(QString filter: filters){
regList.append(StringUtil::wildchardRex(filter.toStdString()));
}
return StringUtil::match(regList,parentString);
}
bool StringUtil::match(const QList<std::regex> ®s,QString parentString){
std::string name = parentString.toLower().toStdString();
for(auto reg: regs){
if(std::regex_search(name,reg)){
return true;
}
}
return false;
}
string StringUtil::replaceAll(string &str, const string& oldStr, const string& newStr){
string::size_type pos = str.find(oldStr);
if(oldStr.empty()) return str;
while(pos != string::npos){
str.replace(pos, oldStr.length(), newStr);
pos = str.find(oldStr,pos + newStr.length());
}
return str;
}