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;
|
||||
|
||||
module.exports = BufferList;
|
||||
|
||||
function BufferList() {
|
||||
module.exports = class BufferList {
|
||||
constructor() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
this.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
BufferList.prototype.push = function(v) {
|
||||
push(v) {
|
||||
const entry = { data: v, next: null };
|
||||
if (this.length > 0)
|
||||
this.tail.next = entry;
|
||||
@ -18,17 +17,17 @@ BufferList.prototype.push = function(v) {
|
||||
this.head = entry;
|
||||
this.tail = entry;
|
||||
++this.length;
|
||||
};
|
||||
}
|
||||
|
||||
BufferList.prototype.unshift = function(v) {
|
||||
unshift(v) {
|
||||
const entry = { data: v, next: this.head };
|
||||
if (this.length === 0)
|
||||
this.tail = entry;
|
||||
this.head = entry;
|
||||
++this.length;
|
||||
};
|
||||
}
|
||||
|
||||
BufferList.prototype.shift = function() {
|
||||
shift() {
|
||||
if (this.length === 0)
|
||||
return;
|
||||
const ret = this.head.data;
|
||||
@ -38,14 +37,14 @@ BufferList.prototype.shift = function() {
|
||||
this.head = this.head.next;
|
||||
--this.length;
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
BufferList.prototype.clear = function() {
|
||||
clear() {
|
||||
this.head = this.tail = null;
|
||||
this.length = 0;
|
||||
};
|
||||
}
|
||||
|
||||
BufferList.prototype.join = function(s) {
|
||||
join(s) {
|
||||
if (this.length === 0)
|
||||
return '';
|
||||
var p = this.head;
|
||||
@ -53,9 +52,9 @@ BufferList.prototype.join = function(s) {
|
||||
while (p = p.next)
|
||||
ret += s + p.data;
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
BufferList.prototype.concat = function(n) {
|
||||
concat(n) {
|
||||
if (this.length === 0)
|
||||
return Buffer.alloc(0);
|
||||
if (this.length === 1)
|
||||
@ -69,4 +68,5 @@ BufferList.prototype.concat = function(n) {
|
||||
p = p.next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user