1 // Copyright 2017-2018 ccls Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <limits.h>
7 #include <string>
8 #include <string_view>
9 
10 namespace ccls {
11 class FuzzyMatcher {
12 public:
13   constexpr static int kMaxPat = 100;
14   constexpr static int kMaxText = 200;
15   // Negative but far from INT_MIN so that intermediate results are hard to
16   // overflow.
17   constexpr static int kMinScore = INT_MIN / 4;
18 
19   FuzzyMatcher(std::string_view pattern, int case_sensitivity);
20   int match(std::string_view text, bool strict);
21 
22 private:
23   int case_sensitivity;
24   std::string pat;
25   std::string_view text;
26   int pat_set, text_set;
27   char low_pat[kMaxPat], low_text[kMaxText];
28   int pat_role[kMaxPat], text_role[kMaxText];
29   int dp[2][kMaxText + 1][2];
30 
31   int matchScore(int i, int j, bool last);
32   int missScore(int j, bool last);
33 };
34 } // namespace ccls
35