rustdoc-search: clean up checkPath

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.
This commit is contained in:
Michael Howell 2023-11-20 13:15:47 -07:00
parent 2f8d81f9db
commit d82a08537a

View File

@ -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;
}