Fix g++ build

This commit is contained in:
Fangrui Song 2018-03-16 08:28:37 -07:00
parent b2b5e57761
commit aa2910208f
2 changed files with 13 additions and 14 deletions

View File

@ -3,11 +3,11 @@
#include <ctype.h> #include <ctype.h>
#include <algorithm> #include <algorithm>
enum FuzzyMatcher::CharClass : int { Other, Lower, Upper }; enum CharClass { Other, Lower, Upper };
enum FuzzyMatcher::CharRole : int { None, Tail, Head }; enum CharRole { None, Tail, Head };
namespace { namespace {
FuzzyMatcher::CharClass GetCharClass(int c) { CharClass GetCharClass(int c) {
if (islower(c)) if (islower(c))
return Lower; return Lower;
if (isupper(c)) if (isupper(c))
@ -15,14 +15,12 @@ FuzzyMatcher::CharClass GetCharClass(int c) {
return Other; return Other;
} }
void CalculateRoles(std::string_view s, void CalculateRoles(std::string_view s, int roles[], int* class_set) {
FuzzyMatcher::CharRole roles[],
int* class_set) {
if (s.empty()) { if (s.empty()) {
*class_set = 0; *class_set = 0;
return; return;
} }
FuzzyMatcher::CharClass pre = Other, cur = GetCharClass(s[0]), suc; CharClass pre = Other, cur = GetCharClass(s[0]), suc;
*class_set = 1 << cur; *class_set = 1 << cur;
auto fn = [&]() { auto fn = [&]() {
if (cur == Other) if (cur == Other)
@ -52,8 +50,11 @@ int FuzzyMatcher::MissScore(int j, bool last) {
int FuzzyMatcher::MatchScore(int i, int j, bool last) { int FuzzyMatcher::MatchScore(int i, int j, bool last) {
int s = 40; int s = 40;
if ((pat[i] == text[j] && ((pat_set & 1 << Upper) || i == j))) if (pat[i] == text[j]) {
s += 20; s++;
if ((pat_set & 1 << Upper) || i == j)
s += 20;
}
if (pat_role[i] == Head && text_role[j] == Head) if (pat_role[i] == Head && text_role[j] == Head)
s += 50; s += 50;
if (text_role[j] == Tail && i && !last) if (text_role[j] == Tail && i && !last)
@ -93,7 +94,7 @@ int FuzzyMatcher::Match(std::string_view text) {
} }
for (int i = 0; i < int(pat.size()); i++) { for (int i = 0; i < int(pat.size()); i++) {
int(*pre)[2] = dp[i & 1]; int(*pre)[2] = dp[i & 1];
int(*cur)[2] = dp[i + 1 & 1]; int(*cur)[2] = dp[(i + 1) & 1];
cur[0][0] = cur[0][1] = kMinScore; cur[0][0] = cur[0][1] = kMinScore;
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
cur[j + 1][0] = std::max(cur[j][0] + MissScore(j, false), cur[j + 1][0] = std::max(cur[j][0] + MissScore(j, false),

View File

@ -3,6 +3,7 @@
#include <string_view.h> #include <string_view.h>
#include <limits.h> #include <limits.h>
#include <string>
class FuzzyMatcher { class FuzzyMatcher {
public: public:
@ -15,15 +16,12 @@ public:
FuzzyMatcher(std::string_view pattern); FuzzyMatcher(std::string_view pattern);
int Match(std::string_view text); int Match(std::string_view text);
enum CharClass : int;
enum CharRole : int;
private: private:
std::string pat; std::string pat;
std::string_view text; std::string_view text;
int pat_set, text_set; int pat_set, text_set;
char low_pat[kMaxPat], low_text[kMaxText]; char low_pat[kMaxPat], low_text[kMaxText];
CharRole pat_role[kMaxPat], text_role[kMaxText]; int pat_role[kMaxPat], text_role[kMaxText];
int dp[2][kMaxText + 1][2]; int dp[2][kMaxText + 1][2];
int MatchScore(int i, int j, bool last); int MatchScore(int i, int j, bool last);