mirror of https://github.com/status-im/nim-rlp.git
Merge pull request #7 from yglukhov/fix-append-openarray-byte
openarray[byte] is now treated as blob instead of list
This commit is contained in:
commit
e0fa989449
|
@ -144,7 +144,7 @@ template appendImpl(self; data, startMarker) =
|
|||
proc append*(self; data: string) =
|
||||
appendImpl(self, data, BLOB_START_MARKER)
|
||||
|
||||
proc append*(self; data: Bytes) =
|
||||
proc appendBlob(self; data: openarray[byte]) =
|
||||
appendImpl(self, data, BLOB_START_MARKER)
|
||||
|
||||
proc appendBytesRange(self; data: BytesRange) =
|
||||
|
@ -165,10 +165,15 @@ proc append*(self; i: Integer) =
|
|||
|
||||
self.maybeClosePendingLists()
|
||||
|
||||
proc append*[T](self; list: openarray[T]) =
|
||||
self.startList list.len
|
||||
for i in 0 ..< list.len:
|
||||
self.append list[i]
|
||||
proc append*[T](self; listOrBlob: openarray[T]) =
|
||||
# TODO: This append proc should be overloaded by `openarray[byte]` after
|
||||
# nim bug #7416 is fixed.
|
||||
when T is byte:
|
||||
self.appendBlob(listOrBlob)
|
||||
else:
|
||||
self.startList listOrBlob.len
|
||||
for i in 0 ..< listOrBlob.len:
|
||||
self.append listOrBlob[i]
|
||||
|
||||
proc append*[T](self; list: seq[T]) =
|
||||
self.startList list.len
|
||||
|
|
|
@ -107,3 +107,15 @@ test "malformed/truncated RLP":
|
|||
var rlp = rlpFromHex("b8056d6f6f7365")
|
||||
expect MalformedRlpError:
|
||||
discard rlp.inspect
|
||||
|
||||
test "encode byte arrays":
|
||||
var b1 = [byte(1), 2, 5, 7, 8]
|
||||
var b2 = [byte(6), 8, 12, 123]
|
||||
var b3 = [byte(122), 56, 65, 12]
|
||||
|
||||
let rlp = rlpFromBytes(encode((b1, b2, b3)))
|
||||
check:
|
||||
rlp.listLen == 3
|
||||
rlp.listElem(0).toBytes().toSeq() == @b1
|
||||
rlp.listElem(1).toBytes().toSeq() == @b2
|
||||
rlp.listElem(2).toBytes().toSeq() == @b3
|
||||
|
|
Loading…
Reference in New Issue