Skip to content

Commit 6fe042f

Browse files
committed
Fix missing support for writing self-terminated fields
1 parent b1a2dd6 commit 6fe042f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

binaryparse.nim

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -541,18 +541,15 @@ macro createParser*(name: untyped, paramsAndDef: varargs[untyped]): untyped =
541541
"String for field of static length not right size"))
542542
else:
543543
newStmtList()
544+
writer.add(quote do:
545+
var `ii` = 0
546+
while `ii` < (`input`[`field`].len).int:
547+
`writeField`
548+
`writeNull`
549+
inc `ii`
550+
)
544551
if def[1][0].len == 2:
545-
var
546-
readFields = def[1][0][1].copyNimTree
547-
writeFields = def[1][0][1].copyNimTree
548-
writeFields.replace(seenFields, input)
549-
writer.add(quote do:
550-
var `ii` = 0
551-
while `ii` < (`input`[`field`].len).int:
552-
`writeField`
553-
`writeNull`
554-
inc `ii`
555-
)
552+
var readFields = def[1][0][1].copyNimTree
556553
readFields.replace(seenFields)
557554
inner.add(quote do:
558555
`res`[`field`] = newSeq[`kind`](`readFields`)
@@ -720,6 +717,9 @@ when isMainModule:
720717
createParser(test):
721718
*twoInts: fields[]
722719

720+
createParser(terminatedInts):
721+
u32: myInts[]
722+
723723
block parse:
724724
var fs = newFileStream("data.hex", fmRead)
725725
defer: fs.close()
@@ -747,3 +747,11 @@ when isMainModule:
747747
ss2.setPosition(0)
748748
echo ss2.readAll()
749749

750+
var
751+
ss3 = newStringStream()
752+
outData: typeGetter(terminatedInts)
753+
outData.myInts = @[1'u32,2,3,4,5,6]
754+
terminatedInts.put(ss3, outData)
755+
ss3.setPosition(0)
756+
echo terminatedInts.get(ss3)
757+

binaryparse.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.2.1"
3+
version = "0.2.2"
44
author = "Peter Munch-Ellingsen"
55
description = "Binary parser (and writer) in pure Nim. Generates efficient parsing procedures that handle many commonly seen patterns seen in binary files and does sub-byte field reading."
66
license = "MIT"

0 commit comments

Comments
 (0)