Skip to content

Commit

Permalink
rustdoc-search: clean up checkPath
Browse files Browse the repository at this point in the history
This computes the same result with less code by computing many of
the old checks at once:

* It won't enter the loop if clength > length, because then the
  result of length - clength will be negative and the
  loop conditional will fail.
* i + clength will never be greater than length, because it
  starts out as i = length - clength, implying that i + clength
  equals length, and it only goes down from there.
* The aborted variable is replaced with control flow.
  • Loading branch information
notriddle committed Nov 21, 2023
1 parent 2f8d81f commit d82a085
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1840,26 +1840,16 @@ function initSearch(rawSearchIndex) {

const length = path.length;
const clength = contains.length;
if (clength > length) {
return maxEditDistance + 1;
}
for (let i = 0; i < length; ++i) {
if (i + clength > length) {
break;
}
pathiter: for (let i = length - clength; i >= 0; i -= 1) {
let dist_total = 0;
let aborted = false;
for (let x = 0; x < clength; ++x) {
const dist = editDistance(path[i + x], contains[x], maxEditDistance);
if (dist > maxEditDistance) {
aborted = true;
break;
continue pathiter;
}
dist_total += dist;
}
if (!aborted) {
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
}
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
}
return ret_dist;
}
Expand Down

0 comments on commit d82a085

Please sign in to comment.