2011-11-18 13:57:07 +00:00
|
|
|
var buffer = require('buffer');
|
|
|
|
var HTML5 = require('../html5');
|
|
|
|
|
|
|
|
function Buffer() {
|
|
|
|
this.data = '';
|
|
|
|
this.start = 0;
|
2012-06-05 21:32:26 +00:00
|
|
|
this.committed = 0;
|
2011-11-18 13:57:07 +00:00
|
|
|
var eof;
|
|
|
|
this.__defineSetter__('eof', function(f) {
|
|
|
|
eof = f
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.eof=', f)
|
2011-11-18 13:57:07 +00:00
|
|
|
})
|
|
|
|
this.__defineGetter__('eof', function() { return eof })
|
|
|
|
this.eof = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.Buffer = Buffer;
|
|
|
|
|
|
|
|
Buffer.prototype = {
|
|
|
|
slice: function() {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.slice')
|
2011-11-18 13:57:07 +00:00
|
|
|
if(this.start >= this.data.length) {
|
|
|
|
if(!this.eof) throw HTML5.DRAIN
|
|
|
|
return HTML5.EOF;
|
|
|
|
}
|
|
|
|
return this.data.slice(this.start, this.data.length);
|
|
|
|
},
|
|
|
|
char: function() {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.char')
|
2011-11-18 13:57:07 +00:00
|
|
|
if(!this.eof && this.start >= this.data.length - 1) throw HTML5.DRAIN;
|
|
|
|
if(this.start >= this.data.length) {
|
|
|
|
return HTML5.EOF;
|
|
|
|
}
|
|
|
|
return this.data[this.start++];
|
|
|
|
},
|
|
|
|
advance: function(amount) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.advance', amount)
|
2011-11-18 13:57:07 +00:00
|
|
|
this.start += amount;
|
|
|
|
if(this.start >= this.data.length) {
|
|
|
|
if(!this.eof) throw HTML5.DRAIN;
|
2012-06-05 21:32:26 +00:00
|
|
|
return HTML5.EOF;
|
2011-11-18 13:57:07 +00:00
|
|
|
} else {
|
|
|
|
if(this.committed > this.data.length / 2) {
|
|
|
|
// Sliiiide
|
|
|
|
this.data = this.data.slice(this.committed);
|
|
|
|
this.start = this.start - this.committed;
|
|
|
|
this.committed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
matchWhile: function(re) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.matchWhile', re);
|
|
|
|
if(this.eof && this.start >= this.data.length ) return '';
|
2011-11-18 13:57:07 +00:00
|
|
|
var r = new RegExp("^"+re+"+");
|
2012-06-05 21:32:26 +00:00
|
|
|
var m;
|
2011-11-18 13:57:07 +00:00
|
|
|
if(m = r.exec(this.slice())) {
|
|
|
|
if(!this.eof && m[0].length == this.data.length - this.start) throw HTML5.DRAIN;
|
|
|
|
this.advance(m[0].length);
|
|
|
|
return m[0];
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
matchUntil: function(re) {
|
2012-06-05 21:32:26 +00:00
|
|
|
var m, s;
|
|
|
|
HTML5.debug('buffer.matchUntil', re);
|
|
|
|
s = this.slice();
|
|
|
|
if(s === HTML5.EOF) {
|
|
|
|
return '';
|
|
|
|
} else if(m = new RegExp(re + (this.eof ? "|\0|$" : "|\0")).exec(this.slice())) {
|
2011-11-18 13:57:07 +00:00
|
|
|
var t = this.data.slice(this.start, this.start + m.index);
|
|
|
|
this.advance(m.index);
|
|
|
|
return t.toString();
|
|
|
|
} else {
|
2012-06-05 21:32:26 +00:00
|
|
|
throw HTML5.DRAIN;
|
2011-11-18 13:57:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
append: function(data) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.append', data);
|
2011-11-18 13:57:07 +00:00
|
|
|
this.data += data
|
|
|
|
},
|
|
|
|
shift: function(n) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.shift', n);
|
2011-11-18 13:57:07 +00:00
|
|
|
if(!this.eof && this.start + n >= this.data.length) throw HTML5.DRAIN;
|
2012-06-05 21:32:26 +00:00
|
|
|
if(this.eof && this.start >= this.data.length) return HTML5.EOF;
|
2011-11-18 13:57:07 +00:00
|
|
|
var d = this.data.slice(this.start, this.start + n).toString();
|
|
|
|
this.advance(Math.min(n, this.data.length - this.start));
|
|
|
|
return d;
|
|
|
|
},
|
|
|
|
peek: function(n) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.peek', n)
|
2011-11-18 13:57:07 +00:00
|
|
|
if(!this.eof && this.start + n >= this.data.length) throw HTML5.DRAIN;
|
2012-06-05 21:32:26 +00:00
|
|
|
if(this.eof && this.start >= this.data.length) return HTML5.EOF;
|
2011-11-18 13:57:07 +00:00
|
|
|
return this.data.slice(this.start, Math.min(this.start + n, this.data.length)).toString();
|
|
|
|
},
|
|
|
|
length: function() {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.length')
|
2011-11-18 13:57:07 +00:00
|
|
|
return this.data.length - this.start - 1;
|
|
|
|
},
|
|
|
|
unget: function(d) {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.unget', d);
|
|
|
|
if(d === HTML5.EOF) return;
|
2011-11-18 13:57:07 +00:00
|
|
|
this.start -= (d.length);
|
|
|
|
},
|
|
|
|
undo: function() {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.undo')
|
2011-11-18 13:57:07 +00:00
|
|
|
this.start = this.committed;
|
|
|
|
},
|
|
|
|
commit: function() {
|
2012-06-05 21:32:26 +00:00
|
|
|
HTML5.debug('buffer.commit')
|
2011-11-18 13:57:07 +00:00
|
|
|
this.committed = this.start;
|
|
|
|
}
|
|
|
|
}
|