Buffer中的split方法分析
Buffer中存在indexOf,slice,split方法,前两者可以直接使用,而split方法需要进行造轮子才可以使用,分析如下
let b=new Buffer("aaa---bbb---ccc---ddd");
Buffer.prototype.split=Buffer.prototype.split || function (spl) {//spl表示分隔符
let arr=[];//定义数组接收分隔出来的内容
let cur=0;//表示当前遍历的位置
let n=0;//用来接收spl分隔符索引到的位置
while((n=this.indexOf(spl,cur))!=-1){
//如果索引值存在,即不为-1, 同时将索引到的位置赋值给n。
arr.push(this.slice(cur,n));//切割从当前位置到索引位置,再将该段字符串存到数组中
cur=n+spl.length;//当前位置向后进行遍历,寻找下一个分隔符
}
return arr;
}
let array=b.split('---')
console.log(array);
console.log(array.map(array => array.toString()));//将二进制数据转化为字符串