Skip to content

Commit

Permalink
feat(read-string): optimize read-string performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanfang committed Apr 16, 2021
1 parent 3fc320f commit 4b80828
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/parser/read-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,65 @@ var ExprType = require('./expr-type');
*/
function readString(walker) {
var startCode = walker.currentCode();
var startIndex = walker.index;
var value = "";
var charCode;

walkLoop: while ((charCode = walker.nextCode())) {
switch (charCode) {
case 92: // \
walker.go(1);
charCode = walker.nextCode();

switch (charCode) {
case 117: // \u
value += String.fromCharCode(parseInt(
walker.cut(walker.index + 1, walker.index + 5)
), 16);
walker.go(4);
break;

case 120: // \x
value += String.fromCharCode(parseInt(
walker.cut(walker.index + 1, walker.index + 3)
), 16);
walker.go(2);
break;

case 98:
value += '\b';
break;
case 102:
value += '\f';
break;
case 110:
value += '\n';
break;
case 114:
value += '\r';
break;
case 116:
value += '\t';
break;
case 118:
value += '\v';
break;

default:
value += String.fromCharCode(charCode);
}

break;
case startCode:
walker.go(1);
break walkLoop;
default:
value += String.fromCharCode(charCode);
}
}

var literal = walker.cut(startIndex, walker.index);
return {
type: ExprType.STRING,
// 处理字符转义
value: (new Function('return ' + literal))()
value: value
};
}

Expand Down

0 comments on commit 4b80828

Please sign in to comment.