From 805ef52ae6156abffe1dee7fa3c25dcd7923f913 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 14 Nov 2024 13:01:25 -0800 Subject: [PATCH] Use loop for acceptParams (#6066) --- lib/utils.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 67eacf274e..35b2781306 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -84,16 +84,33 @@ exports.normalizeTypes = function(types){ */ function acceptParams (str) { - var parts = str.split(/ *; */); - var ret = { value: parts[0], quality: 1, params: {} } + var length = str.length; + var colonIndex = str.indexOf(';'); + var index = colonIndex === -1 ? length : colonIndex; + var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} }; - for (var i = 1; i < parts.length; ++i) { - var pms = parts[i].split(/ *= */); - if ('q' === pms[0]) { - ret.quality = parseFloat(pms[1]); + while (index < length) { + var splitIndex = str.indexOf('=', index); + if (splitIndex === -1) break; + + var colonIndex = str.indexOf(';', index); + var endIndex = colonIndex === -1 ? length : colonIndex; + + if (splitIndex > endIndex) { + index = str.lastIndexOf(';', splitIndex - 1) + 1; + continue; + } + + var key = str.slice(index, splitIndex).trim(); + var value = str.slice(splitIndex + 1, endIndex).trim(); + + if (key === 'q') { + ret.quality = parseFloat(value); } else { - ret.params[pms[0]] = pms[1]; + ret.params[key] = value; } + + index = endIndex + 1; } return ret;