Skip to content

Commit 930cba8

Browse files
committed
rustdoc-search: replace TAB/NL/LF with SP first
This way, most of the parsing code doesn't need to be designed to handle it, since they should always be treated exactly the same anyhow.
1 parent 93f1711 commit 930cba8

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,6 @@ function initSearch(rawSearchIndex) {
287287
}
288288
}
289289

290-
function isWhitespace(c) {
291-
return " \t\n\r".indexOf(c) !== -1;
292-
}
293-
294290
function isSpecialStartCharacter(c) {
295291
return "<\"".indexOf(c) !== -1;
296292
}
@@ -408,7 +404,7 @@ function initSearch(rawSearchIndex) {
408404
* @return {boolean}
409405
*/
410406
function isPathSeparator(c) {
411-
return c === ":" || isWhitespace(c);
407+
return c === ":" || c === " ";
412408
}
413409

414410
/**
@@ -425,7 +421,7 @@ function initSearch(rawSearchIndex) {
425421
const c = parserState.userQuery[pos - 1];
426422
if (c === lookingFor) {
427423
return true;
428-
} else if (!isWhitespace(c)) {
424+
} else if (c !== " ") {
429425
break;
430426
}
431427
pos -= 1;
@@ -454,7 +450,7 @@ function initSearch(rawSearchIndex) {
454450
function skipWhitespace(parserState) {
455451
while (parserState.pos < parserState.userQuery.length) {
456452
const c = parserState.userQuery[parserState.pos];
457-
if (!isWhitespace(c)) {
453+
if (c !== " ") {
458454
break;
459455
}
460456
parserState.pos += 1;
@@ -599,7 +595,7 @@ function initSearch(rawSearchIndex) {
599595
} else {
600596
while (parserState.pos + 1 < parserState.length) {
601597
const next_c = parserState.userQuery[parserState.pos + 1];
602-
if (!isWhitespace(next_c)) {
598+
if (next_c !== " ") {
603599
break;
604600
}
605601
parserState.pos += 1;
@@ -953,7 +949,7 @@ function initSearch(rawSearchIndex) {
953949
query.literalSearch = false;
954950
foundStopChar = true;
955951
continue;
956-
} else if (isWhitespace(c)) {
952+
} else if (c === " ") {
957953
skipWhitespace(parserState);
958954
continue;
959955
}
@@ -1113,7 +1109,7 @@ function initSearch(rawSearchIndex) {
11131109
}
11141110
}
11151111
}
1116-
userQuery = userQuery.trim();
1112+
userQuery = userQuery.trim().replace(/\r|\n|\t/g, " ");
11171113
const parserState = {
11181114
length: userQuery.length,
11191115
pos: 0,

tests/rustdoc-js-std/parser-errors.js

+9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ const PARSED = [
161161
userQuery: "a:: ::b",
162162
error: "Unexpected `:: ::`",
163163
},
164+
{
165+
query: "a::\t::b",
166+
elems: [],
167+
foundElems: 0,
168+
original: "a:: ::b",
169+
returned: [],
170+
userQuery: "a:: ::b",
171+
error: "Unexpected `:: ::`",
172+
},
164173
{
165174
query: "a::b::",
166175
elems: [],

tests/rustdoc-js-std/parser-separators.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PARSED = [
55
query: 'aaaaaa b',
66
elems: [
77
{
8-
name: 'aaaaaa\tb',
8+
name: 'aaaaaa b',
99
fullPath: ['aaaaaa', 'b'],
1010
pathWithoutLast: ['aaaaaa'],
1111
pathLast: 'b',
@@ -14,9 +14,9 @@ const PARSED = [
1414
},
1515
],
1616
foundElems: 1,
17-
original: "aaaaaa b",
17+
original: "aaaaaa b",
1818
returned: [],
19-
userQuery: "aaaaaa b",
19+
userQuery: "aaaaaa b",
2020
error: null,
2121
},
2222
{
@@ -40,9 +40,9 @@ const PARSED = [
4040
},
4141
],
4242
foundElems: 2,
43-
original: "aaaaaa, b",
43+
original: "aaaaaa, b",
4444
returned: [],
45-
userQuery: "aaaaaa, b",
45+
userQuery: "aaaaaa, b",
4646
error: null,
4747
},
4848
{
@@ -93,7 +93,7 @@ const PARSED = [
9393
query: 'a\tb',
9494
elems: [
9595
{
96-
name: 'a\tb',
96+
name: 'a b',
9797
fullPath: ['a', 'b'],
9898
pathWithoutLast: ['a'],
9999
pathLast: 'b',
@@ -102,9 +102,9 @@ const PARSED = [
102102
},
103103
],
104104
foundElems: 1,
105-
original: "a\tb",
105+
original: "a b",
106106
returned: [],
107-
userQuery: "a\tb",
107+
userQuery: "a b",
108108
error: null,
109109
},
110110
{
@@ -176,7 +176,7 @@ const PARSED = [
176176
pathLast: 'a',
177177
generics: [
178178
{
179-
name: 'b\tc',
179+
name: 'b c',
180180
fullPath: ['b', 'c'],
181181
pathWithoutLast: ['b'],
182182
pathLast: 'c',
@@ -187,9 +187,9 @@ const PARSED = [
187187
},
188188
],
189189
foundElems: 1,
190-
original: "a<b\tc>",
190+
original: "a<b c>",
191191
returned: [],
192-
userQuery: "a<b\tc>",
192+
userQuery: "a<b c>",
193193
error: null,
194194
},
195195
];

tests/rustdoc-js-std/parser-weird-queries.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ const PARSED = [
9292
query: 'mod\t:',
9393
elems: [],
9494
foundElems: 0,
95-
original: 'mod\t:',
95+
original: 'mod :',
9696
returned: [],
97-
userQuery: 'mod\t:',
97+
userQuery: 'mod :',
9898
error: "Unexpected `:` (expected path after type filter `mod:`)",
9999
},
100100
];

0 commit comments

Comments
 (0)