tink_chunk 0.1.0

Released 2016-12-13.

To install, run:

haxelib install tink_chunk 0.1.0

See using Haxelib in Haxelib documentation for more information.

Maintainerback2dos
Websitehttps://github.com/haxetink/tink_chunk/
Current version0.1.0
StatisticsInstalled 75 times
LicenseMIT
Tags binary, bytes, cross, io, tink

README.md

Tinkerbell Binary Chunks

This library provides an abstraction for readonly binary data, that can be sliced and concatenated without copying the actual payload.

abstract Chunk {
  
  var length(get, never):Int;    
  function concat(that:Chunk):Chunk;
    
  function cursor():ChunkCursor;//see below
  function iterator():Iterator;
      
  function slice(from:Int, to:Int):Chunk;    
  function blitTo(target:Bytes, offset:Int):Void;
  
  static var EMPTY(default, null):Chunk;  
  
  //Some implicit conversions:
  
  @:to function toString():String;
  @:to function toBytes():Bytes;
    
  @:from static private function ofBytes(b:Bytes):Chunk;
  @:from static private function ofString(s:String):Chunk;
    
  //A few defitions for `&` to concatenate with other chunks but also strings and bytes
  
  @:op(a & b) static private function catChunk(a:Chunk, b:Chunk):Chunk;    
  @:op(a & b) static private function rcatString(a:Chunk, b:String):Chunk;
  @:op(a & b) static private function lcatString(a:String, b:Chunk):Chunk;
  @:op(a & b) static private function lcatBytes(a:Bytes, b:Chunk):Chunk;
  @:op(a & b) static private function rcatBytes(a:Chunk, b:Bytes):Chunk; 
}

class ChunkCursor {
  var length(default, null):Int;
  var currentPos(default, null):Int;
  var currentByte(default, null):Int;
  function next():Bool;
}

The cursor is not unlike a normal iterator. It's API will however be expanded for seeking and other niceties. To iterate over just a part of a chunk just grab the slice you need first and then create a cursor or iterator.