Skip to content

Commit

Permalink
Relieve Bincode bool check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Jan 4, 2025
1 parent 10f6308 commit 7dbb31a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
14 changes: 14 additions & 0 deletions serde-generate/runtime/dart/bcs/bcs_deserializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ class BcsDeserializer extends BinaryDeserializer {
"Error while decoding map: keys are not serialized in the expected order");
}
}

bool deserializeBool() {
final result = input.getUint8(offset);
offset += 1;
if (result == 0) {
return false;
} else if (result == 1) {
return true;
} else {
throw Exception(
'Invalid boolean: expected 0 or 1, but got ${result}',
);
}
}
}
6 changes: 6 additions & 0 deletions serde-generate/runtime/dart/bincode/bincode_deserializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class BincodeDeserializer extends BinaryDeserializer {
void checkThatKeySlicesAreIncreasing(Slice key1, Slice key2) {
// Not required by the format.
}

bool deserializeBool() {
final result = input.getUint8(offset) != 0;
offset += 1;
return result;
}
}
62 changes: 24 additions & 38 deletions serde-generate/runtime/dart/serde/binary_deserializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,74 @@ abstract class BinaryDeserializer {

@protected
final ByteData input;
int _offset = 0;
@protected
int offset = 0;
@protected
int containerDepthBudget;

int get offset {
return _offset;
}

bool deserializeBool() {
final result = input.getUint8(_offset);
_offset += 1;
if (result == 0) {
return false;
} else if (result == 1) {
return true;
} else {
throw Exception(
'Invalid boolean: expected 0 or 1, but got ${result}',
);
}
}
bool deserializeBool();

Unit deserializeUnit() {
return const Unit();
}

int deserializeUint8() {
final result = input.getUint8(_offset);
_offset += 1;
final result = input.getUint8(offset);
offset += 1;
return result;
}

int deserializeUint16() {
final result = input.getUint16(_offset, Endian.little);
_offset += 2;
final result = input.getUint16(offset, Endian.little);
offset += 2;
return result;
}

int deserializeUint32() {
final result = input.getUint32(_offset, Endian.little);
_offset += 4;
final result = input.getUint32(offset, Endian.little);
offset += 4;
return result;
}

Uint64 deserializeUint64() {
final number = _bytesToBigInt(8, signed: false);
_offset += 8;
offset += 8;
return Uint64(number);
}

int deserializeInt8() {
final result = input.getInt8(_offset);
_offset += 1;
final result = input.getInt8(offset);
offset += 1;
return result;
}

int deserializeInt16() {
final result = input.getInt16(_offset, Endian.little);
_offset += 2;
final result = input.getInt16(offset, Endian.little);
offset += 2;
return result;
}

int deserializeInt32() {
final result = input.getInt32(_offset, Endian.little);
_offset += 4;
final result = input.getInt32(offset, Endian.little);
offset += 4;
return result;
}

int deserializeInt64() {
final result = input.getInt64(_offset, Endian.little);
_offset += 8;
final result = input.getInt64(offset, Endian.little);
offset += 8;
return result;
}

double deserializeFloat32() {
final result = input.getFloat32(_offset, Endian.little);
_offset += 4;
final result = input.getFloat32(offset, Endian.little);
offset += 4;
return result;
}

double deserializeFloat64() {
final result = input.getFloat64(_offset, Endian.little);
_offset += 8;
final result = input.getFloat64(offset, Endian.little);
offset += 8;
return result;
}

Expand Down Expand Up @@ -149,7 +135,7 @@ abstract class BinaryDeserializer {
// number += BigInt.from(bytes[byteLength - i - 1]) << (8 * i);

// little endian
number += BigInt.from(input.getUint8(_offset + i)) << (8 * i);
number += BigInt.from(input.getUint8(offset + i)) << (8 * i);
}

if (signed) {
Expand Down

0 comments on commit 7dbb31a

Please sign in to comment.