No description
Find a file
2023-08-25 14:49:27 +03:00
bitstreams.nim fix bug in endian swapping algorithm 2023-08-25 14:49:27 +03:00
bitstreams.nimble add files 2020-12-10 15:16:34 +02:00
LICENSE Initial commit 2020-12-10 14:52:52 +02:00
README.md added string constructor 2021-03-19 02:48:35 +02:00

bitstreams

Interface for reading/writing per bits

How it works

This module is a wrapper around stdlib's streams module.

type
  BitStream* = ref object
    stream: Stream
    buffer: uint64
    bitsLeft: int

bitsLeft keeps track of how many bits in buffer are not read by the end-user, then:

  • if they are more than the user requests, then no real read happens on steam
  • if they are less than the user requests, data are read from stream into buffer

Constructor & Modes

Two constructors are provided:

proc newFileBitStream*(f: string; mode = fmRead; size = -1): BitStream
proc newStringBitStream*(s = ""): BitStream

fmWrite and fmAppend are disallowed because when writing to a bitstream, reads on the underlying Stream object are necessary.