Skip to content

Commit

Permalink
Added examples for endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Nov 20, 2024
1 parent 8578908 commit f68fc19
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,72 @@ Binary structures
- Strong static type checking
- Tree shaking friendly design
- Support for little-endian, big-endian, and dynamic endian values.

# Usage

## Fixed Endianness

Endianness can be defined for each individual member.

```ts
import { memberU16, memberU8, Struct } from '@hqtsm/struct';

export class Example extends Struct {
declare public readonly ['constructor']: typeof Example;

declare public alpha: number;

declare public beta: number;

declare public gamma: number;

public static override readonly BYTE_LENGTH: number = ((o) => {
o += memberU16(this, 'alpha', o, true);
o += memberU16(this, 'beta', o, false);
o += memberU8(this, 'gamma', o);
return o;
})(super.BYTE_LENGTH);
}

const data = new Uint8Array(Example.BYTE_LENGTH);

const example = new Example(data.buffer);
example.alpha = 0xABCD;
example.beta = 0xBCDE;
example.gamma = 123;
console.assert(data.join(', ') === '205, 171, 188, 222, 123');
```

## Dynamic Endianness

Using the endian passed into the constructor, or host endianness.

```ts
import { memberU16, Struct } from '@hqtsm/struct';

export class Example extends Struct {
declare public readonly ['constructor']: typeof Example;

declare public alpha: number;

declare public beta: number;

public static override readonly BYTE_LENGTH: number = ((o) => {
o += memberU16(this, 'alpha', o);
o += memberU16(this, 'beta', o);
return o;
})(super.BYTE_LENGTH);
}

const data = new Uint8Array(Example.BYTE_LENGTH);

const exampleLE = new Example(data.buffer, 0, true);
exampleLE.alpha = 0xABCD;
exampleLE.beta = 0xBCDE;
console.assert(data.join(', ') === '205, 171, 222, 188');

const exampleBE = new Example(data.buffer, 0, false);
exampleBE.alpha = 0xABCD;
exampleBE.beta = 0xBCDE;
console.assert(data.join(', ') === '171, 205, 188, 222');
```

0 comments on commit f68fc19

Please sign in to comment.