|
2 | 2 |
|
3 | 3 | const Buffer = require('buffer').Buffer;
|
4 | 4 |
|
5 |
| -module.exports = BufferList; |
| 5 | +module.exports = class BufferList { |
| 6 | + constructor() { |
| 7 | + this.head = null; |
| 8 | + this.tail = null; |
| 9 | + this.length = 0; |
| 10 | + } |
6 | 11 |
|
7 |
| -function BufferList() { |
8 |
| - this.head = null; |
9 |
| - this.tail = null; |
10 |
| - this.length = 0; |
11 |
| -} |
| 12 | + push(v) { |
| 13 | + const entry = { data: v, next: null }; |
| 14 | + if (this.length > 0) |
| 15 | + this.tail.next = entry; |
| 16 | + else |
| 17 | + this.head = entry; |
| 18 | + this.tail = entry; |
| 19 | + ++this.length; |
| 20 | + } |
12 | 21 |
|
13 |
| -BufferList.prototype.push = function(v) { |
14 |
| - const entry = { data: v, next: null }; |
15 |
| - if (this.length > 0) |
16 |
| - this.tail.next = entry; |
17 |
| - else |
| 22 | + unshift(v) { |
| 23 | + const entry = { data: v, next: this.head }; |
| 24 | + if (this.length === 0) |
| 25 | + this.tail = entry; |
18 | 26 | this.head = entry;
|
19 |
| - this.tail = entry; |
20 |
| - ++this.length; |
21 |
| -}; |
| 27 | + ++this.length; |
| 28 | + } |
22 | 29 |
|
23 |
| -BufferList.prototype.unshift = function(v) { |
24 |
| - const entry = { data: v, next: this.head }; |
25 |
| - if (this.length === 0) |
26 |
| - this.tail = entry; |
27 |
| - this.head = entry; |
28 |
| - ++this.length; |
29 |
| -}; |
| 30 | + shift() { |
| 31 | + if (this.length === 0) |
| 32 | + return; |
| 33 | + const ret = this.head.data; |
| 34 | + if (this.length === 1) |
| 35 | + this.head = this.tail = null; |
| 36 | + else |
| 37 | + this.head = this.head.next; |
| 38 | + --this.length; |
| 39 | + return ret; |
| 40 | + } |
30 | 41 |
|
31 |
| -BufferList.prototype.shift = function() { |
32 |
| - if (this.length === 0) |
33 |
| - return; |
34 |
| - const ret = this.head.data; |
35 |
| - if (this.length === 1) |
| 42 | + clear() { |
36 | 43 | this.head = this.tail = null;
|
37 |
| - else |
38 |
| - this.head = this.head.next; |
39 |
| - --this.length; |
40 |
| - return ret; |
41 |
| -}; |
42 |
| - |
43 |
| -BufferList.prototype.clear = function() { |
44 |
| - this.head = this.tail = null; |
45 |
| - this.length = 0; |
46 |
| -}; |
| 44 | + this.length = 0; |
| 45 | + } |
47 | 46 |
|
48 |
| -BufferList.prototype.join = function(s) { |
49 |
| - if (this.length === 0) |
50 |
| - return ''; |
51 |
| - var p = this.head; |
52 |
| - var ret = '' + p.data; |
53 |
| - while (p = p.next) |
54 |
| - ret += s + p.data; |
55 |
| - return ret; |
56 |
| -}; |
| 47 | + join(s) { |
| 48 | + if (this.length === 0) |
| 49 | + return ''; |
| 50 | + var p = this.head; |
| 51 | + var ret = '' + p.data; |
| 52 | + while (p = p.next) |
| 53 | + ret += s + p.data; |
| 54 | + return ret; |
| 55 | + } |
57 | 56 |
|
58 |
| -BufferList.prototype.concat = function(n) { |
59 |
| - if (this.length === 0) |
60 |
| - return Buffer.alloc(0); |
61 |
| - if (this.length === 1) |
62 |
| - return this.head.data; |
63 |
| - const ret = Buffer.allocUnsafe(n >>> 0); |
64 |
| - var p = this.head; |
65 |
| - var i = 0; |
66 |
| - while (p) { |
67 |
| - p.data.copy(ret, i); |
68 |
| - i += p.data.length; |
69 |
| - p = p.next; |
| 57 | + concat(n) { |
| 58 | + if (this.length === 0) |
| 59 | + return Buffer.alloc(0); |
| 60 | + if (this.length === 1) |
| 61 | + return this.head.data; |
| 62 | + const ret = Buffer.allocUnsafe(n >>> 0); |
| 63 | + var p = this.head; |
| 64 | + var i = 0; |
| 65 | + while (p) { |
| 66 | + p.data.copy(ret, i); |
| 67 | + i += p.data.length; |
| 68 | + p = p.next; |
| 69 | + } |
| 70 | + return ret; |
70 | 71 | }
|
71 |
| - return ret; |
72 | 72 | };
|
0 commit comments