Skip to content

Commit 9e583d0

Browse files
committed
use local variables
1 parent 3d49bee commit 9e583d0

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

.gas-snapshot

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ ComparisonTest:test_decodeString_CBORDecoder() (gas: 3869)
2525
ComparisonTest:test_decodeString_ReadCbor() (gas: 957)
2626
ComparisonTest:test_decodeTrue_CBORDecoder() (gas: 937)
2727
ComparisonTest:test_decodeTrue_ReadCbor() (gas: 483)
28-
HeaderTest:test_header() (gas: 660)
28+
HeaderTest:test_header() (gas: 651)
2929
HeaderTest:test_header32_u16() (gas: 726)
3030
HeaderTest:test_header32_u32() (gas: 700)
3131
MapTest:test_decodeEmptyMap() (gas: 539)
3232
MapTest:test_decodeNestedMap() (gas: 6619)
3333
MapTest:test_decodeSingleKeyMap() (gas: 1764)
3434
MapTest:test_deeplyNestedStructure() (gas: 3560)
3535
NIntTest:test_decodeLongNInt8() (gas: 768)
36-
NIntTest:test_decodeNInt16() (gas: 927)
37-
NIntTest:test_decodeNInt32() (gas: 910)
38-
NIntTest:test_decodeNInt64() (gas: 947)
36+
NIntTest:test_decodeNInt16() (gas: 918)
37+
NIntTest:test_decodeNInt32() (gas: 901)
38+
NIntTest:test_decodeNInt64() (gas: 938)
3939
NIntTest:test_decodeNInt8() (gas: 789)
4040
NIntTest:test_decodeShortNInt8() (gas: 689)
4141
NIntTest:test_decodeSmallNInts() (gas: 1155)
@@ -68,17 +68,17 @@ PeekTest:test_isUInt_false() (gas: 349)
6868
PeekTest:test_isUInt_true() (gas: 370)
6969
PeekTest:test_isUndefined_false() (gas: 375)
7070
PeekTest:test_isUndefined_true() (gas: 303)
71-
ReadBignum_Test:test_Integer() (gas: 12694)
72-
ReadBignum_Test:test_NInt256_max() (gas: 1706)
73-
ReadBignum_Test:test_NInt256_middle() (gas: 2143)
74-
ReadBignum_Test:test_NInt256_multi() (gas: 1199)
75-
ReadBignum_Test:test_NInt256_random(int256) (runs: 256, μ: 5109, ~: 5109)
76-
ReadBignum_Test:test_NInt256_single() (gas: 1200)
77-
ReadBignum_Test:test_UInt256_max() (gas: 1511)
78-
ReadBignum_Test:test_UInt256_middle() (gas: 1955)
79-
ReadBignum_Test:test_UInt256_multi() (gas: 1077)
80-
ReadBignum_Test:test_UInt256_random(uint256) (runs: 256, μ: 1570, ~: 1570)
81-
ReadBignum_Test:test_UInt256_single() (gas: 1373)
71+
ReadBignum_Test:test_Integer() (gas: 12559)
72+
ReadBignum_Test:test_NInt256_max() (gas: 1697)
73+
ReadBignum_Test:test_NInt256_middle() (gas: 2134)
74+
ReadBignum_Test:test_NInt256_multi() (gas: 1190)
75+
ReadBignum_Test:test_NInt256_random(int256) (runs: 256, μ: 5100, ~: 5100)
76+
ReadBignum_Test:test_NInt256_single() (gas: 1191)
77+
ReadBignum_Test:test_UInt256_max() (gas: 1502)
78+
ReadBignum_Test:test_UInt256_middle() (gas: 1946)
79+
ReadBignum_Test:test_UInt256_multi() (gas: 1068)
80+
ReadBignum_Test:test_UInt256_random(uint256) (runs: 256, μ: 1561, ~: 1561)
81+
ReadBignum_Test:test_UInt256_single() (gas: 1364)
8282
ReadCidSha256_Test:test_Cid() (gas: 521)
8383
ReadCidSha256_Test:test_Cid_multicodec_raw() (gas: 449)
8484
ReadCidSha256_Test:test_Cid_random(uint256) (runs: 256, μ: 3520, ~: 3520)
@@ -95,12 +95,12 @@ StringTest:test_decodeLongString() (gas: 801)
9595
StringTest:test_decodeMediumString() (gas: 852)
9696
StringTest:test_decodeShortString() (gas: 778)
9797
StringTest:test_skipString() (gas: 677)
98-
TagTest:test_decodeExpectedTag() (gas: 636)
99-
TagTest:test_decodeTag() (gas: 675)
98+
TagTest:test_decodeExpectedTag() (gas: 627)
99+
TagTest:test_decodeTag() (gas: 666)
100100
UIntTest:test_decodeLongUInt8() (gas: 636)
101101
UIntTest:test_decodeShortUInt8() (gas: 578)
102102
UIntTest:test_decodeSmallInts() (gas: 913)
103-
UIntTest:test_decodeUInt16() (gas: 760)
104-
UIntTest:test_decodeUInt32() (gas: 788)
105-
UIntTest:test_decodeUInt64() (gas: 836)
103+
UIntTest:test_decodeUInt16() (gas: 751)
104+
UIntTest:test_decodeUInt32() (gas: 779)
105+
UIntTest:test_decodeUInt64() (gas: 827)
106106
UIntTest:test_decodeUInt8() (gas: 634)

src/ReadCbor.sol

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ library ReadCbor {
3232
/// @return arg The parsed argument value
3333
function parseArg(bytes memory cbor, uint i, uint8 minor) private pure returns (uint n, uint64 arg) {
3434
if (minor < MinorExtendU8) {
35-
return (i, minor);
35+
(n, arg) = (i, minor);
3636
} else if (minor == MinorExtendU8) {
3737
(n, arg) = u8(cbor, i);
3838
require(arg >= MinorExtendU8, "invalid type argument (single-byte value too low)");
@@ -87,10 +87,10 @@ library ReadCbor {
8787
/// @return major The major type
8888
function header(bytes memory cbor, uint i) internal pure returns (uint n, uint64 arg, uint8 major) {
8989
uint8 h;
90-
(i, h) = u8(cbor, i);
90+
(n, h) = u8(cbor, i);
9191
major = h >> shiftMajor;
9292
uint8 minor = h & maskMinor;
93-
(n, arg) = parseArg(cbor, i, minor);
93+
(n, arg) = parseArg(cbor, n, minor);
9494
}
9595

9696
/// @notice Reads a CBOR header with an expected major type
@@ -102,9 +102,9 @@ library ReadCbor {
102102
/// @dev Reverts if major type doesn't match expected
103103
function header(bytes memory cbor, uint i, uint8 expectMajor) internal pure returns (uint n, uint64 arg) {
104104
uint8 h;
105-
(i, h) = u8(cbor, i);
105+
(n, h) = u8(cbor, i);
106106
require(h >> shiftMajor == expectMajor, "unexpected major type");
107-
(n, arg) = parseArg(cbor, i, h & maskMinor);
107+
(n, arg) = parseArg(cbor, n, h & maskMinor);
108108
}
109109

110110
/// @notice Reads a CBOR header with expected major and minor types
@@ -121,12 +121,12 @@ library ReadCbor {
121121
returns (uint n, uint64 arg)
122122
{
123123
uint8 h;
124-
(i, h) = u8(cbor, i);
124+
(n, h) = u8(cbor, i);
125125
uint8 major = h >> shiftMajor;
126126
require(major == expectMajor, "unexpected major type");
127127
uint8 minor = h & maskMinor;
128128
require(minor == expectMinor, "unexpected minor type");
129-
(n, arg) = parseArg(cbor, i, minor);
129+
(n, arg) = parseArg(cbor, n, minor);
130130
}
131131

132132
/// @notice Optimized header reading for uint8 type arguments of an expected major type
@@ -332,15 +332,15 @@ library ReadCbor {
332332
/// @param cbor The CBOR-encoded bytes
333333
/// @param i The current index
334334
/// @return n The new index
335-
/// @return ret The string value
336-
function String(bytes memory cbor, uint i) internal pure returns (uint n, string memory ret) {
335+
/// @return str The string value
336+
function String(bytes memory cbor, uint i) internal pure returns (uint n, string memory str) {
337337
uint32 len;
338338
(i, len) = header32(cbor, i, MajorText);
339339

340-
ret = new string(len);
340+
str = new string(len);
341341
assembly ("memory-safe") {
342342
let src := add(cbor, add(0x20, i))
343-
let dest := add(ret, 0x20)
343+
let dest := add(str, 0x20)
344344
mcopy(dest, src, len)
345345
n := add(i, len)
346346
}
@@ -411,15 +411,15 @@ library ReadCbor {
411411
/// @param cbor The CBOR-encoded bytes
412412
/// @param i The current index
413413
/// @return n The new index
414-
/// @return b The byte string value
415-
function Bytes(bytes memory cbor, uint i) internal pure returns (uint n, bytes memory b) {
414+
/// @return bts The byte string value
415+
function Bytes(bytes memory cbor, uint i) internal pure returns (uint n, bytes memory bts) {
416416
uint32 len;
417417
(i, len) = header32(cbor, i, MajorBytes);
418418

419-
b = new bytes(len);
419+
bts = new bytes(len);
420420
assembly ("memory-safe") {
421421
let src := add(cbor, add(0x20, i))
422-
let dest := add(b, 0x20)
422+
let dest := add(bts, 0x20)
423423
mcopy(dest, src, len)
424424
n := add(i, len)
425425
}
@@ -430,17 +430,17 @@ library ReadCbor {
430430
/// @param i The current index
431431
/// @param maxLen The maximum allowed byte string length, which must be <= 32
432432
/// @return n The new index
433-
/// @return b The bytes32 value
433+
/// @return bts The bytes32 value
434434
/// @return len The byte string length
435435
/// @dev Reverts if byte string length exceeds maxLen
436-
function Bytes32(bytes memory cbor, uint i, uint8 maxLen) internal pure returns (uint n, bytes32 b, uint8 len) {
436+
function Bytes32(bytes memory cbor, uint i, uint8 maxLen) internal pure returns (uint n, bytes32 bts, uint8 len) {
437437
assert(maxLen <= 32);
438438
(i, len) = header8(cbor, i, MajorBytes);
439439
require(len <= maxLen);
440440

441441
assembly ("memory-safe") {
442-
b := mload(add(cbor, add(0x20, i)))
443-
b := and(b, not(shr(mul(len, 8), not(0))))
442+
bts := mload(add(cbor, add(0x20, i)))
443+
bts := and(bts, not(shr(mul(len, 8), not(0))))
444444
n := add(i, len)
445445
}
446446
}

test/Array.t.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract ArrayTest is Test {
1111
function test_decodeEmptyArray() public pure {
1212
bytes memory cbor = hex"80"; // Empty array in CBOR
1313
uint i;
14-
uint len;
14+
uint32 len;
1515
(i, len) = cbor.Array(i);
1616
assert(len == 0);
1717
}
@@ -20,7 +20,7 @@ contract ArrayTest is Test {
2020
// Array with 23 elements (just below the threshold for extended header)
2121
bytes memory cbor = hex"97010101010101010101010101010101010101010101010101";
2222
uint i;
23-
uint len;
23+
uint32 len;
2424
(i, len) = cbor.Array(i);
2525
assert(len == 23);
2626
}
@@ -59,7 +59,7 @@ contract ArrayTest is Test {
5959
function test_decodeSingleElementArray() public pure {
6060
bytes memory cbor = hex"8118ff"; // [0xff]
6161
uint i;
62-
uint len;
62+
uint32 len;
6363
uint8 value;
6464

6565
(i, len) = cbor.Array(0);
@@ -74,7 +74,7 @@ contract ArrayTest is Test {
7474
bytes memory cbor = hex"830161618102";
7575

7676
uint i;
77-
uint len;
77+
uint32 len;
7878
uint8 value;
7979
string memory strValue;
8080
uint innerLen;

0 commit comments

Comments
 (0)