blob: 998c1fba810d07c6aca93eb32e1b151cf454577c [file] [log] [blame]
Sam McCall87496412017-12-01 17:08:02 +00001//===--- FuzzyMatch.h - Approximate identifier matching ---------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements fuzzy-matching of strings against identifiers.
11// It indicates both the existence and quality of a match:
12// 'eb' matches both 'emplace_back' and 'embed', the former has a better score.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUZZYMATCH_H
17#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FUZZYMATCH_H
18
19#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/raw_ostream.h"
23
24namespace clang {
25namespace clangd {
26
27// A matcher capable of matching and scoring strings against a single pattern.
28// It's optimized for matching against many strings - match() does not allocate.
29class FuzzyMatcher {
30public:
31 // Characters beyond MaxPat are ignored.
32 FuzzyMatcher(llvm::StringRef Pattern);
33
34 // If Word matches the pattern, return a score in [0,1] (higher is better).
35 // Characters beyond MaxWord are ignored.
36 llvm::Optional<float> match(llvm::StringRef Word);
37
38 // Dump internal state from the last match() to the stream, for debugging.
39 // Returns the pattern with [] around matched characters, e.g.
40 // [u_p] + "unique_ptr" --> "[u]nique[_p]tr"
41 llvm::SmallString<256> dumpLast(llvm::raw_ostream &) const;
42
43private:
44 // We truncate the pattern and the word to bound the cost of matching.
45 constexpr static int MaxPat = 63, MaxWord = 127;
Sam McCall8e97cca2017-12-02 03:35:19 +000046 enum CharRole : unsigned char; // For segmentation.
47 enum CharType : unsigned char; // For segmentation.
Sam McCall3ea96402017-12-02 04:15:55 +000048 // Action should be an enum, but this causes bitfield problems:
49 // - for MSVC the enum type must be explicitly unsigned for correctness
50 // - GCC 4.8 complains not all values fit if the type is unsigned
51 using Action = bool;
52 constexpr static Action Miss = false, Match = true;
Sam McCall87496412017-12-01 17:08:02 +000053
54 bool init(llvm::StringRef Word);
55 void buildGraph();
56 void calculateRoles(const char *Text, CharRole *Out, int N);
57 int skipPenalty(int W, Action Last);
58 int matchBonus(int P, int W, Action Last);
59
60 // Pattern data is initialized by the constructor, then constant.
61 char Pat[MaxPat]; // Pattern data
62 int PatN; // Length
63 char LowPat[MaxPat]; // Pattern in lowercase
64 CharRole PatRole[MaxPat]; // Pattern segmentation info
65 bool CaseSensitive; // Case-sensitive match if pattern has uppercase
66 float ScoreScale; // Normalizes scores for the pattern length.
67
68 // Word data is initialized on each call to match(), mostly by init().
69 char Word[MaxWord]; // Word data
70 int WordN; // Length
71 char LowWord[MaxWord]; // Word in lowercase
72 CharRole WordRole[MaxWord]; // Word segmentation info
73 bool WordContainsPattern; // Simple substring check
74
75 // Cumulative best-match score table.
76 // Boundary conditions are filled in by the constructor.
77 // The rest is repopulated for each match(), by buildGraph().
78 struct ScoreInfo {
79 signed int Score : 15;
80 Action Prev : 1;
81 };
82 ScoreInfo Scores[MaxPat + 1][MaxWord + 1][/* Last Action */ 2];
83};
84
85} // namespace clangd
86} // namespace clang
87
88#endif