Convenience varints API going from int straight to openarray
This commit is contained in:
parent
a81d1fac85
commit
5f1dc751ca
|
@ -58,6 +58,17 @@ type
|
||||||
when defined(debug):
|
when defined(debug):
|
||||||
state: VarintState
|
state: VarintState
|
||||||
|
|
||||||
|
VarintBuffer* = object
|
||||||
|
bytes*: array[10, byte]
|
||||||
|
totalBytesWritten*: int
|
||||||
|
|
||||||
|
func append*(buf: var VarintBuffer, b: byte) =
|
||||||
|
buf.bytes[buf.totalBytesWritten] = b
|
||||||
|
inc buf.totalBytesWritten
|
||||||
|
|
||||||
|
template writtenBytes*(buf: VarintBuffer): auto =
|
||||||
|
buf.bytes.toOpenArray(0, buf.totalBytesWritten - 1)
|
||||||
|
|
||||||
func maxBits(T: type VarintParser): uint8 {.compileTime.} =
|
func maxBits(T: type VarintParser): uint8 {.compileTime.} =
|
||||||
when T.flavour == ProtoBuf:
|
when T.flavour == ProtoBuf:
|
||||||
uint8(sizeof(T.IntType) * 8)
|
uint8(sizeof(T.IntType) * 8)
|
||||||
|
@ -199,3 +210,12 @@ func vsizeof*(x: SomeInteger): int {.inline.} =
|
||||||
if x == 0: 1
|
if x == 0: 1
|
||||||
else: (log2trunc(x) + 1 + 7 - 1) div 7
|
else: (log2trunc(x) + 1 + 7 - 1) div 7
|
||||||
|
|
||||||
|
template varintBytes*(x: SomeInteger,
|
||||||
|
flavour: static VarintFlavour = ProtoBuf): untyped =
|
||||||
|
var buf: VarintBuffer
|
||||||
|
buf.appendVarint(x, flavour)
|
||||||
|
# TODO: toOpenArray doesn't work here for some reason, so we must
|
||||||
|
# use the less optimal approach of allocating a sequence copy.
|
||||||
|
# buf.bytes.toOpenArray(0, buf.totalBytesWritten - 1)
|
||||||
|
buf.bytes[0 .. buf.totalBytesWritten - 1]
|
||||||
|
|
||||||
|
|
|
@ -25,21 +25,9 @@ const edgeValues = {
|
||||||
0xFFFF_FFFF_FFFF_FFFF'u64 : "ffffffffffffffffff01"
|
0xFFFF_FFFF_FFFF_FFFF'u64 : "ffffffffffffffffff01"
|
||||||
}
|
}
|
||||||
|
|
||||||
type
|
|
||||||
PseudoStream = object
|
|
||||||
bytes: array[12, byte]
|
|
||||||
bytesWritten: int
|
|
||||||
|
|
||||||
func append(s: var PseudoStream, b: byte) =
|
|
||||||
s.bytes[s.bytesWritten] = b
|
|
||||||
inc s.bytesWritten
|
|
||||||
|
|
||||||
template writtenData(s: PseudoStream): auto =
|
|
||||||
s.bytes.toOpenArray(0, s.bytesWritten - 1)
|
|
||||||
|
|
||||||
suite "varints":
|
suite "varints":
|
||||||
template roundtipTest(val) =
|
template roundtipTest(val) =
|
||||||
var s {.inject.}: PseudoStream
|
var s {.inject.}: VarintBuffer
|
||||||
s.appendVarint val
|
s.appendVarint val
|
||||||
|
|
||||||
var roundtripVal: uint64
|
var roundtripVal: uint64
|
||||||
|
@ -47,7 +35,7 @@ suite "varints":
|
||||||
|
|
||||||
check:
|
check:
|
||||||
val == roundtripVal
|
val == roundtripVal
|
||||||
bytesRead == s.bytesWritten
|
bytesRead == s.totalBytesWritten
|
||||||
bytesRead == vsizeof(val)
|
bytesRead == vsizeof(val)
|
||||||
|
|
||||||
test "[ProtoBuf] Success edge cases test":
|
test "[ProtoBuf] Success edge cases test":
|
||||||
|
@ -55,8 +43,9 @@ suite "varints":
|
||||||
let (val, hex) = pair
|
let (val, hex) = pair
|
||||||
roundtipTest val
|
roundtipTest val
|
||||||
check:
|
check:
|
||||||
s.bytesWritten == hex.len div 2
|
s.totalBytesWritten == hex.len div 2
|
||||||
toHex(s.writtenData) == hex
|
toHex(s.writtenBytes) == hex
|
||||||
|
toHex(val.varintBytes) == hex
|
||||||
|
|
||||||
test "[ProtoBuf] random values":
|
test "[ProtoBuf] random values":
|
||||||
for i in 0..10000:
|
for i in 0..10000:
|
||||||
|
|
Loading…
Reference in New Issue