Base bytes on bytePairs.

This commit is contained in:
coffeepots 2018-05-15 18:16:55 +01:00 committed by zah
parent b98d0bca9c
commit 8c7290c0fe

View File

@ -2,16 +2,9 @@ from asyncdispatch import Port
proc `$`*(port: Port): string = $int(port)
# REVIEW: you can base one of the iterators on the other to avoid the code duplication
# e.g. implement `bytes` in terms of `bytePairs`.
iterator bytes*[T: SomeUnsignedInt](value: T): byte {.inline.} =
## Traverse the bytes of a value in little endian
let argSize = sizeOf(T)
for bIdx in 0 ..< argSize:
let
shift = bIdx.uint * 8
mask = 0xff'u64 shl shift
yield byte((value and mask) shr shift)
yield value.bytePairs[1]
iterator bytePairs*[T: SomeUnsignedInt](value: T): tuple[key: int, val: byte] {.inline.} =
let argSize = sizeOf(T)