Skip to content

Commit b43befa

Browse files
authored
Use loop for acceptParams
from expressjs/express#6066
1 parent 080f007 commit b43befa

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/utils.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,33 @@ function canBeOptimized(pattern) {
106106
}
107107

108108
function acceptParams(str) {
109-
const parts = str.split(/ *; */);
110-
const ret = { value: parts[0], quality: 1, params: {} }
111-
112-
for (let i = 1, len = parts.length; i < len; ++i) {
113-
const pms = parts[i].split(/ *= */);
114-
const [pms_0, pms_1] = pms;
115-
if ('q' === pms_0) {
116-
ret.quality = parseFloat(pms_1);
117-
} else {
118-
ret.params[pms_0] = pms_1;
119-
}
109+
const length = str.length;
110+
const colonIndex = str.indexOf(';');
111+
const index = colonIndex === -1 ? length : colonIndex;
112+
const ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };
113+
114+
while (index < length) {
115+
const splitIndex = str.indexOf('=', index);
116+
if (splitIndex === -1) break;
117+
118+
const colonIndex = str.indexOf(';', index);
119+
const endIndex = colonIndex === -1 ? length : colonIndex;
120+
121+
if (splitIndex > endIndex) {
122+
index = str.lastIndexOf(';', splitIndex - 1) + 1;
123+
continue;
124+
}
125+
126+
const key = str.slice(index, splitIndex).trim();
127+
const value = str.slice(splitIndex + 1, endIndex).trim();
128+
129+
if (key === 'q') {
130+
ret.quality = parseFloat(value);
131+
} else {
132+
ret.params[key] = value;
133+
}
134+
135+
index = endIndex + 1;
120136
}
121137

122138
return ret;

0 commit comments

Comments
 (0)