-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7bb332
commit f0bb6b5
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { type Options, SizedType } from "../mod.ts"; | ||
|
||
type U2Number = 0 | 1 | 2 | 3; | ||
type U4Number = U2Number | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15; | ||
|
||
export class U2 extends SizedType<U2Number> { | ||
constructor() { | ||
super(1, 1); | ||
} | ||
|
||
readPacked(dt: DataView, options: Options = { byteOffset: 0 }): U2Number { | ||
const v = dt.getUint8(options.byteOffset) & 0b11; | ||
super.incrementOffset(options); | ||
return v as U2Number; | ||
} | ||
|
||
writePacked( | ||
value: U2Number, | ||
dt: DataView, | ||
options: Options = { byteOffset: 0 }, | ||
): void { | ||
dt.setUint8(options.byteOffset, value & 0b11); | ||
super.incrementOffset(options); | ||
} | ||
} | ||
|
||
export class U4 extends SizedType<U4Number> { | ||
constructor() { | ||
super(1, 1); | ||
} | ||
|
||
readPacked(dt: DataView, options: Options = { byteOffset: 0 }): U4Number { | ||
const v = dt.getUint8(options.byteOffset) & 0b1111; | ||
super.incrementOffset(options); | ||
return v as U4Number; | ||
} | ||
|
||
writePacked( | ||
value: U4Number, | ||
dt: DataView, | ||
options: Options = { byteOffset: 0 }, | ||
): void { | ||
dt.setUint8(options.byteOffset, value & 0b1111); | ||
super.incrementOffset(options); | ||
} | ||
} | ||
|
||
export const u2 = new U2(); | ||
export const u4 = new U4(); |