Module

DynamicBuffer

Package
purescript-dynamic-buffers
Repository
kritzcreek/purescript-dynamic-buffer

#DBuffer Source

newtype DBuffer

A growable Buffer type for binary data

#Offset Source

type Offset = Int

A byte-offset into a Buffer. Just an alias for documentation purposes.

#create Source

create :: Int -> Effect DBuffer

Creates a new DBuffer with the given initial capacity.

Picking a capacity in the order of magnitude you expect to write will reduce the overall amount of allocation.

#addByte Source

addByte :: DBuffer -> Int -> Effect Unit

Writes a single byte into the DBuffer

Throws an exception if you pass a value that's not within (0..255)

#addBuffer Source

addBuffer :: DBuffer -> DBuffer -> Effect Unit

Adds the contents of the second buffer to the first. The contents are copied, so subsequent modifications to the second buffer don't affect the first.

import Prelude

import DynamicBuffer as DBuffer
import Debug.Trace as Debug

main = do
  b1 <- DBuffer.create 8
  b2 <- DBuffer.create 8
  DBuffer.addByte b1 10
  DBuffer.addByte b2 20
  DBuffer.addByte b2 30
  DBuffer.addBuffer b1 b2

  Debug.traceM =<< DBuffer.size b1 -- 3
  Debug.traceM =<< DBuffer.contents b1 -- Uint8Array(3) [ 10, 20, 30 ]

#setByte Source

setByte :: DBuffer -> Offset -> Int -> Effect Unit

Mutates the buffer at the given offset.

Throws an exception if you pass an offset that's not within the current Buffer's size. Not capacity.

Throws an exception if you pass a value that's not within (0..255)

#fromUtf8 Source

fromUtf8 :: String -> Effect DBuffer

Creates a DBuffer that contains the Utf8 encoding of the given String.

#contents Source

contents :: DBuffer -> Effect Uint8Array

Extracts the contents of this DBuffer

#unsafeContents Source

unsafeContents :: DBuffer -> Effect Uint8Array

Extracts the contents of this DBuffer without copying.

Careful! Modifying the contents of the DBuffer afterwards will modify the returned Uint8Array in place

#size Source

size :: DBuffer -> Effect Int

Returns the current size of the DBuffer. Not its capacity.

#debugToString Source

debugToString :: DBuffer -> String

Displays the contents of the DBuffer for debugging purposes. Don't use this in "production", it's not referentially transparent.

import Prelude

import DynamicBuffer as DBuffer
import Effect.Console as Console

main = do
  b <- DBuffer.create 8
  DBuffer.addByte b 10
  DBuffer.addByte b 20
  DBuffer.addByte b 30

  Console.log (DBuffer.debugToString b) -- [0xA, 0x14, 0x1E]
Modules
DynamicBuffer