Module

DynamicBuffer

#DBuffer

newtype DBuffer

A growable Buffer type for binary data

#Offset

type Offset = Int

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

#create

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

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

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

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

fromUtf8 :: String -> Effect DBuffer

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

#contents

contents :: DBuffer -> Effect Uint8Array

Extracts the contents of this DBuffer

#unsafeContents

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

size :: DBuffer -> Effect Int

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

#debugToString

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