-
Notifications
You must be signed in to change notification settings - Fork 54
/
MaterialMatcher.cpp
52 lines (46 loc) · 1.52 KB
/
MaterialMatcher.cpp
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 "MaterialMatcher.h"
using namespace std;
using namespace DFHack;
using namespace df::enums;
// The main function that checks if two given strings match. The first
// string may contain wildcard characters
bool match(const char *first, const char * second)
{
// If we reach at the end of both strings, we are done
if (*first == '\0' && *second == '\0')
return true;
// Make sure that the characters after '*' are present in second string.
// This function assumes that the first string will not contain two
// consecutive '*'
if (*first == '*' && *(first + 1) != '\0' && *second == '\0')
return false;
// If the first string contains '?', or current characters of both
// strings match
if (*first == '?' || *first == *second)
return match(first + 1, second + 1);
// If there is *, then there are two possibilities
// a) We consider current character of second string
// b) We ignore current character of second string.
if (*first == '*')
return match(first + 1, second) || match(first, second + 1);
return false;
}
int FuzzyCompare(std::string source, std::string target)
{
if (!match(source.c_str(), target.c_str()))
return -1;
int similarity = target.size() - source.size();
for (size_t i = 0; i < source.size(); i++)
{
switch (source[i])
{
case '*':
similarity += 2;
break;
case '?':
similarity += 1;
break;
}
}
return similarity;
}