Shorten the MeshStreamReader.prototype.readBits method a little bit

- Use a `BaseStream`-instance method to directly get the int32 value.

 - Use local variables more.
This commit is contained in:
Jonas Jenwald 2025-02-08 11:17:35 +01:00
parent 72339dc561
commit 36522d85cc

View File

@ -340,24 +340,19 @@ class MeshStreamReader {
} }
readBits(n) { readBits(n) {
let buffer = this.buffer; const { stream } = this;
let bufferLength = this.bufferLength; let { buffer, bufferLength } = this;
if (n === 32) { if (n === 32) {
if (bufferLength === 0) { if (bufferLength === 0) {
return ( return stream.getInt32() >>> 0;
((this.stream.getByte() << 24) |
(this.stream.getByte() << 16) |
(this.stream.getByte() << 8) |
this.stream.getByte()) >>>
0
);
} }
buffer = buffer =
(buffer << 24) | (buffer << 24) |
(this.stream.getByte() << 16) | (stream.getByte() << 16) |
(this.stream.getByte() << 8) | (stream.getByte() << 8) |
this.stream.getByte(); stream.getByte();
const nextByte = this.stream.getByte(); const nextByte = stream.getByte();
this.buffer = nextByte & ((1 << bufferLength) - 1); this.buffer = nextByte & ((1 << bufferLength) - 1);
return ( return (
((buffer << (8 - bufferLength)) | ((buffer << (8 - bufferLength)) |
@ -366,10 +361,10 @@ class MeshStreamReader {
); );
} }
if (n === 8 && bufferLength === 0) { if (n === 8 && bufferLength === 0) {
return this.stream.getByte(); return stream.getByte();
} }
while (bufferLength < n) { while (bufferLength < n) {
buffer = (buffer << 8) | this.stream.getByte(); buffer = (buffer << 8) | stream.getByte();
bufferLength += 8; bufferLength += 8;
} }
bufferLength -= n; bufferLength -= n;