streams: refactor BufferList into ES6 class
PR-URL: https://github.com/nodejs/node/pull/12644 Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
parent
ed0716f0e9
commit
e2199e0fc2
@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
const Buffer = require('buffer').Buffer;
|
const Buffer = require('buffer').Buffer;
|
||||||
|
|
||||||
module.exports = BufferList;
|
module.exports = class BufferList {
|
||||||
|
constructor() {
|
||||||
function BufferList() {
|
|
||||||
this.head = null;
|
this.head = null;
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferList.prototype.push = function(v) {
|
push(v) {
|
||||||
const entry = { data: v, next: null };
|
const entry = { data: v, next: null };
|
||||||
if (this.length > 0)
|
if (this.length > 0)
|
||||||
this.tail.next = entry;
|
this.tail.next = entry;
|
||||||
@ -18,17 +17,17 @@ BufferList.prototype.push = function(v) {
|
|||||||
this.head = entry;
|
this.head = entry;
|
||||||
this.tail = entry;
|
this.tail = entry;
|
||||||
++this.length;
|
++this.length;
|
||||||
};
|
}
|
||||||
|
|
||||||
BufferList.prototype.unshift = function(v) {
|
unshift(v) {
|
||||||
const entry = { data: v, next: this.head };
|
const entry = { data: v, next: this.head };
|
||||||
if (this.length === 0)
|
if (this.length === 0)
|
||||||
this.tail = entry;
|
this.tail = entry;
|
||||||
this.head = entry;
|
this.head = entry;
|
||||||
++this.length;
|
++this.length;
|
||||||
};
|
}
|
||||||
|
|
||||||
BufferList.prototype.shift = function() {
|
shift() {
|
||||||
if (this.length === 0)
|
if (this.length === 0)
|
||||||
return;
|
return;
|
||||||
const ret = this.head.data;
|
const ret = this.head.data;
|
||||||
@ -38,14 +37,14 @@ BufferList.prototype.shift = function() {
|
|||||||
this.head = this.head.next;
|
this.head = this.head.next;
|
||||||
--this.length;
|
--this.length;
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
|
|
||||||
BufferList.prototype.clear = function() {
|
clear() {
|
||||||
this.head = this.tail = null;
|
this.head = this.tail = null;
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
BufferList.prototype.join = function(s) {
|
join(s) {
|
||||||
if (this.length === 0)
|
if (this.length === 0)
|
||||||
return '';
|
return '';
|
||||||
var p = this.head;
|
var p = this.head;
|
||||||
@ -53,9 +52,9 @@ BufferList.prototype.join = function(s) {
|
|||||||
while (p = p.next)
|
while (p = p.next)
|
||||||
ret += s + p.data;
|
ret += s + p.data;
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
|
|
||||||
BufferList.prototype.concat = function(n) {
|
concat(n) {
|
||||||
if (this.length === 0)
|
if (this.length === 0)
|
||||||
return Buffer.alloc(0);
|
return Buffer.alloc(0);
|
||||||
if (this.length === 1)
|
if (this.length === 1)
|
||||||
@ -69,4 +68,5 @@ BufferList.prototype.concat = function(n) {
|
|||||||
p = p.next;
|
p = p.next;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user