handle char arrays as blobs just like byte arrays

This commit is contained in:
Zahary Karadjov 2018-04-01 05:37:50 +03:00
parent 8acf725628
commit a97cb6c32f
2 changed files with 6 additions and 3 deletions

View File

@ -278,7 +278,7 @@ proc read*(rlp: var Rlp, T: typedesc[enum]): T =
proc read*[R, E](rlp: var Rlp, T: type array[R, E]): T = proc read*[R, E](rlp: var Rlp, T: type array[R, E]): T =
mixin read mixin read
when E is byte: when E is (byte or char):
if not rlp.isBlob: if not rlp.isBlob:
raise newException(BadCastError, "The source RLP is not a blob.") raise newException(BadCastError, "The source RLP is not a blob.")
@ -302,7 +302,7 @@ proc read*[R, E](rlp: var Rlp, T: type array[R, E]): T =
proc read*[E](rlp: var Rlp, T: type seq[E]): T = proc read*[E](rlp: var Rlp, T: type seq[E]): T =
mixin read mixin read
when E is byte: when E is (byte or char):
var bytes = rlp.toBytes var bytes = rlp.toBytes
result = newSeq[byte](bytes.len) result = newSeq[byte](bytes.len)
copyMem(addr result[0], bytes.baseAddr, bytes.len) copyMem(addr result[0], bytes.baseAddr, bytes.len)

View File

@ -148,6 +148,9 @@ proc append*(self; data: string) =
proc appendBlob(self; data: openarray[byte]) = proc appendBlob(self; data: openarray[byte]) =
appendImpl(self, data, BLOB_START_MARKER) appendImpl(self, data, BLOB_START_MARKER)
proc appendBlob(self; data: openarray[char]) =
appendImpl(self, data, BLOB_START_MARKER)
proc appendBytesRange(self; data: BytesRange) = proc appendBytesRange(self; data: BytesRange) =
appendImpl(self, data, BLOB_START_MARKER) appendImpl(self, data, BLOB_START_MARKER)
@ -176,7 +179,7 @@ proc append*[T](self; listOrBlob: openarray[T]) =
# TODO: This append proc should be overloaded by `openarray[byte]` after # TODO: This append proc should be overloaded by `openarray[byte]` after
# nim bug #7416 is fixed. # nim bug #7416 is fixed.
when T is byte: when T is (byte or char):
self.appendBlob(listOrBlob) self.appendBlob(listOrBlob)
else: else:
self.startList listOrBlob.len self.startList listOrBlob.len