chronos/bipbuffer

Source   Edit  

This module implements Bip Buffer (bi-partite circular buffer) by Simone Cooke.

The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping one head and tail pointer to the data in the buffer, it maintains two revolving regions, allowing for fast data access without having to worry about wrapping at the end of the buffer. Buffer allocations are always maintained as contiguous blocks, allowing the buffer to be used in a highly efficient manner with API calls, and also reducing the amount of copying which needs to be performed to put data into the buffer. Finally, a two-phase allocation system allows the user to pessimistically reserve an area of buffer space, and then trim back the buffer to commit to only the space which was used.

https://www.codeproject.com/Articles/3479/The-Bip-Buffer-The-Circular-Buffer-with-a-Twist

Types

BipBuffer = object
Source   Edit  

Procs

proc addLineInto(bp: BipBuffer; tgt: var string; state: var int; limit: int;
                 sep: openArray[char]): tuple[n: Natural, done: bool] {.
    ...raises: [], tags: [], forbids: [].}
Add bytes from buffer until sep found, limit is hit or buffer ends - sep is removed from the tgt if found. Source   Edit  
func availSpace(bp: BipBuffer): Natural {....raises: [], tags: [], forbids: [].}
Returns amount of space available for reserve in buffer bp. Source   Edit  
proc commit(bp: var BipBuffer; size: Natural) {....raises: [], tags: [],
    forbids: [].}
Updates structure's pointers when new data inserted into buffer. Source   Edit  
proc consume(bp: var BipBuffer; size: Natural) {....raises: [], tags: [],
    forbids: [].}
The procedure removes/frees size bytes from the buffer bp. Source   Edit  
proc copyInto(bp: var BipBuffer; tgt: var openArray[byte]): Natural {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc copyUntil[T: byte | char](bp: BipBuffer; tgt: var openArray[T];
                               state: var int; sep: openArray[T]): tuple[
    n: Natural, found: bool] {....raises: [].}
Bulk-copies the available bytes Source   Edit  
proc init(t: typedesc[BipBuffer]; size: int): BipBuffer {....raises: [].}
Creates new Bip Buffer with size size. Source   Edit  
func len(bp: BipBuffer): Natural {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc reserve(bp: var BipBuffer; size: Natural = 0): tuple[data: ptr byte,
    size: Natural] {....raises: [], tags: [], forbids: [].}

Reserve size bytes in buffer.

If size == 0 (default) reserve all available space from buffer.

If there is not enough space in buffer for resevation - error will be returned.

Returns current reserved range as pointer of type pt and size of type st.

Source   Edit  

Iterators

iterator items(bp: BipBuffer): byte {....raises: [], tags: [], forbids: [].}
Iterates over all the bytes in the buffer. Source   Edit  
iterator regions(bp: var BipBuffer): tuple[data: ptr byte, size: Natural] {.
    ...raises: [], tags: [], forbids: [].}
Iterates over all the regions (a and b) in the buffer. Source   Edit