Added toBytes converters for ByteRange

This commit is contained in:
Yuriy Glukhov 2018-03-22 12:02:51 +02:00 committed by zah
parent 069d73df31
commit c1cc0f58a0
1 changed files with 10 additions and 1 deletions

View File

@ -33,7 +33,7 @@ proc `[]`*(r: var BytesRange, i: int): var byte =
proc `[]=`*(r: var BytesRange, i: int, v: byte) =
r.bytes[r.ibegin + i] = v
template len*(r: BytesRange): int =
proc len*(r: BytesRange): int {.inline.} =
r.iend - r.ibegin
proc slice*(r: BytesRange, ibegin: int, iend = -1): BytesRange =
@ -63,6 +63,15 @@ proc rangeBeginAddr*(r: BytesRange): pointer {.inline.} =
proc baseAddr*(r: BytesRange): pointer =
baseAddr(r.bytes).shift(r.ibegin)
proc toBytes*(r: BytesRange, output: var openarray[byte]) =
let sz = r.len
assert(output.len >= sz)
copyMem(addr output[0], unsafeAddr r.bytes[r.ibegin], sz)
proc toBytes*(r: BytesRange): Bytes =
result = newSeq[byte](r.len)
r.toBytes(result)
when false:
import
ptr_arith, keccak_tiny