# cp.buffer

Internal byte buffer type. Allows additional chunks of bytes to be concatonated relatively inexpensively, as well as peek and pop operations to preview/read in chunks of bytes.

For example:

local buff = buffer.new()
buff:push("Hello")
buff:len()       -- 5
buff:peek(2)     -- "He"
buff:peek(7)     -- nil
buff:write(" world!")
buff:len()       -- 12
buff:peek(7)     -- "Hello w"
buff:pop(3)      -- "Hel"
buff:len()       -- 9
buff:bytes()     -- "lo world!"

# API Overview

Functions - API calls offered directly by the extension

  • is

Constructors - API calls which return an object, typically one that offers API methods

  • clone
  • fromHex
  • new

Methods - API calls which can only be made on an object returned by a constructor

  • drop
  • len
  • peek
  • pop
  • push

# API Documentation

# Functions

# is

Signature cp.buffer.is(value) -> boolean
Type Function
Description Checks if the value is an instance of a buffer.
Parameters
  • value - The value to check.
Returns
  • true if the value is an instance of buffer.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 48

# Constructors

# clone

Signature cp.buffer.clone(otherBuffer) -> buffer
Type Constructor
Description Creates a copy of the provided buffer. It shares data with the original, but can be modified via pop/push, etc without affecting the original.
Parameters
  • otherBuffer - The buffer to clone.
Returns
  • the clone of the original buffer.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 110

# fromHex

Signature cp.buffer.fromHex(hexString[, spacer]) -> cp.buffer
Type Constructor
Description Creates a buffer from the bytes represented by the provided hex string.
Parameters
  • hexString - The string of hex characters to convert.
  • spacer - The character to ignore as a spacer. Defaults to space (" ").
Returns
  • The new buffer.
Notes
  • Examples:
  • buffer.fromHex("ABCDE")
  • buffer.fromHex("12-34-56", "-")
Examples None
Source src/extensions/cp//buffer.lua line 90

# new

Signature cp.buffer.new(...) -> buffer
Type Constructor
Description Creates a new byte string buffer containing the provided string chunks.
Parameters
  • ... - The new string chunks to add to the end of the buffer.
Returns
  • The new buffer.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 65

# Methods

# drop

Signature cp.buffer:drop(len) -> boolean
Type Method
Description Drops the specified len of bytes from the start of the buffer.
Parameters
  • len - The number of bytes to read.
Returns
  • true if successful, or false if there are not enough bytes available for the requested len.
Notes
  • Equivalent to, but more efficient than pop if you don't need the bytes being dropped.
Examples None
Source src/extensions/cp//buffer.lua line 267

# len

Signature cp.buffer:len() -> number
Type Method
Description Returns the total number of bytes in the buffer.
Parameters
  • None
Returns
  • The number of bytes in the buffer.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 134

# peek

Signature cp.buffer:peek(len) -> string | nil
Type Method
Description Reads the specified len of bytes from the start of the buffer without removing them.
Parameters
  • len - The number of bytes to read.
Returns
  • The string of bytes or nil if there are not enough bytes available for the requested len.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 214

# pop

Signature cp.buffer:pop(len) -> string | nil
Type Method
Description Reads the specified len of bytes from the start of the buffer, removing them.
Parameters
  • len - The number of bytes to read.
Returns
  • The string of bytes or nil if there are not enough bytes available for the requested len.
Notes None
Examples None
Source src/extensions/cp//buffer.lua line 227

# push

Signature cp.buffer:push(...) -> buffer
Type Method
Description Pushes the provided strings onto the end of the buffer.
Parameters
  • ... - The new string chunks to add to the end of the buffer.
Returns
  • The same buffer instance.
Notes
  • Throws an error if more than cp.buffer.maxChunks are currently in the buffer when a new value is pushed.
Examples None
Source src/extensions/cp//buffer.lua line 240