Skip to content

Commit d82a085

Browse files
committed
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.
1 parent 2f8d81f commit d82a085

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

src/librustdoc/html/static/js/search.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1840,26 +1840,16 @@ function initSearch(rawSearchIndex) {
18401840

18411841
const length = path.length;
18421842
const clength = contains.length;
1843-
if (clength > length) {
1844-
return maxEditDistance + 1;
1845-
}
1846-
for (let i = 0; i < length; ++i) {
1847-
if (i + clength > length) {
1848-
break;
1849-
}
1843+
pathiter: for (let i = length - clength; i >= 0; i -= 1) {
18501844
let dist_total = 0;
1851-
let aborted = false;
18521845
for (let x = 0; x < clength; ++x) {
18531846
const dist = editDistance(path[i + x], contains[x], maxEditDistance);
18541847
if (dist > maxEditDistance) {
1855-
aborted = true;
1856-
break;
1848+
continue pathiter;
18571849
}
18581850
dist_total += dist;
18591851
}
1860-
if (!aborted) {
1861-
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
1862-
}
1852+
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
18631853
}
18641854
return ret_dist;
18651855
}

0 commit comments

Comments
 (0)