Skip to content

Commit 1d9faf6

Browse files
committed
Revert part of Nat encode improvement (#87)
1 parent bc80e28 commit 1d9faf6

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

packages/agent_dart_base/lib/agent/utils/leb128.dart

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ List<T> safeRead<T>(BufferPipe<T> pipe, int ref) {
2222
/// nearest integer.
2323
/// @param value The number to encode.
2424
Uint8List lebEncode(dynamic value) {
25-
BigInt bn = switch (value) {
26-
BigInt() => value,
27-
num() => BigInt.from(value),
28-
String() => BigInt.parse(value),
29-
_ => throw ArgumentError('Invalid big number: $value', 'lebEncode'),
30-
};
25+
BigInt bn = value is BigInt ? value : BigInt.from(value);
3126
if (bn < BigInt.zero) {
3227
throw StateError('Cannot leb-encode negative values.');
3328
}
@@ -63,13 +58,9 @@ BigInt lebDecode<T>(BufferPipe<T> pipe) {
6358
/// Encode a number (or bigint) into a Buffer, with support for negative numbers.
6459
/// The number will be floored to the nearest integer.
6560
/// @param value The number to encode.
66-
Uint8List slebEncode(dynamic value) {
67-
BigInt bn = switch (value) {
68-
BigInt() => value,
69-
num() => BigInt.from(value),
70-
String() => BigInt.parse(value),
71-
_ => throw ArgumentError('Invalid big number: $value', 'slebEncode'),
72-
};
61+
Uint8List slebEncode(Comparable value) {
62+
BigInt bn = value is BigInt ? value : BigInt.from(value as num);
63+
7364
final isNeg = bn < BigInt.zero;
7465
if (isNeg) {
7566
bn = -bn - BigInt.one;

packages/agent_dart_base/lib/candid/idl.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,7 @@ class NatClass extends PrimitiveType {
533533

534534
@override
535535
bool covariant(x) {
536-
return (x is BigInt && x >= BigInt.zero) ||
537-
(x is int && x >= 0) ||
538-
(x is String && BigInt.parse(x) >= BigInt.zero);
536+
return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0);
539537
}
540538

541539
@override

packages/agent_dart_base/test/agent/utils/leb128.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void leb128Test() {
2424
lebEncode(BigInt.from(60000000000000000)).toHex(),
2525
'808098f4e9b5ca6a',
2626
);
27-
expect(lebEncode('1').toHex(), '01');
2827

2928
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([0]))), BigInt.zero);
3029
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([1]))), BigInt.one);
@@ -60,7 +59,6 @@ void leb128Test() {
6059
slebEncode(BigInt.parse('60000000000000000')).toHex(),
6160
'808098f4e9b5caea00',
6261
);
63-
expect(slebEncode('1').toHex(), '01');
6462

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

0 commit comments

Comments
 (0)