Skip to content

Commit

Permalink
Refactor parseTokenList function for optimization
Browse files Browse the repository at this point in the history
- Replaced substring with slice for clearer token extraction.
- Simplified token parsing by combining space, comma, and end-of-string checks in a single condition.
- Removed unnecessary end tracking variable, utilizing index directly for efficiency.
- Streamlined the token list generation by combining the logic for handling the final token.
  • Loading branch information
Ayoub-Mabrouk committed Nov 8, 2024
1 parent f185ef1 commit 1dc1a86
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,24 @@ function parseHttpDate (date) {
* Parse a HTTP token list.
*
* @param {string} str
* @returns {Array<string>}
* @private
*/

function parseTokenList (str) {
var end = 0
var list = []
var start = 0
var len = str.length

// gather tokens
for (var i = 0, len = str.length; i < len; i++) {
switch (str.charCodeAt(i)) {
case 0x20: /* */
if (start === end) {
start = end = i + 1
}
break
case 0x2c: /* , */
list.push(str.substring(start, end))
start = end = i + 1
break
default:
end = i + 1
break
for (var i = 0; i <= len; i++) {
var charCode = str.charCodeAt(i)
if (charCode === 0x20 || charCode === 0x2c || i === len) {
if (i > start) {
list.push(str.slice(start, i))
}
start = i + 1
}
}

// final token
list.push(str.substring(start, end))

return list
}

0 comments on commit 1dc1a86

Please sign in to comment.