Skip to content

Commit

Permalink
add fast path and move negation out (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday authored Jan 13, 2024
1 parent 3c666ae commit 21d4d2f
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,32 @@ function parse (str, opt) {
throw new TypeError('argument str must be a string')
}

const result = {}
const dec = opt?.decode || decodeURIComponent
const result = {}

const strLen = str.length
let pos = 0
let terminatorPos = 0
let eqIdx = 0

while (true) {
if (terminatorPos === str.length) {
break
}

if (terminatorPos === strLen) break
terminatorPos = str.indexOf(';', pos)
terminatorPos = (terminatorPos === -1) ? str.length : terminatorPos
eqIdx = str.indexOf('=', pos)
if (terminatorPos === -1) terminatorPos = strLen // This is the last pair

// skip things that don't look like key=value
if (eqIdx === -1 || eqIdx > terminatorPos) {
let eqIdx = str.indexOf('=', pos)
if (eqIdx === -1) break // No key-value pairs left
if (eqIdx > terminatorPos) {
// Malformed key-value pair
pos = terminatorPos + 1
continue
}

const key = str.substring(pos, eqIdx++).trim()

// only assign once
if (result[key] === undefined) {
const val = (str.charCodeAt(eqIdx) === 0x22)
const val = str.charCodeAt(eqIdx) === 0x22
? str.substring(eqIdx + 1, terminatorPos - 1).trim()
: str.substring(eqIdx, terminatorPos).trim()

result[key] = (dec !== decodeURIComponent || val.indexOf('%') !== -1)
result[key] = !(dec === decodeURIComponent && val.indexOf('%') === -1)
? tryDecode(val, dec)
: val
}
Expand Down

0 comments on commit 21d4d2f

Please sign in to comment.