You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also the most expensive operation seems to be te charCodeAt()-call. So maybe if we loaded highCharCode already and skip it if we should not decode that character?
'use strict'consthighCharCodeDefault=newArray(65536)for(leti=0;i<65536;++i){highCharCodeDefault[i]=null}constdecodeLookup=newArray(65536)for(leti=0;i<65536;++i){decodeLookup[i]=highCharCodeDefault}decodeLookup[50]=highCharCodeDefault.slice()decodeLookup[50][53]='%'decodeLookup[50][51]='#'decodeLookup[50][52]='$'decodeLookup[50][54]='&'decodeLookup[50][66]='+'decodeLookup[50][98]='+'decodeLookup[50][67]=','decodeLookup[50][99]=','decodeLookup[50][70]='/'decodeLookup[50][102]='/'decodeLookup[51]=highCharCodeDefault.slice()decodeLookup[51][65]=':'decodeLookup[51][97]=':'decodeLookup[51][66]=';'decodeLookup[51][98]=';'decodeLookup[51][68]='='decodeLookup[51][100]='='decodeLookup[51][70]='?'decodeLookup[51][102]='?'decodeLookup[52]=highCharCodeDefault.slice()decodeLookup[52][48]='@'constshouldHighCharCodeDefault=newArray(65536)for(leti=0;i<65536;++i){shouldHighCharCodeDefault[i]=false}constshouldDecodeLookup=newArray(65536)for(leti=0;i<65536;++i){shouldDecodeLookup[i]=shouldHighCharCodeDefault}shouldDecodeLookup[50]=shouldHighCharCodeDefault.slice()shouldDecodeLookup[50][53]=true// '%'shouldDecodeLookup[50][51]=true// '#'shouldDecodeLookup[50][52]=true// '$'shouldDecodeLookup[50][54]=true// '&'shouldDecodeLookup[50][66]=true// '+'shouldDecodeLookup[50][98]=true// '+'shouldDecodeLookup[50][67]=true// ','shouldDecodeLookup[50][99]=true// ','shouldDecodeLookup[50][70]=true// '/'shouldDecodeLookup[50][102]=true// '/'shouldDecodeLookup[51]=shouldHighCharCodeDefault.slice()shouldDecodeLookup[51][65]=true// ':'shouldDecodeLookup[51][97]=true// ':'shouldDecodeLookup[51][66]=true// ';'shouldDecodeLookup[51][98]=true// ';'shouldDecodeLookup[51][68]=true// '='shouldDecodeLookup[51][100]=true// '='shouldDecodeLookup[51][70]=true// '?'shouldDecodeLookup[51][102]=true// '?'shouldDecodeLookup[52]=shouldHighCharCodeDefault.slice()shouldDecodeLookup[52][48]=true// '@'constshouldDecodeHighCharCodeLookup=newArray(65536)for(leti=0;i<65536;++i){shouldDecodeHighCharCodeLookup[i]=i===50||i===51||i===52}constrfcNonConform=newArray(65536)for(leti=0;i<65536;++i){rfcNonConform[i]=i===63||i===59||i===35}functionsafeDecodeURI(path){letshouldDecode=falseletshouldDecodeParam=falseletquerystring=''for(leti=1;i<path.length;i++){constcharCode=path.charCodeAt(i)if(charCode===37){consthighCharCode=path.charCodeAt(i+1)constlowCharCode=(shouldDecodeHighCharCodeLookup[highCharCode]&&path.charCodeAt(i+2))||0if(shouldDecodeLookup[highCharCode][lowCharCode]){shouldDecodeParam=true// %25 - encoded % char. We need to encode one more time to prevent double decodingif(highCharCode===50&&lowCharCode===53){shouldDecode=truepath=path.slice(0,i+1)+'25'+path.slice(i+1)i+=2}i+=2}else{shouldDecode=true}// Some systems do not follow RFC and separate the path and query// string with a `;` character (code 59), e.g. `/foo;jsessionid=123456`.// Thus, we need to split on `;` as well as `?` and `#`.}elseif(rfcNonConform[charCode]){querystring=path.slice(i+1)path=path.slice(0,i)break}}constdecodedPath=shouldDecode ? decodeURI(path) : pathreturn{path: decodedPath, querystring, shouldDecodeParam }}functionsafeDecodeURIComponent(uriComponent){conststartIndex=uriComponent.indexOf('%')if(startIndex===-1)returnuriComponentletdecoded=''letlastIndex=startIndexconstlen=uriComponent.lengthfor(leti=startIndex;i<len;i++){if(uriComponent.charCodeAt(i)===37){consthighCharCode=uriComponent.charCodeAt(i+1)constlowCharCode=(shouldDecodeHighCharCodeLookup[highCharCode]&&uriComponent.charCodeAt(i+2))||0constdecodedChar=decodeLookup[highCharCode][lowCharCode]decoded+=uriComponent.slice(lastIndex,i)+decodedCharlastIndex=i+3}}returnuriComponent.slice(0,startIndex)+decoded+uriComponent.slice(lastIndex)}module.exports={ safeDecodeURI, safeDecodeURIComponent }
The text was updated successfully, but these errors were encountered:
It looks a bit expensive. And you still call the charCodeAt for each char in the path, right?
for(leti=1;i<path.length;i++){constcharCode=path.charCodeAt(i)// <-- still have the call if(charCode===37){consthighCharCode=path.charCodeAt(i+1)// <-- this call is not really expansive, bc it's not a part of the hot path. It's called only when you have an encoded char in the path
I would say it doesn't worth it. It's a one additional charCodeAt call when you meet an encoded char and it's not from the list. It's definitely not a hot path. Theoratically you can reduce this call, but it might kill the code readability.
@ivan-tymoshenko
What do you think about such a construction?
Also the most expensive operation seems to be te charCodeAt()-call. So maybe if we loaded highCharCode already and skip it if we should not decode that character?
The text was updated successfully, but these errors were encountered: