#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

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

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


#API Documentation

#Functions

Signaturecp.buffer.is(value) -> boolean
TypeFunction
DescriptionChecks 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 48

#Constructors

Signaturecp.buffer.clone(otherBuffer) -> buffer
TypeConstructor
DescriptionCreates 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 110

Signaturecp.buffer.fromHex(hexString[, spacer]) -> cp.buffer
TypeConstructor
DescriptionCreates 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", "-")
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 90

Signaturecp.buffer.new(...) -> buffer
TypeConstructor
DescriptionCreates 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 65

#Methods

Signaturecp.buffer:drop(len) -> boolean
TypeMethod
DescriptionDrops 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.
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 267

Signaturecp.buffer:len() -> number
TypeMethod
DescriptionReturns the total number of bytes in the buffer.
Parameters
  • None
Returns
  • The number of bytes in the buffer.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 134

Signaturecp.buffer:peek(len) -> string | nil
TypeMethod
DescriptionReads 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 214

Signaturecp.buffer:pop(len) -> string | nil
TypeMethod
DescriptionReads 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.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 227

Signaturecp.buffer:push(...) -> buffer
TypeMethod
DescriptionPushes 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.
ExamplesNone
Sourcesrc/extensions/cp//buffer.lua line 240