gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.12 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
import{sInt as t}from"../safe/safe.js";import{AByteWriter as r}from"./_AByteWriter.js";const e=Symbol.for("nodejs.util.inspect.custom");export class ByteWriter extends r{tryWriteByte(t){return!(this._ptr>=this._buff.length)&&(this._buff[this._ptr++]=t,!0)}tryWrite(t){const r=this.space;return t.length>r?(this._buff.set(t.subarray(0,r),this._ptr),this._ptr=this._buff.length,!1):(this._buff.set(t,this._ptr),this._ptr+=t.length,!0)}writeByte(t){if(this._ptr>=this._buff.length)throw new Error("not enough space");this._buff[this._ptr++]=t}write(t){if(this._ptr+t.length>this._buff.length)throw new Error("not enough space");this._buff.set(t,this._ptr),this._ptr+=t.length}sub(r,e=!1){t("len",r).unsigned().atMost(this._buff.length-this._ptr).throwNot();const s=new ByteWriter(this._buff.subarray(this._ptr,this._ptr+r));return e||(this._ptr+=r),s}get[Symbol.toStringTag](){return"ByteWriter"}[e](){return`ByteWriter([${this._buff.length}]@${this._ptr})`}static mount(t){return new ByteWriter(t)}static size(t){const r=new Uint8Array(t);return new ByteWriter(r)}}