summaryrefslogtreecommitdiffstats
path: root/src/fts_fuzzy_match.hh
blob: 1d3b01328539fdece37cdb2cac76c7f13f6a9836 (plain)
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
// LICENSE
//
//   This software is dual-licensed to the public domain and under the following
//   license: you are granted a perpetual, irrevocable license to copy, modify,
//   publish, and distribute this file as you see fit.
//
// VERSION
//   0.2.0  (2017-02-18)  Scored matches perform exhaustive search for best
//   score 0.1.0  (2016-03-28)  Initial release
//
// AUTHOR
//   Forrest Smith
//
// NOTES
//   Compiling
//     You MUST add '#define FTS_FUZZY_MATCH_IMPLEMENTATION' before including
//     this header in ONE source file to create implementation.
//
//   fuzzy_match_simple(...)
//     Returns true if each character in pattern is found sequentially within
//     str
//
//   fuzzy_match(...)
//     Returns true if pattern is found AND calculates a score.
//     Performs exhaustive search via recursion to find all possible matches and
//     match with highest score. Scores values have no intrinsic meaning.
//     Possible score range is not normalized and varies with pattern. Recursion
//     is limited internally (default=10) to prevent degenerate cases
//     (pattern="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") Uses uint8_t for
//     match indices. Therefore patterns are limited to 256 characters. Score
//     system should be tuned for YOUR use case. Words, sentences, file names,
//     or method names all prefer different tuning.

#ifndef FTS_FUZZY_MATCH_H
#define FTS_FUZZY_MATCH_H

#include <cstdint>  // uint8_t

// Public interface
namespace fts {
bool fuzzy_match_simple(char const* pattern, char const* str);
bool fuzzy_match(char const* pattern, char const* str, int& outScore);
bool fuzzy_match(char const* pattern,
                 char const* str,
                 int& outScore,
                 uint8_t* matches,
                 int maxMatches);
}  // namespace fts

#endif  // FTS_FUZZY_MATCH_H