Skip to content

Commit 63164b7

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 46ecc10 commit 63164b7

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
@@ -1648,26 +1648,16 @@ function initSearch(rawSearchIndex) {
16481648

16491649
const length = path.length;
16501650
const clength = contains.length;
1651-
if (clength > length) {
1652-
return maxEditDistance + 1;
1653-
}
1654-
for (let i = 0; i < length; ++i) {
1655-
if (i + clength > length) {
1656-
break;
1657-
}
1651+
path: for (let i = length - clength; i >= 0; i -= 1) {
16581652
let dist_total = 0;
1659-
let aborted = false;
16601653
for (let x = 0; x < clength; ++x) {
16611654
const dist = editDistance(path[i + x], contains[x], maxEditDistance);
16621655
if (dist > maxEditDistance) {
1663-
aborted = true;
1664-
break;
1656+
continue path;
16651657
}
16661658
dist_total += dist;
16671659
}
1668-
if (!aborted) {
1669-
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
1670-
}
1660+
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
16711661
}
16721662
return ret_dist;
16731663
}

0 commit comments

Comments
 (0)