Skip to content

Commit

Permalink
Revert part of Nat encode improvement (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Dec 28, 2024
1 parent bc80e28 commit 1d9faf6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
17 changes: 4 additions & 13 deletions packages/agent_dart_base/lib/agent/utils/leb128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ List<T> safeRead<T>(BufferPipe<T> pipe, int ref) {
/// nearest integer.
/// @param value The number to encode.
Uint8List lebEncode(dynamic value) {
BigInt bn = switch (value) {
BigInt() => value,
num() => BigInt.from(value),
String() => BigInt.parse(value),
_ => throw ArgumentError('Invalid big number: $value', 'lebEncode'),
};
BigInt bn = value is BigInt ? value : BigInt.from(value);
if (bn < BigInt.zero) {
throw StateError('Cannot leb-encode negative values.');
}
Expand Down Expand Up @@ -63,13 +58,9 @@ BigInt lebDecode<T>(BufferPipe<T> pipe) {
/// Encode a number (or bigint) into a Buffer, with support for negative numbers.
/// The number will be floored to the nearest integer.
/// @param value The number to encode.
Uint8List slebEncode(dynamic value) {
BigInt bn = switch (value) {
BigInt() => value,
num() => BigInt.from(value),
String() => BigInt.parse(value),
_ => throw ArgumentError('Invalid big number: $value', 'slebEncode'),
};
Uint8List slebEncode(Comparable value) {
BigInt bn = value is BigInt ? value : BigInt.from(value as num);

final isNeg = bn < BigInt.zero;
if (isNeg) {
bn = -bn - BigInt.one;
Expand Down
4 changes: 1 addition & 3 deletions packages/agent_dart_base/lib/candid/idl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,7 @@ class NatClass extends PrimitiveType {

@override
bool covariant(x) {
return (x is BigInt && x >= BigInt.zero) ||
(x is int && x >= 0) ||
(x is String && BigInt.parse(x) >= BigInt.zero);
return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0);
}

@override
Expand Down
2 changes: 0 additions & 2 deletions packages/agent_dart_base/test/agent/utils/leb128.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void leb128Test() {
lebEncode(BigInt.from(60000000000000000)).toHex(),
'808098f4e9b5ca6a',
);
expect(lebEncode('1').toHex(), '01');

expect(lebDecode(BufferPipe<int>(Uint8List.fromList([0]))), BigInt.zero);
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([1]))), BigInt.one);
Expand Down Expand Up @@ -60,7 +59,6 @@ void leb128Test() {
slebEncode(BigInt.parse('60000000000000000')).toHex(),
'808098f4e9b5caea00',
);
expect(slebEncode('1').toHex(), '01');

expect(
slebDecode(BufferPipe<int>(Uint8List.fromList([0x7f]))),
Expand Down

0 comments on commit 1d9faf6

Please sign in to comment.