Push more utilities and UUID stuff

This commit is contained in:
zontreck 2024-05-06 20:49:48 -07:00
parent dd6ee1bf60
commit 9b7b84153f
7 changed files with 325 additions and 0 deletions

View file

@ -283,4 +283,74 @@ class ByteLayer {
int value = readLong();
return (value >> 1) ^ (-(value & 1));
}
void writeBytes(Iterable<int> bytes) {
for (int byte in bytes) {
writeByte(byte);
}
}
List<int> readBytes(int num) {
List<int> lst = [];
for (int i = 0; i < num; i++) {
lst.add(readByte());
}
return lst;
}
void setBit(int position, int maskToSet) {
if (position < _byteBuffer.length) {
// Set the value now
seek(position);
int current = readByte();
seek(position);
current |= maskToSet;
writeByte(current);
}
}
void clearBit(int position, int maskToClear) {
if (position < _byteBuffer.length) {
// Lets clear the bit
seek(position);
int current = readByte();
current &= maskToClear;
seek(position);
writeByte(current);
}
}
bool checkBit(int position, int mask) {
if (position < _byteBuffer.length) {
seek(position);
int current = readByte();
return (current & mask) == mask;
} else
return false;
}
int getBit(int position) {
if (position < _byteBuffer.length) {
seek(position);
return readByte();
} else
return 0;
}
void seek(int position) {
_position = 0;
_ensureCapacity(position);
_position = position;
}
void waitSetBit(int position, int maskToClear, int maskToSet) {
clearBit(position, maskToClear);
setBit(position, maskToSet);
while (!checkBit(position, maskToSet)) {
clearBit(position, maskToClear);
setBit(position, maskToSet);
}
}
}