Fewer calls to charCodeAt

This commit is contained in:
Tim Schaub
2013-06-09 00:21:55 -06:00
parent 9edc9ebcc5
commit 063b461ffd
2 changed files with 25 additions and 25 deletions

View File

@@ -342,46 +342,46 @@ ol.expression.Lexer.prototype.next = function() {
// check for common punctuation // check for common punctuation
if (code === ol.expression.Char.LEFT_PAREN || if (code === ol.expression.Char.LEFT_PAREN ||
code === ol.expression.Char.RIGHT_PAREN) { code === ol.expression.Char.RIGHT_PAREN) {
return this.scanPunctuator_(); return this.scanPunctuator_(code);
} }
// check for string literal // check for string literal
if (code === ol.expression.Char.SINGLE_QUOTE || if (code === ol.expression.Char.SINGLE_QUOTE ||
code === ol.expression.Char.DOUBLE_QUOTE) { code === ol.expression.Char.DOUBLE_QUOTE) {
return this.scanStringLiteral_(); return this.scanStringLiteral_(code);
} }
// check for identifier // check for identifier
if (this.isIdentifierStart_(code)) { if (this.isIdentifierStart_(code)) {
return this.scanIdentifier_(); return this.scanIdentifier_(code);
} }
// check dot punctuation or decimal // check dot punctuation or decimal
if (code === ol.expression.Char.DOT) { if (code === ol.expression.Char.DOT) {
if (this.isDecimalDigit_(this.getCharCode_(1))) { if (this.isDecimalDigit_(this.getCharCode_(1))) {
return this.scanNumericLiteral_(); return this.scanNumericLiteral_(code);
} }
return this.scanPunctuator_(); return this.scanPunctuator_(code);
} }
// check for numeric literal // check for numeric literal
if (this.isDecimalDigit_(code)) { if (this.isDecimalDigit_(code)) {
return this.scanNumericLiteral_(); return this.scanNumericLiteral_(code);
} }
// all the rest is punctuation // all the rest is punctuation
return this.scanPunctuator_(); return this.scanPunctuator_(code);
}; };
/** /**
* Scan hex literal as numeric token. * Scan hex literal as numeric token.
* *
* @param {number} code The current character code.
* @return {ol.expression.Token} Numeric literal token. * @return {ol.expression.Token} Numeric literal token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanHexLiteral_ = function() { ol.expression.Lexer.prototype.scanHexLiteral_ = function(code) {
var code = this.getCurrentCharCode_();
var str = ''; var str = '';
while (this.index_ < this.length_) { while (this.index_ < this.length_) {
@@ -415,11 +415,11 @@ ol.expression.Lexer.prototype.scanHexLiteral_ = function() {
/** /**
* Scan identifier token. * Scan identifier token.
* *
* @param {number} code The current character code.
* @return {ol.expression.Token} Identifier token. * @return {ol.expression.Token} Identifier token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanIdentifier_ = function() { ol.expression.Lexer.prototype.scanIdentifier_ = function(code) {
var code = this.getCurrentCharCode_();
goog.asserts.assert(this.isIdentifierStart_(code), goog.asserts.assert(this.isIdentifierStart_(code),
'Must be called with a valid identifier'); 'Must be called with a valid identifier');
@@ -460,11 +460,11 @@ ol.expression.Lexer.prototype.scanIdentifier_ = function() {
* Scan numeric literal token. * Scan numeric literal token.
* http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3 * http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
* *
* @param {number} code The current character code.
* @return {ol.expression.Token} Numeric literal token. * @return {ol.expression.Token} Numeric literal token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanNumericLiteral_ = function() { ol.expression.Lexer.prototype.scanNumericLiteral_ = function(code) {
var code = this.getCurrentCharCode_();
goog.asserts.assert( goog.asserts.assert(
code === ol.expression.Char.DOT || this.isDecimalDigit_(code), code === ol.expression.Char.DOT || this.isDecimalDigit_(code),
'Valid start for numeric literal: ' + String.fromCharCode(code)); 'Valid start for numeric literal: ' + String.fromCharCode(code));
@@ -481,13 +481,13 @@ ol.expression.Lexer.prototype.scanNumericLiteral_ = function() {
if (nextCode === ol.expression.Char.UPPER_X || if (nextCode === ol.expression.Char.UPPER_X ||
nextCode === ol.expression.Char.LOWER_X) { nextCode === ol.expression.Char.LOWER_X) {
this.increment_(2); this.increment_(2);
return this.scanHexLiteral_(); return this.scanHexLiteral_(this.getCurrentCharCode_());
} }
// octals start with 0 // octals start with 0
if (this.isOctalDigit_(nextCode)) { if (this.isOctalDigit_(nextCode)) {
this.increment_(1); this.increment_(1);
return this.scanOctalLiteral_(); return this.scanOctalLiteral_(nextCode);
} }
// numbers like 09 not allowed // numbers like 09 not allowed
@@ -564,11 +564,11 @@ ol.expression.Lexer.prototype.scanNumericLiteral_ = function() {
/** /**
* Scan octal literal as numeric token. * Scan octal literal as numeric token.
* *
* @param {number} code The current character code.
* @return {ol.expression.Token} Numeric literal token. * @return {ol.expression.Token} Numeric literal token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanOctalLiteral_ = function() { ol.expression.Lexer.prototype.scanOctalLiteral_ = function(code) {
var code = this.getCurrentCharCode_();
goog.asserts.assert(this.isOctalDigit_(code)); goog.asserts.assert(this.isOctalDigit_(code));
var str = '0' + String.fromCharCode(code); var str = '0' + String.fromCharCode(code);
@@ -602,11 +602,11 @@ ol.expression.Lexer.prototype.scanOctalLiteral_ = function() {
/** /**
* Scan punctuator token (a subset of allowed tokens in 7.7). * Scan punctuator token (a subset of allowed tokens in 7.7).
* *
* @param {number} code The current character code.
* @return {ol.expression.Token} Punctuator token. * @return {ol.expression.Token} Punctuator token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanPunctuator_ = function() { ol.expression.Lexer.prototype.scanPunctuator_ = function(code) {
var code = this.getCurrentCharCode_();
// single char punctuation that also doesn't start longer punctuation // single char punctuation that also doesn't start longer punctuation
// (we disallow assignment, so no += etc.) // (we disallow assignment, so no += etc.)
@@ -700,11 +700,11 @@ ol.expression.Lexer.prototype.scanPunctuator_ = function() {
/** /**
* Scan string literal token. * Scan string literal token.
* *
* @param {number} quote The current character code.
* @return {ol.expression.Token} String literal token. * @return {ol.expression.Token} String literal token.
* @private * @private
*/ */
ol.expression.Lexer.prototype.scanStringLiteral_ = function() { ol.expression.Lexer.prototype.scanStringLiteral_ = function(quote) {
var quote = this.getCurrentCharCode_();
goog.asserts.assert(quote === ol.expression.Char.SINGLE_QUOTE || goog.asserts.assert(quote === ol.expression.Char.SINGLE_QUOTE ||
quote === ol.expression.Char.DOUBLE_QUOTE, quote === ol.expression.Char.DOUBLE_QUOTE,
'Strings must start with a quote: ' + String.fromCharCode(quote)); 'Strings must start with a quote: ' + String.fromCharCode(quote));

View File

@@ -95,7 +95,7 @@ describe('ol.expression.Lexer', function() {
function scan(source) { function scan(source) {
var lexer = new ol.expression.Lexer(source); var lexer = new ol.expression.Lexer(source);
return lexer.scanIdentifier_(); return lexer.scanIdentifier_(lexer.getCurrentCharCode_());
} }
it('works for short identifiers', function() { it('works for short identifiers', function() {
@@ -182,7 +182,7 @@ describe('ol.expression.Lexer', function() {
function scan(source) { function scan(source) {
var lexer = new ol.expression.Lexer(source); var lexer = new ol.expression.Lexer(source);
return lexer.scanNumericLiteral_(); return lexer.scanNumericLiteral_(lexer.getCurrentCharCode_());
} }
it('works for integers', function() { it('works for integers', function() {
@@ -233,7 +233,7 @@ describe('ol.expression.Lexer', function() {
function scan(source) { function scan(source) {
var lexer = new ol.expression.Lexer(source); var lexer = new ol.expression.Lexer(source);
return lexer.scanPunctuator_(); return lexer.scanPunctuator_(lexer.getCurrentCharCode_());
} }
it('works for dot', function() { it('works for dot', function() {
@@ -320,7 +320,7 @@ describe('ol.expression.Lexer', function() {
function scan(source) { function scan(source) {
var lexer = new ol.expression.Lexer(source); var lexer = new ol.expression.Lexer(source);
return lexer.scanStringLiteral_(); return lexer.scanStringLiteral_(lexer.getCurrentCharCode_());
} }
it('parses double quoted string', function() { it('parses double quoted string', function() {