Skip to content

Commit

Permalink
substring optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii.Kliuchnyk authored and tmont committed Jun 7, 2016
1 parent 5315da8 commit 833ac01
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
exports.create = function(raw, options, regex) {
var index = 0;
var index = 0,
substring = null;

var context = {
text: '',
peek: function(count) {
count = count || 1;
return this.raw.substr(index + 1, count);
return this.raw.substr(this.index + 1, count);
},
read: function(count) {
if (count === 0) {
return '';
}
count = count || 1;
var next = this.peek(count);
index += count;
if (index > this.length) {
index = this.length;
this.index += count;
if (this.index > this.length) {
this.index = this.length;
}
return next;
},
Expand All @@ -31,11 +33,11 @@ exports.create = function(raw, options, regex) {
return value;
},
isEof: function() {
return index >= this.length;
return this.index >= this.length;
},
readRegex: function(regex) {
var value = (regex.exec(this.raw.substring(this.index)) || [''])[0];
index += value.length;
this.index += value.length;
return value;
},
peekIgnoreWhitespace: function(count) {
Expand Down Expand Up @@ -67,8 +69,12 @@ exports.create = function(raw, options, regex) {
context.__defineGetter__('index', function() {
return index;
});
context.__defineSetter__('index', function(value) {
index = value;
substring = null;
});
context.__defineGetter__('substring', function() {
return this.raw.substring(this.index);
return substring === null ? (substring = this.raw.substring(this.index)) : substring;
});

context.callbacks = {};
Expand Down

0 comments on commit 833ac01

Please sign in to comment.