use the new status-im/ranges library

This commit is contained in:
Zahary Karadjov 2018-02-16 17:28:19 +02:00
parent a0f075865c
commit ce79722d43
7 changed files with 44 additions and 19 deletions

5
.gitignore vendored
View File

@ -1,6 +1,7 @@
nimcache/
# ignore all executable files # ignore all executable files
* *
!*.* !*.*
!*/ !*/
nimcache/

View File

@ -315,6 +315,9 @@ template decode*(bytes: openarray[byte], T: typedesc): T =
var bytesCopy = @bytes var bytesCopy = @bytes
decode(initBytesRange(bytesCopy), T) 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) = proc inspectAux(self: var Rlp, depth: int, output: var string) =
if not hasData(): if not hasData():
return return

View File

@ -1,13 +1,13 @@
mode = ScriptMode.Verbose mode = ScriptMode.Verbose
packageName = "rlp" packageName = "rlp"
version = "1.0.0" version = "1.0.1"
author = "Status Research & Development GmbH" author = "Status Research & Development GmbH"
description = "RLP serialization library for Nim" description = "RLP serialization library for Nim"
license = "Apache2" license = "Apache2"
skipDirs = @["tests"] skipDirs = @["tests"]
requires "nim >= 0.17.0" requires "nim >= 0.17.0", "ranges >= 0.0.1"
proc configForTests() = proc configForTests() =
--hints: off --hints: off

View File

@ -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)

View File

@ -1,3 +1,6 @@
import
ranges/ptr_arith
type type
Bytes* = seq[byte] Bytes* = seq[byte]
@ -45,13 +48,21 @@ iterator items*(r: BytesRange): byte =
for i in r.ibegin ..< r.iend: for i in r.ibegin ..< r.iend:
yield r.bytes[i] yield r.bytes[i]
converter fromSeq*(s: Bytes): BytesRange = proc toRange*(s: Bytes): BytesRange =
var seqCopy = s var seqCopy = s
return initBytesRange(seqCopy) return initBytesRange(seqCopy)
converter fromVarSeq*(s: var Bytes): BytesRange = proc toRange*(s: var Bytes): BytesRange =
return initBytesRange(s) 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: when false:
import import
ptr_arith, keccak_tiny ptr_arith, keccak_tiny

View File

@ -1,5 +1,10 @@
import import
types, ptr_arith, object_serialization, priv/defs, macros macros, types,
ranges/[memranges, ptr_arith],
object_serialization, priv/defs
export
memranges
type type
RlpWriter* = object RlpWriter* = object
@ -119,14 +124,18 @@ proc startList(self; listSize: int) =
pendingLists.add((listSize, output.len)) pendingLists.add((listSize, output.len))
template appendImpl(self; data, startMarker) = template appendImpl(self; data, startMarker) =
mixin baseAddr
if data.len == 1 and byte(data[0]) < BLOB_START_MARKER: if data.len == 1 and byte(data[0]) < BLOB_START_MARKER:
output.add byte(data[0]) self.output.add byte(data[0])
else: else:
output.writeCount(data.len, startMarker) self.output.writeCount(data.len, startMarker)
let startPos = output.len let startPos = output.len
output.setLen(startPos + data.len) self.output.setLen(startPos + data.len)
copyMem(output.baseAddr.shift(startPos), data.baseAddr, data.len) copyMem(shift(baseAddr(self.output), startPos),
baseAddr(data),
data.len)
maybeClosePendingLists() maybeClosePendingLists()
@ -136,6 +145,12 @@ proc append*(self; data: string) =
proc append*(self; data: Bytes) = proc append*(self; data: Bytes) =
appendImpl(self, data, BLOB_START_MARKER) 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) = proc append*(self; i: Integer) =
if i == 0: if i == 0:
self.output.add BLOB_START_MARKER self.output.add BLOB_START_MARKER
@ -186,12 +201,12 @@ macro encodeList*(args: varargs[untyped]): BytesRange =
for arg in args: for arg in args:
body.add quote do: body.add quote do:
`writer`.append(`arg`) append(`writer`, `arg`)
result = quote do: result = quote do:
var `writer` = initRlpList(`listLen`) var `writer` = initRlpList(`listLen`)
`body` `body`
`writer`.finish() finish(`writer`)
when false: when false:
# XXX: Currently fails with a malformed AST error on the args.len expression # XXX: Currently fails with a malformed AST error on the args.len expression

View File

@ -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 proc inspectMatch(r: Rlp, s: string): bool = r.inspect.i == s.i
test "empty bytes are not a proper RLP": test "empty bytes are not a proper RLP":
var rlp = rlpFromBytes Bytes(@[]) var rlp = rlpFromBytes Bytes(@[]).toRange
check: check:
not rlp.hasData not rlp.hasData