Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ This project was initially forked from [filecoin's CborDecode.sol](https://githu

## Usage

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.
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.

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


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.

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

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.


```solidity
using ReadCbor for bytes;
bytes constant someBytes = hex"84616103616102";
struct Cursor {
bytes b;
uint32 i;
uint256 i;
}
function example() pure {
Cursor memory c = Cursor(someBytes, 0);
uint32 len;
(c.i, len) = c.b.Array(c.i);
uint32 arrayLen;
assert(len == 4);
(c.i, arrayLen) = c.b.Array(c.i);
string[] memory arrayStrs = new string[](len / 2);
uint64[] memory arrayNums = new uint64[](len / 2);
// In this example, we know the array length.
assert(arrayLen == 4);
string[] memory arrayStrs = new string[](2);
uint64[] memory arrayNums = new uint64[](2);
// CBOR arrays may contain items of any type.
for (uint32 arrayIdx = 0; arrayIdx < len; arrayIdx++) {
for (uint32 arrayIdx = 0; arrayIdx < arrayLen; arrayIdx++) {
if (c.b.isString(c.i)) {
(c.i, arrayStrs[arrayIdx / 2]) = c.b.String(c.i);
} else if (c.b.isUInt(c.i)) {
(c.i, arrayNums[arrayIdx / 2]) = c.b.UInt(c.i);
}
}
c.b.requireComplete(c.i);
// Require that the data was fully consumed.
require(c.b.length == c.i);
}
```

Expand Down
Loading