Push unsigned read methods

This commit is contained in:
zontreck 2024-05-07 14:18:35 -07:00
parent d4cf453005
commit f8918b9fcf
2 changed files with 41 additions and 12 deletions

View file

@ -86,12 +86,24 @@ class ByteLayer {
_position++;
}
void writeUnsignedByte(int value) {
_ensureCapacity(1);
_byteBuffer.buffer.asByteData().setUint8(_position, value);
_position++;
}
int readByte() {
final value = _byteBuffer[_position];
_position++;
return value;
}
int readUnsignedByte() {
final value = _byteBuffer.buffer.asByteData().getUint8(_position);
_position++;
return value;
}
void writeVarInt(int value) {
while ((value & ~0x7F) != 0) {
writeByte((value & 0x7F) | 0x80);
@ -239,6 +251,13 @@ class ByteLayer {
return value;
}
int readUnsignedLong() {
final value =
_byteBuffer.buffer.asByteData().getUint64(_position, Endian.big);
_position += 8;
return value;
}
void writeVarLongNoZigZag(int value) {
while (true) {
if ((value & ~0x7F) == 0) {
@ -286,14 +305,14 @@ class ByteLayer {
void writeBytes(Iterable<int> bytes) {
for (int byte in bytes) {
writeByte(byte);
writeUnsignedByte(byte);
}
}
List<int> readBytes(int num) {
List<int> lst = [];
for (int i = 0; i < num; i++) {
lst.add(readByte());
lst.add(readUnsignedByte());
}
return lst;
@ -303,10 +322,10 @@ class ByteLayer {
if (position < _byteBuffer.length) {
// Set the value now
seek(position);
int current = readByte();
int current = readUnsignedByte();
seek(position);
current |= maskToSet;
writeByte(current);
writeUnsignedByte(current);
}
}
@ -314,17 +333,17 @@ class ByteLayer {
if (position < _byteBuffer.length) {
// Lets clear the bit
seek(position);
int current = readByte();
int current = readUnsignedByte();
current &= maskToClear;
seek(position);
writeByte(current);
writeUnsignedByte(current);
}
}
bool checkBit(int position, int mask) {
if (position < _byteBuffer.length) {
seek(position);
int current = readByte();
int current = readUnsignedByte();
return (current & mask) == mask;
} else
return false;
@ -333,9 +352,10 @@ class ByteLayer {
int getBit(int position) {
if (position < _byteBuffer.length) {
seek(position);
return readByte();
} else
return readUnsignedByte();
} else {
return 0;
}
}
void seek(int position) {

View file

@ -94,11 +94,20 @@ class UUID {
layer.writeBytes(bytes);
layer.unsetSetBit(6, 0x0F, 0x30);
print(
"Existing bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
layer.unsetSetBit(8, 0x3F, 0x80);
print(
"New bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
layer.resetPosition();
return UUID(layer.readLong(), layer.readLong());
var msb = layer.readUnsignedLong();
var lsb = layer.readUnsignedLong();
if (msb < 0) print("Most significant bit is negative!");
if (lsb < 0) print("Least significant bit is negative!");
return UUID(msb, lsb);
}
case 4:
{
@ -115,7 +124,7 @@ class UUID {
layer.resetPosition();
return UUID(layer.readLong(), layer.readLong());
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
}
case 5:
{
@ -145,7 +154,7 @@ class UUID {
layer.resetPosition();
return UUID(layer.readLong(), layer.readLong());
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
}
default:
throw ArgumentError('Unsupported UUID version: $version');