mirror of https://github.com/status-im/nim-rlp.git
use the new status-im/ranges library
This commit is contained in:
parent
a0f075865c
commit
ce79722d43
|
@ -1,6 +1,7 @@
|
|||
nimcache/
|
||||
|
||||
# ignore all executable files
|
||||
*
|
||||
!*.*
|
||||
!*/
|
||||
|
||||
nimcache/
|
||||
|
||||
|
|
3
rlp.nim
3
rlp.nim
|
@ -315,6 +315,9 @@ template decode*(bytes: openarray[byte], T: typedesc): T =
|
|||
var bytesCopy = @bytes
|
||||
decode(initBytesRange(bytesCopy), T)
|
||||
|
||||
proc append*(writer: var RlpWriter; rlp: Rlp) =
|
||||
append(writer, rlp.rawData)
|
||||
|
||||
proc inspectAux(self: var Rlp, depth: int, output: var string) =
|
||||
if not hasData():
|
||||
return
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
mode = ScriptMode.Verbose
|
||||
|
||||
packageName = "rlp"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
author = "Status Research & Development GmbH"
|
||||
description = "RLP serialization library for Nim"
|
||||
license = "Apache2"
|
||||
skipDirs = @["tests"]
|
||||
|
||||
requires "nim >= 0.17.0"
|
||||
requires "nim >= 0.17.0", "ranges >= 0.0.1"
|
||||
|
||||
proc configForTests() =
|
||||
--hints: off
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
proc baseAddr*[T](x: openarray[T]): pointer = cast[pointer](x)
|
||||
|
||||
proc shift*(p: pointer, delta: int): pointer =
|
||||
cast[pointer](cast[int](p) + delta)
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
import
|
||||
ranges/ptr_arith
|
||||
|
||||
type
|
||||
Bytes* = seq[byte]
|
||||
|
||||
|
@ -45,13 +48,21 @@ iterator items*(r: BytesRange): byte =
|
|||
for i in r.ibegin ..< r.iend:
|
||||
yield r.bytes[i]
|
||||
|
||||
converter fromSeq*(s: Bytes): BytesRange =
|
||||
proc toRange*(s: Bytes): BytesRange =
|
||||
var seqCopy = s
|
||||
return initBytesRange(seqCopy)
|
||||
|
||||
converter fromVarSeq*(s: var Bytes): BytesRange =
|
||||
proc toRange*(s: var Bytes): BytesRange =
|
||||
return initBytesRange(s)
|
||||
|
||||
# XXX: This could be a template once the following issue is fixed:
|
||||
# https://github.com/nim-lang/Nim/issues/7223
|
||||
proc rangeBeginAddr*(r: BytesRange): pointer {.inline.} =
|
||||
baseAddr(r.bytes).shift(r.ibegin)
|
||||
|
||||
proc baseAddr*(r: BytesRange): pointer =
|
||||
baseAddr(r.bytes).shift(r.ibegin)
|
||||
|
||||
when false:
|
||||
import
|
||||
ptr_arith, keccak_tiny
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
import
|
||||
types, ptr_arith, object_serialization, priv/defs, macros
|
||||
macros, types,
|
||||
ranges/[memranges, ptr_arith],
|
||||
object_serialization, priv/defs
|
||||
|
||||
export
|
||||
memranges
|
||||
|
||||
type
|
||||
RlpWriter* = object
|
||||
|
@ -119,14 +124,18 @@ proc startList(self; listSize: int) =
|
|||
pendingLists.add((listSize, output.len))
|
||||
|
||||
template appendImpl(self; data, startMarker) =
|
||||
mixin baseAddr
|
||||
|
||||
if data.len == 1 and byte(data[0]) < BLOB_START_MARKER:
|
||||
output.add byte(data[0])
|
||||
self.output.add byte(data[0])
|
||||
else:
|
||||
output.writeCount(data.len, startMarker)
|
||||
self.output.writeCount(data.len, startMarker)
|
||||
|
||||
let startPos = output.len
|
||||
output.setLen(startPos + data.len)
|
||||
copyMem(output.baseAddr.shift(startPos), data.baseAddr, data.len)
|
||||
self.output.setLen(startPos + data.len)
|
||||
copyMem(shift(baseAddr(self.output), startPos),
|
||||
baseAddr(data),
|
||||
data.len)
|
||||
|
||||
maybeClosePendingLists()
|
||||
|
||||
|
@ -136,6 +145,12 @@ proc append*(self; data: string) =
|
|||
proc append*(self; data: Bytes) =
|
||||
appendImpl(self, data, BLOB_START_MARKER)
|
||||
|
||||
proc append*(self; data: BytesRange) =
|
||||
appendImpl(self, data, BLOB_START_MARKER)
|
||||
|
||||
proc append*(self; data: MemRange) =
|
||||
appendImpl(self, data, BLOB_START_MARKER)
|
||||
|
||||
proc append*(self; i: Integer) =
|
||||
if i == 0:
|
||||
self.output.add BLOB_START_MARKER
|
||||
|
@ -186,12 +201,12 @@ macro encodeList*(args: varargs[untyped]): BytesRange =
|
|||
|
||||
for arg in args:
|
||||
body.add quote do:
|
||||
`writer`.append(`arg`)
|
||||
append(`writer`, `arg`)
|
||||
|
||||
result = quote do:
|
||||
var `writer` = initRlpList(`listLen`)
|
||||
`body`
|
||||
`writer`.finish()
|
||||
finish(`writer`)
|
||||
|
||||
when false:
|
||||
# XXX: Currently fails with a malformed AST error on the args.len expression
|
||||
|
|
|
@ -7,7 +7,7 @@ proc i(s: string): string = s.replace(" ").replace("\n")
|
|||
proc inspectMatch(r: Rlp, s: string): bool = r.inspect.i == s.i
|
||||
|
||||
test "empty bytes are not a proper RLP":
|
||||
var rlp = rlpFromBytes Bytes(@[])
|
||||
var rlp = rlpFromBytes Bytes(@[]).toRange
|
||||
|
||||
check:
|
||||
not rlp.hasData
|
||||
|
|
Loading…
Reference in New Issue