You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js中实现类似Go bytes.Split的Buffer按分隔符分割功能?

How to Split a Buffer by a Separator in Node.js

Great question! If you're coming from Go and used to bytes.Split() for splitting buffers, Node.js has a couple of solid approaches to achieve the same result—whether you're working with text-based buffers or raw binary data. Let's break them down:

Method 1: String Conversion (Simple for Text Buffers)

If your buffer contains UTF-8 encoded text (like most common use cases), you can convert the buffer to a string, split it with the standard split() method, then convert each segment back to a Buffer. This is straightforward and easy to read:

function splitBuffer(buffer, separator) {
  // Convert the separator to a Buffer if it's passed as a string
  const sepBuffer = Buffer.from(separator);
  // Split the buffer's string representation, then map each part back to Buffer
  return buffer.toString().split(sepBuffer.toString()).map(part => Buffer.from(part));
}

// Example usage
const myBuffer = Buffer.from("user=john=age=30");
const resultArray = splitBuffer(myBuffer, "=");
console.log(resultArray); 
// Output: [ <Buffer 75 73 65 72>, <Buffer 6a 6f 68 6e>, <Buffer 61 67 65>, <Buffer 33 30> ]

Note: This method works best for text data. If your buffer contains non-UTF-8 binary data, converting to a string might corrupt the data—use the next method instead.

Method 2: Pure Buffer Manipulation (Safe for Binary Data)

For raw binary buffers (or when you want to avoid encoding/decoding overhead), you can manually find the separator positions using buffer.indexOf() and slice the buffer accordingly. This mimics how Go's bytes.Split() works under the hood:

function splitBuffer(buffer, separator) {
  // Normalize the separator to a Buffer (handle both string and Buffer inputs)
  const sep = typeof separator === 'string' ? Buffer.from(separator) : separator;
  const sepLength = sep.length;
  const parts = [];
  let currentStart = 0;
  let separatorIndex;

  // Loop until no more separators are found
  while ((separatorIndex = buffer.indexOf(sep, currentStart)) !== -1) {
    // Slice the buffer from current start to the separator's position
    parts.push(buffer.slice(currentStart, separatorIndex));
    // Move the start pointer past the separator
    currentStart = separatorIndex + sepLength;
  }

  // Add the final segment after the last separator
  parts.push(buffer.slice(currentStart));
  return parts;
}

// Example with binary data
const binaryBuffer = Buffer.from([0x01, 0x02, 0x3d, 0x03, 0x04, 0x3d, 0x05]);
const binaryResult = splitBuffer(binaryBuffer, Buffer.from([0x3d])); // Split by "=" (0x3d in hex)
console.log(binaryResult);
// Output: [ <Buffer 01 02>, <Buffer 03 04>, <Buffer 05> ]

This method is more robust for non-text use cases and avoids any potential data corruption from string conversion.

内容的提问来源于stack exchange,提问作者Akashii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:42:15