netkit/buffer/vector

This module implements a growable buffer VectorBuffer. The buffer can grow exponentially as needed until it reaches a critical value. When the critical value is reached, continued growth will cause an exception.

Types

VectorBuffer = object of RootObj
  value: seq[byte]
  endPos: Natural
  capacity: Natural
  minCapacity: Natural
  maxCapacity: Natural
A growable buffer.   Source Edit

Procs

proc initVectorBuffer(minCapacity: Natural = BufferSize;
                     maxCapacity: Natural = BufferSize * 8): VectorBuffer {...}{.raises: [],
    tags: [].}
Initializes an VectorBuffer .   Source Edit
proc capacity(b: VectorBuffer): Natural {...}{.raises: [], tags: [].}
Returns the capacity of this buffer.   Source Edit
proc minCapacity(b: VectorBuffer): Natural {...}{.raises: [], tags: [].}
Returns the minimum capacity of this buffer.   Source Edit
proc maxCapacity(b: VectorBuffer): Natural {...}{.raises: [], tags: [].}
Returns the maximum capacity of this buffer.   Source Edit
proc len(b: VectorBuffer): Natural {...}{.raises: [], tags: [].}
Returns the length of the data currently stored in this buffer.   Source Edit
proc reset(b: var VectorBuffer): Natural {...}{.raises: [], tags: [].}
Resets the buffer to restore to the original capacity while clear all stored data.   Source Edit
proc expand(b: var VectorBuffer) {...}{.raises: [OverflowError], tags: [].}
Expands the capacity of the buffer. If it exceeds the maximum capacity, an exception is raised.   Source Edit
proc next(b: var VectorBuffer): (pointer, Natural) {...}{.raises: [], tags: [].}

Gets the next safe storage space. The return value includes the address and length of the storable space.

Examples:

var source = "Hello World"
var (regionPtr, regionLen) = buffer.next()
var length = min(regionLen, s.len)
copyMem(regionPtr, source.cstring, length) 
  Source Edit
proc pack(b: var VectorBuffer; size: Natural): Natural {...}{.raises: [], tags: [].}

Tells the buffer that size bytes from the current storage location are promoted to data. Returns the actual length promoted.

When next() is called, data is written to the storage space inside the buffer, but the buffer cannot know how much data was written. pack () tells the buffer the length of the data written.

Whenever next() is called, pack() should be called immediately.

Examples:

var source = "Hello World"
var (regionPtr, regionLen) = buffer.next()
var length = min(regionLen, s.len)
copyMem(regionPtr, source.cstring, length)
var n = buffer.pack(length)
  Source Edit
proc add(b: var VectorBuffer; source: pointer; size: Natural): Natural {...}{.raises: [],
    tags: [].}

Copies up to size lengths of data from source and store the data in the buffer. Returns the actual length copied. This is a simplified version of the next pack combination call. The difference is that an additional copy operation is made instead of writing directly to the buffer.

Examples:

var source = "Hello World"
var n = buffer.add(source.cstring, source.len)
  Source Edit
proc get(b: var VectorBuffer; dest: pointer; size: Natural; start: Natural): Natural {...}{.
    raises: [], tags: [].}
Gets up to size of the stored data from start position, copy the data to the space dest. Returns the actual number copied.   Source Edit
proc clear(b: var VectorBuffer): Natural {...}{.raises: [], tags: [].}
Deletes all the stored data.   Source Edit