-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer.js
66 lines (52 loc) · 1.33 KB
/
buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function() {
if(typeof window.Buffer != 'undefined') {
var _Buffer = window.Buffer;
}
var Buffer = window.Buffer = function(args) {
if(this instanceof arguments.callee) {
this.init.apply(this, args && args.callee ? args : arguments);
} else {
return new Buffer(arguments);
}
};
Buffer.no_conflict = function() {
window.Buffer = _Buffer;
return Buffer;
};
Buffer.prototype = {
init: function(text) {
this._text = text;
this._pos = 0;
},
read: function() {
var c = this.peek();
if(c === false) {
return false;
}
this.eat();
return c;
},
read_until: function(pred) {
var start = this._pos
while(this.peek() !== false && !pred(this.peek())) {
this.eat();
};
return this._text.substring(start, this._pos);
},
eat: function() {
++this._pos;
if(this._pos > this._text.length) {
throw new Error('Unexpected end of file at position ' + this._pos);
}
},
peek: function() {
if(this._pos >= this._text.length) {
return false;
}
return this._text.charAt(this._pos);
},
get: function(index) {
return this._text.charAt(index != undefined ? index : this._pos);
}
};
})();