Node中 Buffer 利用 slice + indexOf 生成 split 方法

794 阅读1分钟

让你明明白白学知识,有代码,有讲解,抄的走,学的会!

具体实现

Buffer.prototype.split = Buffer.prototype.split || function (seq) {
  // 存储最终切割的内容
  let arr = [];

  // 当前位置--
  let current = 0;

  // pos--在buffer中找到某个符合seq条件片段的索引位置
  let position = 0;

  while (this.indexOf(seq, current) !== -1) {
    // 在这个位置找到符合条件的内容
    position = this.indexOf(seq, current)

    // 将符合条件的前面一段buffer切出来
    let prevBuffer = this.slice(current, position)

    arr.push(prevBuffer);

    // 现在从新的位置开始继续找
    current = position + seq.length;
  }

  // 上面while结束,表示再也找不到符合seq的buffer了,现在需要将最后一段buffer装进数组
  arr.push(this.slice(current));

  return arr;
}

// 测试
let  buf  = Buffer.from('1,2,3,4,5');

let arr = buf.split(',')

console.log(arr)
console.log("第一段buffer,转成字符串--》",arr[0].toString())
console.log("第2段buffer,转成字符串--》",arr[1].toString())
console.log("第3段buffer,转成字符串--》",arr[2].toString())

效果截图

以 空格 分割内容

// 这里还是以上面的Buffer上挂载的 split方法为例
let buf2 = Buffer.from('I brush my teeth with a toothbrush in the morning')

let arr2 = buf2.split(' ')
console.log(arr2)
console.log("第一段buffer,转成字符串--》",arr2[0].toString(), '-----原始Buffer-->',arr2[0])
console.log("第2段buffer,转成字符串--》",arr2[1].toString())
console.log("第3段buffer,转成字符串--》",arr2[2].toString())

在原生Node的Buffer中不能直接使用split方法, 会报下面的错误,一定要自己去实现这个方法,且挂载到原型链上

split is not a function