Skip to content

Commit 83f626c

Browse files
authored
update readme for index size (#4)
1 parent 9b51f5e commit 83f626c

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ This project was initially forked from [filecoin's CborDecode.sol](https://githu
1515

1616
## Usage
1717

18-
Most methods accept parameters `bytes` of CBOR data and `uint32` index, and return an updated index (and one or more values if appropriate). Since the data parameter is always first, you may sugar calls via `using` directive.
18+
Most methods accept parameters `bytes` of CBOR data and `uint256` index, and return an updated index (and one or more values if appropriate). Since the data parameter is always first, you may sugar calls via `using` directive.
1919

2020
CBOR natively supports values up to `uint64`, so the typical values returned are `uint64`. Some methods return other types.
2121

22-
2322
Deserialization methods are a capitalized name of the type like `UInt`, `NInt`, `Text`, `Map`, and so on for every CBOR type. These return a value of the equivalent solidity type when possible.
2423

2524
When specific format constraints exist, some optimized method variants are available, such as `String1` when the next should be a 1-byte string.
@@ -28,38 +27,38 @@ You can peek at the type of the next item with `isBytes` and so on.
2827

2928
The caller is responsible for handling the index and using it to index the appropriate data. No 'cursor' metaphor is provided, but the example below demonstrates how a caller may define and use a cursor for convenience.
3029

31-
3230
```solidity
3331
using ReadCbor for bytes;
3432
3533
bytes constant someBytes = hex"84616103616102";
3634
3735
struct Cursor {
3836
bytes b;
39-
uint32 i;
37+
uint256 i;
4038
}
4139
4240
function example() pure {
4341
Cursor memory c = Cursor(someBytes, 0);
44-
uint32 len;
45-
46-
(c.i, len) = c.b.Array(c.i);
42+
uint32 arrayLen;
4743
48-
assert(len == 4);
44+
(c.i, arrayLen) = c.b.Array(c.i);
4945
50-
string[] memory arrayStrs = new string[](len / 2);
51-
uint64[] memory arrayNums = new uint64[](len / 2);
46+
// In this example, we know the array length.
47+
assert(arrayLen == 4);
48+
string[] memory arrayStrs = new string[](2);
49+
uint64[] memory arrayNums = new uint64[](2);
5250
5351
// CBOR arrays may contain items of any type.
54-
for (uint32 arrayIdx = 0; arrayIdx < len; arrayIdx++) {
52+
for (uint32 arrayIdx = 0; arrayIdx < arrayLen; arrayIdx++) {
5553
if (c.b.isString(c.i)) {
5654
(c.i, arrayStrs[arrayIdx / 2]) = c.b.String(c.i);
5755
} else if (c.b.isUInt(c.i)) {
5856
(c.i, arrayNums[arrayIdx / 2]) = c.b.UInt(c.i);
5957
}
6058
}
6159
62-
c.b.requireComplete(c.i);
60+
// Require that the data was fully consumed.
61+
require(c.b.length == c.i);
6362
}
6463
```
6564

0 commit comments

Comments
 (0)