Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export class Reader implements IReader, IReaderResettable {
return bin;
}

public subarray(start: number = 0, end?: number): Uint8Array {
const x = this.x;
const actualStart = x + start;
const actualEnd = typeof end === 'number' ? x + end : this.end;
return this.uint8.subarray(actualStart, actualEnd);
}

/**
* Creates a new {@link Reader} that references the same underlying memory
* buffer. But with independent cursor and end.
Expand Down
7 changes: 7 additions & 0 deletions src/StreamingReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export class StreamingReader implements IReader, IReaderResettable {
return bin;
}

public subarray(start: number = 0, end?: number): Uint8Array {
const x = this.x;
const actualStart = x + start;
const actualEnd = typeof end === 'number' ? x + end : this.size() + x - start;
return this.uint8.subarray(actualStart, actualEnd);
}

/**
* Creates a new {@link Reader} that references the same underlying memory
* buffer. But with independent cursor and end.
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export interface IReaderBase {
*/
cut(size?: number): IReaderBase;

subarray(start?: number, end?: number): Uint8Array;

/** Get current byte value without advancing the cursor. */
peek(): number;

Expand Down