| 31 | |
| 32 | namespace { |
| 33 | struct SubstringCache |
| 34 | { |
| 35 | explicit SubstringCache(const QString& string = QString()) |
| 36 | : substring(string) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | inline int containedIn(const Identifier& id) const |
| 41 | { |
| 42 | int index = id.index(); |
| 43 | QHash<int, int>::const_iterator it = cache.constFind(index); |
| 44 | if (it != cache.constEnd()) { |
| 45 | return *it; |
| 46 | } |
| 47 | |
| 48 | const QString idStr = id.identifier().str(); |
| 49 | |
| 50 | int result = idStr.lastIndexOf(substring, -1, Qt::CaseInsensitive); |
| 51 | if (result < 0 && !idStr.isEmpty() && !substring.isEmpty()) { |
| 52 | // no match; try abbreviations |
| 53 | result = matchesAbbreviation(idStr, substring) ? 0 : -1; |
| 54 | } |
| 55 | |
| 56 | //here we shift the values if the matched string is bigger than the substring, |
| 57 | //so closer matches will appear first |
| 58 | if (result >= 0) { |
| 59 | result = result + (idStr.size() - substring.size()); |
| 60 | } |
| 61 | |
| 62 | cache[index] = result; |
| 63 | |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | QString substring; |
| 68 | mutable QHash<int, int> cache; |
| 69 | }; |
| 70 | |
| 71 | struct ClosestMatchToText |
| 72 | { |