#
buffer
A universal handler for dealing with streams, buffer data and large computable data.
This can be used to perform arithmetic, bitwise, stacks or even consumers.
Buffers can be held in reference in C to allow for continueous streams.
#
Functions
buffer.new(data?: number | string | buffer): buffer
- Creates a new buffer class.
- Passing another buffer through this just clones it.
buffer.number(data: number): buffer
- Strict number conversion to a buffer.
buffer.string(data: string): buffer
- Strict string conversion to a buffer.
buffer.hex(data: string): buffer
- Converts a hex-style string to a buffer.
buffer.bytes(data: number[]): buffer
- Converts an array of 8-bit numbers to a buffer.
- 8-bit numbers are represented as 0 - 255, anything above will get ignored due to dataloss.
buffer.binary(data: number[] | boolean[]): buffer
- Converts an array of single-bit numbers or booleans to a buffer
- Obviously if you pass anything higher than a "1" it will get ignored.
#
Basics
buffer:clone(): buffer
- Makes a clone of the current buffer.
- This is similar to
buffer.new
, in a sense you can pass a number and get a buffer from it.
buffer:size(): number
- Gets the allocated size of the buffer.
- Size is relative to how much data the buffer must use to keep relevant data.
buffer:peek(idx: number): number
- Gets a byte out of the buffer without deleting it.
buffer:insert(idx: number, value: number...)
- Inserts bytes into the buffer.
buffer:push(value: number...)
- Pushes bytes into the buffer at the start.
buffer:push_back(value: number...)
- Pushes bytes into the buffer at the end.
buffer:remove(idx: number): number
- Removes and returns a byte from the buffer at an index.
buffer:shift(): number
- Removes and returns a byte from the buffer from the front.
buffer:pop(): number
- Removes and returns a byte from the buffer from the back.
buffer:substitute(beginning: number, ending?: number): buffer
- Gets a subset of the buffer based on the start and end positions.
buffer:concat(other: number | string | buffer): buffer
- Concats two buffers or data together.
- Basically "hello " .. "world" in buffers would make "hello world".
#
Consumers
buffer:bytes(size: number): buffer
- Gets a subset of the buffer while consuming the size requested.
buffer:uint8(): number
- Gets & consumes an unsigned integer of size 8
buffer:uint8(data: number)
- Pushes back an unsigned integer of size 8
buffer:int8(): number
- Gets & consumes an integer of size 8
buffer:int8(data: number)
- Pushes back an integer of size 8
buffer:uint16(): number
- Gets & consumes an unsigned integer of size 16
buffer:uint16(data: number)
- Pushes back an unsigned integer of size 16
buffer:int16(): number
- Gets & consumes an integer of size 16
buffer:int16(data: number)
- Pushes back an integer of size 16
buffer:uint32(): number
- Gets & consumes an unsigned integer of size 32
buffer:uint32(data: number)
- Pushes back an unsigned integer of size 32
buffer:int32(): number
- Gets & consumes an integer of size 32
buffer:int32(data: number)
- Pushes back an integer of size 32
buffer:uint64(): number
- Gets & consumes an unsigned integer of size 64
buffer:uint64(data: number)
- Pushes back an unsigned integer of size 64
buffer:int64(): number
- Gets & consumes an integer of size 64
buffer:int64(data: number)
- Pushes back an integer of size 64
buffer:uleb128(): number
- Gets & consumes an unsigned integer of size 128
- This is a form of compression for large numbers
buffer:uleb128(data: number)
- Pushes back an unsigned integer of size 128
- This is a form of compression for large numbers
#
Comparators
buffer:equal(other: number | string | buffer): boolean
- If a value or buffer is equal
buffer:notequal(other: number | string | buffer): boolean
- If a value or buffer is not equal
buffer:lessthan(other: number | string | buffer): boolean
- If a value or buffer is less than
buffer:greaterthan(other: number | string | buffer): boolean
- If a value or buffer is greater than
#
Arithmetic
buffer:add(other: number | string | buffer): buffer
- Performs an arithmetic addition.
buffer:sub(other: number | string | buffer): buffer
- Performs an arithmetic subtraction.
buffer:mul(other: number | string | buffer): buffer
- Performs an arithmetic multiplication.
buffer:div(other: number | string | buffer): buffer
- Performs an arithmetic division.
buffer:pow(other: number | string | buffer): buffer
- Performs an arithmetic powers.
- Do note that this can be very intensive and memory consuming.
#
Bitwise
buffer:bnot(): buffer
- Performs a bitwise not operation.
buffer:bor(other: number | string | buffer): buffer
- Performs a bitwise or operation.
buffer:band(other: number | string | buffer): buffer
- Performs a bitwise and operation.
buffer:bxor(other: number | string | buffer): buffer
- Performs a bitwise exclusive or operation.
buffer:blshift(other: number | string | buffer): buffer
- Performs a bitwise left-shift operation.
buffer:brshift(other: number | string | buffer): buffer
- Performs a bitwise right-shift operation.
buffer:blroll(other: number | string | buffer): buffer
- Performs a bitwise left-roll operation.
buffer:brroll(other: number | string | buffer): buffer
- Performs a bitwise right-roll operation.
#
Conversions
buffer:tonumber(): number
- Converts the buffer to a number representation.
- Lua handles numbers using doubles so you may get some precision loss or missing data.
buffer:tostring(): string
- Converts the buffer to a string representation.
buffer:tohex(): string
- Converts the buffer to a hex-string representation.
buffer:totable(): number[]
- Converts the buffer to an array of 8-bits.
- The buffer internally is represented by this.
buffer:tobinary(): number[]
- Converts the buffer to an array of bits.
- Represented as 1's and 0's, not booleans.
#
Meta Operators
Support for __* operations.
buffer:operator+(other: buffer): buffer
- Adds two buffers together through arithmetrics
buffer:operator-(other: buffer): buffer
- Subtracts two buffers together through arithmetrics
buffer:operator*(other: buffer): buffer
- Multiplies two buffers together through arithmetrics
buffer:operator/(other: buffer): buffer
- Divides two buffers together through arithmetrics
buffer:operator^(other: buffer): buffer
- Pow's two buffers together through arithmetrics
buffer:operator..(other: number | string | buffer): buffer
- Concats two buffers together just like buffer:concat
buffer:operator==(other: buffer): boolean
- Compares two buffers together just like buffer:equal
buffer:operator><(other: buffer): boolean
- Compares two buffers together just like buffer:lessthan and buffer:greaterthan