Skip to content

Commit f774847

Browse files
Nancy LiDevtools-frontend LUCI CQ
authored andcommitted
[Quick Open] Make the calculateScore function more clear for future debugging
Bug: 391533572 Change-Id: If3e3d4378c83a7e6d598058cbb50725e0bc8c7f0 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6189461 Auto-Submit: Nancy Li <[email protected]> Commit-Queue: Nancy Li <[email protected]> Reviewed-by: Kateryna Prokopenko <[email protected]> Commit-Queue: Kateryna Prokopenko <[email protected]>
1 parent 5f097b5 commit f774847

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

front_end/panels/sources/FilePathScoreFunction.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,60 @@ export class FilePathScoreFunction {
4545
this.fileNameIndex = 0;
4646
}
4747

48+
/**
49+
* Calculates the score of a given data string against the query string.
50+
*
51+
* The score is calculated by comparing the characters of the query string to
52+
* the characters of the data string. Characters that match are given a score
53+
* of 10, while characters that don't match are given a score of 0. The score
54+
* of a match is also influenced by the context of the match. For example,
55+
* matching the beginning of the file name is worth more than matching a
56+
* character in the middle of the file name.
57+
*
58+
* The score of a match is also influenced by the number of consecutive
59+
* matches. The more consecutive matches there are, the higher the score.
60+
*
61+
* @param data The data string to score.
62+
* @param matchIndexes An optional array to store the indexes of matching
63+
* characters. If provided, it will be filled with the indexes of the matching
64+
* characters in the data string.
65+
* @returns The score of the data string.
66+
*/
4867
calculateScore(data: string, matchIndexes: number[]|null): number {
4968
if (!data || !this.query) {
5069
return 0;
5170
}
52-
const n = this.query.length;
53-
const m = data.length;
54-
if (!this.score || this.score.length < n * m) {
55-
this.score = new Int32Array(n * m * 2);
56-
this.sequence = new Int32Array(n * m * 2);
71+
const queryLength = this.query.length;
72+
const dataLength = data.length;
73+
if (!this.score || this.score.length < queryLength * dataLength) {
74+
this.score = new Int32Array(queryLength * dataLength * 2);
75+
this.sequence = new Int32Array(queryLength * dataLength * 2);
5776
}
5877
const score = this.score;
5978
const sequence = (this.sequence as Int32Array);
6079
this.dataUpperCase = data.toUpperCase();
6180
this.fileNameIndex = data.lastIndexOf('/');
62-
for (let i = 0; i < n; ++i) {
63-
for (let j = 0; j < m; ++j) {
64-
const skipCharScore = j === 0 ? 0 : score[i * m + j - 1];
65-
const prevCharScore = i === 0 || j === 0 ? 0 : score[(i - 1) * m + j - 1];
66-
const consecutiveMatch = i === 0 || j === 0 ? 0 : sequence[(i - 1) * m + j - 1];
81+
for (let i = 0; i < queryLength; ++i) {
82+
for (let j = 0; j < dataLength; ++j) {
83+
const scoreIndex = i * dataLength + j;
84+
const skipCharScore = j === 0 ? 0 : score[scoreIndex - 1];
85+
const prevCharScore = i === 0 || j === 0 ? 0 : score[(i - 1) * dataLength + j - 1];
86+
const consecutiveMatch = i === 0 || j === 0 ? 0 : sequence[(i - 1) * dataLength + j - 1];
6787
const pickCharScore = this.match(this.query, data, i, j, consecutiveMatch);
6888
if (pickCharScore && prevCharScore + pickCharScore >= skipCharScore) {
69-
sequence[i * m + j] = consecutiveMatch + 1;
70-
score[i * m + j] = (prevCharScore + pickCharScore);
89+
sequence[scoreIndex] = consecutiveMatch + 1;
90+
score[scoreIndex] = (prevCharScore + pickCharScore);
7191
} else {
72-
sequence[i * m + j] = 0;
73-
score[i * m + j] = skipCharScore;
92+
sequence[scoreIndex] = 0;
93+
score[scoreIndex] = skipCharScore;
7494
}
7595
}
7696
}
7797
if (matchIndexes) {
78-
this.restoreMatchIndexes(sequence, n, m, matchIndexes);
98+
this.restoreMatchIndexes(sequence, queryLength, dataLength, matchIndexes);
7999
}
80100
const maxDataLength = 256;
81-
return score[n * m - 1] * maxDataLength + (maxDataLength - data.length);
101+
return score[queryLength * dataLength - 1] * maxDataLength + (maxDataLength - data.length);
82102
}
83103

84104
private testWordStart(data: string, j: number): boolean {
@@ -91,10 +111,10 @@ export class FilePathScoreFunction {
91111
(data[j - 1] !== this.dataUpperCase[j - 1] && data[j] === this.dataUpperCase[j]);
92112
}
93113

94-
private restoreMatchIndexes(sequence: Int32Array, n: number, m: number, out: number[]): void {
95-
let i = n - 1, j = m - 1;
114+
private restoreMatchIndexes(sequence: Int32Array, queryLength: number, dataLength: number, out: number[]): void {
115+
let i = queryLength - 1, j = dataLength - 1;
96116
while (i >= 0 && j >= 0) {
97-
switch (sequence[i * m + j]) {
117+
switch (sequence[i * dataLength + j]) {
98118
case 0:
99119
--j;
100120
break;

0 commit comments

Comments
 (0)