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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
//===--- FuzzyStringMatcher.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/IDE/FuzzyStringMatcher.h"
#include "clang/Basic/CharInfo.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
using namespace swift;
using namespace swift::ide;
using clang::toUppercase;
using clang::toLowercase;
using clang::isUppercase;
using clang::isLowercase;
FuzzyStringMatcher::FuzzyStringMatcher(StringRef pattern_)
: pattern(pattern_), charactersInPattern(1 << (sizeof(char) * 8)) {
lowercasePattern.reserve(pattern.size());
unsigned upperCharCount = 0;
for (char c : pattern) {
char lower = toLowercase(c);
upperCharCount += (c == lower) ? 0 : 1;
lowercasePattern.push_back(lower);
charactersInPattern.set(static_cast<unsigned char>(lower));
charactersInPattern.set(static_cast<unsigned char>(toUppercase(c)));
}
assert(pattern.size() == lowercasePattern.size());
// FIXME: pull out the magic constants.
// This depends on the inner details of the matching algorithm and will need
// to be updated if we substantially alter it.
if (pattern.size() == 1) {
maxScore = 3.0 + // uppercase match
0.001; // size bonus
} else {
maxScore = 0.25 + // percent match bonus
2.5 + // match at start bonus
pattern.size() * pattern.size(); // max run length score
if (upperCharCount) // max uppercase match score
maxScore += (upperCharCount + 1) * (upperCharCount + 1);
maxScore *= 1.1 * 2.5 * 1.2; // exact prefix match bonus
}
}
bool FuzzyStringMatcher::matchesCandidate(StringRef candidate) const {
unsigned patternLength = pattern.size();
unsigned candidateLength = candidate.size();
if (patternLength > candidateLength)
return false;
// Do all of the pattern characters match the candidate in order?
unsigned pidx = 0, cidx = 0;
while (pidx < patternLength && cidx < candidateLength) {
char c = candidate[cidx];
char p = lowercasePattern[pidx];
if (p == c || p == toLowercase(c))
++pidx;
++cidx;
}
return pidx == patternLength;
}
static bool isTokenizingChar(char c) {
switch (c) {
case '/':
case '.':
case '_':
case '+':
case '-':
case ':':
case ',':
case ' ':
case '(':
case ')':
case '!':
case '?':
return true;
default:
return false;
}
}
namespace {
/// A simple index range.
struct Range {
unsigned location;
unsigned length;
};
} // end anonymous namespace
static void
populateTokenTable(SmallVectorImpl<Range> &tokens,
llvm::MutableArrayRef<unsigned> characterToTokenIndex,
StringRef candidate) {
unsigned start = 0;
characterToTokenIndex[0] = 0;
for (unsigned cidx = 1; cidx < candidate.size(); ++cidx) {
char current = candidate[cidx];
char prev = candidate[cidx - 1];
// Is this a special tokenizing character like '_', or the start of a camel
// case word? The uppercase character should start a new token.
if (isTokenizingChar(prev) ||
(isUppercase(current) && !isUppercase(prev)) ||
(clang::isDigit(current) && !clang::isDigit(prev))) {
tokens.push_back({start, cidx - start});
start = cidx;
} else if (isLowercase(current) && isUppercase(prev) && start != cidx - 1) {
// Or is this the end of a run of uppercase characters?
// E.g. in NSWindow, the 'W' should start a new token.
tokens.push_back({start, cidx - start - 1});
characterToTokenIndex[cidx - 1] = tokens.size();
start = cidx - 1;
}
characterToTokenIndex[cidx] = tokens.size();
}
tokens.push_back({start, static_cast<unsigned>(candidate.size() - start)});
}
static constexpr unsigned notFound = ~0U;
namespace {
/// The candidate-specific matching data and algorithms.
struct CandidateSpecificMatcher {
// The following StringRefs are owned by FuzzyStringMatcher and must outlive
// this object.
StringRef pattern;
StringRef lowercasePattern;
StringRef candidate;
SmallVector<char, 128> lowercaseCandidate;
SmallVector<unsigned, 128> jumpTable; ///< The next matching character index.
SmallVector<Range, 128> tokens; ///< Tokenized ranges from the candidate.
SmallVector<unsigned, 128> characterToTokenIndex;
SmallVector<Range, 128> runs;
CandidateSpecificMatcher(StringRef pattern, StringRef lowercasePattern,
StringRef candidate,
const llvm::BitVector &charactersInPattern,
unsigned &firstPatternPos);
/// Calculates the candidate's score, matching the candidate from
/// \p firstPatternPos or later.
///
/// This drives scoreCandidateTrial by trying the possible matches.
double scoreCandidate(unsigned firstPatternPos);
/// Calculates the candidate's score, matching the candidate from
/// exactly \p firstPatternPos.
double scoreCandidateTrial(unsigned firstPatternPos);
};
} // end anonymous namespace
double FuzzyStringMatcher::scoreCandidate(StringRef candidate) const {
double finalScore = 0.0;
if (candidate.empty() || pattern.empty() || candidate.size() < pattern.size())
return finalScore;
// Single character pattern matching should be simple and fast. Just look at
// the first character.
if (pattern.size() == 1) {
char c = candidate[0];
if (c == pattern[0] && isUppercase(c)) {
finalScore = 3.0;
} else if (toLowercase(c) == lowercasePattern[0]) {
finalScore = 2.0;
}
// Make sure shorter results come first;
if (finalScore)
finalScore += (1 / static_cast<double>(candidate.size())) * (1 / 1000.0);
if (normalize)
finalScore /= maxScore;
return finalScore;
}
// FIXME: path separators would be handled here, jumping straight to the last
// component if the pattern doesn't contain a separator.
unsigned firstPatternPos = 0;
CandidateSpecificMatcher CSM(pattern, lowercasePattern, candidate,
charactersInPattern, firstPatternPos);
finalScore = CSM.scoreCandidate(firstPatternPos);
if (normalize)
finalScore /= maxScore;
return finalScore;
}
CandidateSpecificMatcher::CandidateSpecificMatcher(
StringRef pattern, StringRef lowercasePattern, StringRef candidate,
const llvm::BitVector &charactersInPattern, unsigned &firstPatternPos)
: pattern(pattern), lowercasePattern(lowercasePattern),
candidate(candidate) {
assert(!pattern.empty() && pattern.size() <= candidate.size());
assert(pattern.size() == lowercasePattern.size());
// Build a table that points at the next pattern character so we skip
// through candidate faster.
unsigned candidateLength = candidate.size();
jumpTable.resize(candidateLength);
lowercaseCandidate.resize(candidateLength);
unsigned lastPatternPos = notFound;
for (unsigned cidx = candidateLength - 1;; --cidx) {
char c = candidate[cidx];
lowercaseCandidate[cidx] = toLowercase(c);
jumpTable[cidx] = lastPatternPos;
if (charactersInPattern[static_cast<unsigned char>(c)])
lastPatternPos = cidx;
if (!cidx)
break;
}
firstPatternPos = lastPatternPos;
// Build the token table.
characterToTokenIndex.resize(candidate.size());
populateTokenTable(tokens, characterToTokenIndex, candidate);
}
double CandidateSpecificMatcher::scoreCandidate(unsigned firstPatternPos) {
double finalScore = 0.0;
// The outer matching loop. We run multiple trials so that "a_b_c_abc"
// matching "abc" is matched on the "abc" part instead of the "a_b_c" part.
while (firstPatternPos != notFound) {
// Quickly skip to the first character that matches. We need
// the loop in case the first pattern-character in the
// candidate is not the first character in the pattern.
while (firstPatternPos != notFound) {
if (lowercasePattern[0] == lowercaseCandidate[firstPatternPos])
break;
firstPatternPos = jumpTable[firstPatternPos];
}
if (firstPatternPos == notFound)
break;
double trialScore = scoreCandidateTrial(firstPatternPos);
if (trialScore > finalScore) {
finalScore = trialScore;
// FIXME: update output ranges, if necessary
}
firstPatternPos = jumpTable[firstPatternPos];
}
return finalScore;
}
static double scoreRun(unsigned runStart, unsigned runLength,
unsigned prevTokenStart, unsigned tokenIndex,
unsigned uppercaseMatches, bool isTokenizingChar) {
if (runLength == 0)
return 0.0;
// We really don't like not matching at token starts, but if it's a long match
// give some credit.
if (runStart != prevTokenStart && !isTokenizingChar) {
if (runLength < 5) {
return (runLength < 3) ? 0.0 : runLength;
}
// For really long matches, we'll give a high score. Pretend it's a bit
// shorter.
runLength -= 2;
}
// Bonus if the match is the first or second token.
double prefixBonus = (runStart == 0) ? 2.5 : ((tokenIndex < 2) ? 1.0 : 0.0);
double uppercaseBonus =
uppercaseMatches ? (uppercaseMatches + 1) * (uppercaseMatches + 1) : 0.0;
return (runLength * runLength) + uppercaseBonus + prefixBonus;
}
double
CandidateSpecificMatcher::scoreCandidateTrial(unsigned firstPatternPos) {
double trialScore = 0.0; /// We run multiple trials so that "a_b_c_abc"
/// matching "abc" is matched on the "abc" part
/// instead of the "a_b_c" part.
unsigned uppercaseMatches = 0;
unsigned cidx = firstPatternPos;
unsigned pidx = 0;
unsigned runLength = 0;
unsigned runStart = cidx;
unsigned nonTokenRuns = 0;
unsigned camelCaseLen = 0;
unsigned camelCaseLastToken = 0;
double camelCaseStartBonus = 0.0;
unsigned camelCaseSkips = 0;
unsigned patternLength = pattern.size();
unsigned candidateLength = candidate.size();
while (pidx < patternLength && cidx < candidateLength) {
char lowerPatternChar = lowercasePattern[pidx];
char lowerCandidateChar = lowercaseCandidate[cidx];
unsigned nextCidx = jumpTable[cidx];
bool matched = lowerPatternChar == lowerCandidateChar;
if (matched) {
if (isUppercase(pattern[pidx]) && isUppercase(candidate[cidx])) {
++uppercaseMatches;
}
++runLength;
++pidx;
if (pidx < patternLength)
lowerPatternChar = lowercasePattern[pidx];
}
// If we're skipping forward and were running, the run ended.
if (((cidx + 1) != nextCidx) || !matched) {
if (runLength) {
double runValue =
scoreRun(runStart, runLength,
tokens[characterToTokenIndex[runStart]].location,
characterToTokenIndex[runStart], uppercaseMatches,
isTokenizingChar(candidate[runStart]));
// If it's a poor match in the middle of a token, see if the next char
// starts a token and also matches. If so, use it.
if (runLength == 1 && pidx > 1 && runValue == 0.0 &&
nextCidx != notFound &&
characterToTokenIndex[runStart] < tokens.size() - 1) {
bool foundIt = false;
unsigned charToCheck = matched ? nextCidx : cidx;
while (charToCheck != notFound) {
if (tokens[characterToTokenIndex[charToCheck]].location ==
charToCheck &&
lowercasePattern[pidx - 1] == lowercaseCandidate[charToCheck]) {
foundIt = true;
break;
}
charToCheck = jumpTable[charToCheck];
}
if (foundIt) {
--pidx;
lowerPatternChar = lowercasePattern[pidx];
runStart = cidx = charToCheck;
runLength = 0;
continue;
}
}
// We really don't like matches that don't start at a token.
if (runValue == 0.0) {
++nonTokenRuns;
} else {
unsigned tokenIndex = characterToTokenIndex[runStart];
if (runStart == tokens[tokenIndex].location ||
isTokenizingChar(lowerCandidateChar)) {
camelCaseLen += runLength;
// Bonus for matching the beginning of the candidate.
if (tokenIndex <= 1) {
camelCaseStartBonus = 2.0;
// Penalty for skipping a token.
} else if (tokenIndex != camelCaseLastToken + 1) {
camelCaseSkips += tokenIndex - camelCaseLastToken - 1;
}
camelCaseLastToken = tokenIndex;
if (isTokenizingChar(lowerCandidateChar) && runLength == 1) {
--camelCaseLastToken;
}
}
}
// Accumulate run and reset for next run.
trialScore += runValue;
runs.push_back({runStart, runLength});
uppercaseMatches = 0;
runLength = 0;
}
runStart = nextCidx;
}
cidx = nextCidx;
}
// The trial is done, did we find a match?
// FIXME: this can happen spuriously in foo => ufDownOb.
if (pidx != patternLength)
return 0.0;
// Okay, we found a match.
// FIXME: this code is largely duplicated with the previous block. There are
// some subtle differences that can be seen if you try to remove this one and
// check for pidx == patternLength for the other block.
if (runLength) {
double runValue = scoreRun(
runStart, runLength, tokens[characterToTokenIndex[runStart]].location,
characterToTokenIndex[runStart], uppercaseMatches,
isTokenizingChar(candidate[runStart]));
// If it's a poor match in the middle of a token, see if the next char
// starts a token and also matches. If so, use it.
if (runLength == 1 && runValue == 0.0) {
unsigned nextCidx = jumpTable[runStart];
if (nextCidx != notFound &&
characterToTokenIndex[runStart] < tokens.size() - 1) {
bool foundIt = false;
while (nextCidx != notFound) {
if (tokens[characterToTokenIndex[nextCidx]].location == nextCidx &&
lowercasePattern[pidx - 1] == lowercaseCandidate[nextCidx]) {
foundIt = true;
break;
}
nextCidx = jumpTable[nextCidx];
}
if (foundIt) {
runStart = nextCidx;
uppercaseMatches +=
(isUppercase(pattern[pidx - 1]) &&
isUppercase(candidate[runStart])) ? 1 : 0;
runValue = scoreRun(runStart, runLength,
tokens[characterToTokenIndex[runStart]].location,
characterToTokenIndex[runStart], uppercaseMatches,
isTokenizingChar(candidate[runStart]));
}
}
}
// We really don't like matches that don't start at a token.
if (runValue == 0.0) {
++nonTokenRuns;
} else {
unsigned tokenIndex = characterToTokenIndex[runStart];
if (runStart == tokens[tokenIndex].location ||
isTokenizingChar(lowercaseCandidate[runStart])) {
camelCaseLen += runLength;
if (tokenIndex <= 1) {
// Bonus for matching the beginning of the candidate.
camelCaseStartBonus = 2.0;
} else if (tokenIndex != camelCaseLastToken + 1) {
// Penalty for skipping a token.
camelCaseSkips += tokenIndex - camelCaseLastToken - 1;
}
}
}
// Accumulate run.
trialScore += runValue;
runs.push_back({runStart, runLength});
}
// Unless there were bad matches, prefer camel case matches.
if (nonTokenRuns == 0 && camelCaseSkips < 3) {
double camelCaseScore = (camelCaseLen * camelCaseLen) + camelCaseStartBonus;
if (camelCaseSkips == 1) {
camelCaseScore *= 0.9;
} else if (camelCaseSkips == 2) {
camelCaseScore *= 0.8;
}
if (trialScore < camelCaseScore) {
// Camel case matched better.
trialScore = camelCaseScore;
}
}
// FIXME: using the range up to a dot is silly when candidate isn't a file.
auto dotLoc = candidate.find_last_of('.');
unsigned baseNameLength =
dotLoc != StringRef::npos && dotLoc > 1 ? dotLoc : candidateLength;
// FIXME: file type bonus if we're checking a file path.
// Add a bit for the percentage of the candidate matched.
trialScore += patternLength / static_cast<double>(baseNameLength) * 0.25;
// Exact matches are even better.
if (patternLength >= baseNameLength && !runs.empty() &&
runs[0].location == 0) {
trialScore *= 1.1;
}
// Exact prefix matches are the best.
if (!runs.empty() && runs[0].location == 0 && runs[0].length == patternLength) {
trialScore *= 2.5;
// Case sensitive exact prefix matches are the best of the best.
if (candidate.starts_with(pattern))
trialScore *= 1.2;
}
// FIXME: popular/unpopular API.
// We really don't like matches that don't start at a token.
switch (nonTokenRuns) {
case 0:
break;
case 1:
trialScore *= 0.8125;
break;
case 2:
trialScore *= 0.5;
break;
case 3:
trialScore *= 0.25;
break;
default:
trialScore *= 0.0625;
break;
}
// FIXME: matched ranges output.
return trialScore;
}
|