buffer copy
for(let i=sourceStart;i<sourceEnd;i++){
targetBuffer[targetStart++]=this[i];
}
}
concat
Buffer.concat = function(bufferList,len=bufferList.reduce((a,b)=>a+b.length,0)){
let buffer = Buffer.alloc(len);
let offset = 0;
bufferList.forEach(buf=>{
buf.copy(buffer,offset);
offset += buf.length;
})
return buffer;
}
split
Buffer.prototype.split = function(sep){ // slice + indexOf = split
let arr = [];
let len = Buffer.from(sep).length; // 分割符号的长度
let offset = 0;
let current;
while(-1!=(current = this.indexOf(sep,offset))){
// 找到的位置 加上偏移量
arr.push(this.slice(offset,current));
offset = current+len;
}
arr.push(this.slice(offset));
return arr;
}