Skip to content

Commit

Permalink
refactor(UMP): Simplify readVarInt method
Browse files Browse the repository at this point in the history
Based on the following from the latest `base.js` from YouTube:
```ts
if (a.Kx(b, 1)) {
    var c = a.getUint8(b);
    c = c < 128 ? 1 : c < 192 ? 2 : c < 224 ? 3 : c < 240 ? 4 : 5
} else
    c = 0;
```
  • Loading branch information
LuanRT committed Nov 2, 2024
1 parent 008ded0 commit 3faff2f
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/core/UMP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,13 @@ export class UMP {
}
}

public readVarInt(offset: number): [ number, number ] {
public readVarInt(offset: number): [number, number] {
let byteLength: number;

// Determine the length of the val
if (this.chunkedDataBuffer.canReadBytes(offset, 1)) {
const firstByte = this.chunkedDataBuffer.getUint8(offset);

// Determine the length of the val
if (firstByte < 128) {
byteLength = 1;
} else if (firstByte < 192) {
byteLength = 2;
} else if (firstByte < 224) {
byteLength = 3;
} else if (firstByte < 240) {
byteLength = 4;
} else {
byteLength = 5;
}
byteLength = firstByte < 128 ? 1 : firstByte < 192 ? 2 : firstByte < 224 ? 3 : firstByte < 240 ? 4 : 5;
} else {
byteLength = 0;
}
Expand Down

0 comments on commit 3faff2f

Please sign in to comment.