Skip to content

Commit b00809c

Browse files
committed
fix wrong offset for pack_32 and pack_64 with repeat, closes #7
1 parent 549d9d8 commit b00809c

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/struct.nim

+16-19
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ proc getString*(node: StructNode): string {.noSideEffect, inline.} =
121121
proc computeLength*(format: string): int =
122122
## Compute the length for string represent `format`
123123
var repeat = newString(0)
124-
for i in 0..format.len-1:
125-
let f: char = format[i]
124+
for i in 0..<format.len:
125+
let f = format[i]
126126
if f in '0'..'9':
127127
repeat.add($f)
128128
else:
@@ -169,7 +169,7 @@ proc load_32f*[T: SomeByte](a, b, c, d: T, endian: Endianness): float32 {.inline
169169
o[0] = d
170170

171171
proc load_64*(s: string, endian: Endianness): int64 {.inline.} =
172-
for i in 0..sizeof(int64)-1:
172+
for i in 0..<sizeof(int64):
173173
result = result shl 8
174174
if endian == littleEndian:
175175
result = result or s[8 - i - 1].int64
@@ -178,7 +178,7 @@ proc load_64*(s: string, endian: Endianness): int64 {.inline.} =
178178

179179
proc load_64f*(s: string, endian: Endianness): float64 {.inline.} =
180180
var o = cast[cstring](addr result)
181-
for i in 0..sizeof(float64)-1:
181+
for i in 0..<sizeof(float64):
182182
if endian == littleEndian:
183183
o[i] = s[i]
184184
else:
@@ -276,10 +276,10 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
276276
context.buffer = buf
277277

278278
var repeat = newString(0)
279-
for i in 0..fmt.len-1:
279+
for i in 0..<fmt.len:
280280
let f: char = fmt[i]
281281
if f in '0'..'9':
282-
repeat.add($f)
282+
repeat.add(f)
283283
continue
284284
else:
285285
if repeat == "":
@@ -352,7 +352,7 @@ proc pack_16(result: var string, vars: openarray[StructNode], ctx: var StructCon
352352

353353
proc pack_32(result: var string, vars: openarray[StructNode], ctx: var StructContext, signed: bool) =
354354
for i in 0..<ctx.repeat:
355-
var value: array[0..3, char]
355+
var value: array[4, char]
356356
case vars[ctx.offset].kind:
357357
of StructFloat:
358358
value = extract_32(vars[ctx.offset].fval.float32, ctx.byteOrder)
@@ -363,16 +363,14 @@ proc pack_32(result: var string, vars: openarray[StructNode], ctx: var StructCon
363363
value = extract_32(vars[ctx.offset].num.uint32, ctx.byteOrder)
364364
else:
365365
raise newException(ValueError, "not supported")
366-
367366
for j in 0..3:
368-
result[ctx.index + i + j] = value[j]
369-
367+
result[ctx.index + i * 4 + j] = value[j]
370368
inc(ctx.offset)
371369
inc(ctx.index, 4 * ctx.repeat)
372370

373371
proc pack_64(result: var string, vars: openarray[StructNode], ctx: var StructContext, signed: bool) =
374372
for i in 0..<ctx.repeat:
375-
var value: array[0..7, char]
373+
var value: array[8, char]
376374
case vars[ctx.offset].kind:
377375
of StructFloat:
378376
value = extract_64(vars[ctx.offset].fval, ctx.byteOrder)
@@ -385,7 +383,7 @@ proc pack_64(result: var string, vars: openarray[StructNode], ctx: var StructCon
385383
raise newException(ValueError, "not supported")
386384

387385
for j in 0..7:
388-
result[ctx.index + i + j] = value[j]
386+
result[ctx.index + i * 8 + j] = value[j]
389387

390388
inc(ctx.offset)
391389
inc(ctx.index, 8 * ctx.repeat)
@@ -394,10 +392,10 @@ proc pack_string(result: var string, vars: openarray[StructNode], ctx: var Struc
394392
assert vars[ctx.offset].kind == StructString
395393

396394
let value = vars[ctx.offset].str
397-
for i in 0..value.len-1:
395+
for i in 0..<value.len:
398396
result[ctx.index + i] = value[i]
399397
if(value.len < ctx.repeat):
400-
for i in value.len..ctx.repeat-1:
398+
for i in value.len..<ctx.repeat:
401399
result[ctx.index + i] = '\x00'
402400

403401
inc(ctx.offset)
@@ -412,11 +410,10 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
412410
result = newString(computeLength(fmt))
413411
var context = newStructContext()
414412
var repeat = newString(0)
415-
for i in 0..fmt.len-1:
416-
let f: char = fmt[i]
417-
413+
for i in 0..<fmt.len:
414+
let f = fmt[i]
418415
if f in '0'..'9':
419-
repeat.add($f)
416+
repeat.add(f)
420417
continue
421418
else:
422419
if repeat == "":
@@ -474,7 +471,7 @@ macro pack_m(n: openarray[typed]): untyped =
474471
result = newNimNode(nnkStmtList, n)
475472
result.add(newCall("initStruct", ident("s"), n[0]))
476473
if n.len > 1:
477-
for i in 1..n.len-1:
474+
for i in 1..<n.len:
478475
result.add(newCall(ident("add"), ident("s"), n[i]))
479476

480477
template `pack`*(n: varargs[typed]): untyped =

struct.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Package]
22
name = "struct"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
author = "Huy Doan"
55
description = "Python-like 'struct' for Nim"
66
license = "MIT"

tests/test_issue7.nim

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import struct
2+
3+
let val = unpack(">3I", "Hello World ")
4+
5+
assert $val == "@[1214606444, 1864390511, 1919706144]"
6+
7+
assert pack(">3I", val) == "Hello World "
8+
assert pack(">3I", val[0], val[1], val[2]) == "Hello World "

0 commit comments

Comments
 (0)