Skip to content

Commit

Permalink
Endian swapper
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Nov 20, 2024
1 parent ed8d035 commit 3b5a5ca
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions macro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import type { Struct } from './struct.ts';

/**
* Swap endian of struct.
*
* @param struct Struct to swap the endian of.
* @param littleEndian Little endian, big endian, or default to swap.
* @returns New struct of the same type and memory, different endian.
*/
export function endianSwap<T extends Struct>(
struct: T,
littleEndian: boolean | null = null,
): T {
return new struct.constructor(
struct.buffer,
struct.byteOffset,
littleEndian ?? !struct.littleEndian,
) as T;
}

/**
* Get byte offset of member.
*
Expand Down

0 comments on commit 3b5a5ca

Please sign in to comment.