Browse Source

Don't create a string when lexing all-digit integers.

Nicholas Nethercote 11 years ago
parent
commit
164d7a6e15
  1. 18
      src/core/parser.js

18
src/core/parser.js

@ -396,6 +396,7 @@ var Lexer = (function LexerClosure() {
getNumber: function Lexer_getNumber() { getNumber: function Lexer_getNumber() {
var floating = false; var floating = false;
var ch = this.currentChar; var ch = this.currentChar;
var allDigits = ch >= 0x30 && ch <= 0x39;
var strBuf = this.strBuf; var strBuf = this.strBuf;
strBuf.length = 0; strBuf.length = 0;
strBuf.push(String.fromCharCode(ch)); strBuf.push(String.fromCharCode(ch));
@ -405,20 +406,33 @@ var Lexer = (function LexerClosure() {
} else if (ch === 0x2E && !floating) { // '.' } else if (ch === 0x2E && !floating) { // '.'
strBuf.push('.'); strBuf.push('.');
floating = true; floating = true;
allDigits = false;
} else if (ch === 0x2D) { // '-' } else if (ch === 0x2D) { // '-'
// ignore minus signs in the middle of numbers to match // ignore minus signs in the middle of numbers to match
// Adobe's behavior // Adobe's behavior
warn('Badly formated number'); warn('Badly formated number');
allDigits = false;
} else if (ch === 0x45 || ch === 0x65) { // 'E', 'e' } else if (ch === 0x45 || ch === 0x65) { // 'E', 'e'
floating = true; floating = true;
allDigits = false;
} else { } else {
// the last character doesn't belong to us // the last character doesn't belong to us
break; break;
} }
} }
var value = parseFloat(strBuf.join('')); var value;
if (isNaN(value)) if (allDigits) {
value = 0;
var charCodeOfZero = 48; // '0'
for (var i = 0, ii = strBuf.length; i < ii; i++) {
value = value * 10 + (strBuf[i].charCodeAt(0) - charCodeOfZero);
}
} else {
value = parseFloat(strBuf.join(''));
if (isNaN(value)) {
error('Invalid floating point number: ' + value); error('Invalid floating point number: ' + value);
}
}
return value; return value;
}, },
getString: function Lexer_getString() { getString: function Lexer_getString() {

Loading…
Cancel
Save