Skip to content

Commit 176e72e

Browse files
committed
updates for Nim 0.20.2, fix unpack -1 as reported in #4
1 parent ec1dd83 commit 176e72e

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

struct.nim

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import endians
1313
import macros
1414

1515
type
16+
SomeByte* = byte|char|int8|uint8
17+
1618
StructError* = object of OSError
1719

1820
StructKind* = enum ## possible JSON node types
@@ -62,7 +64,7 @@ proc newStructChar*(c: char): StructNode = StructNode(kind: StructChar, ch: c)
6264

6365
proc newStructBool*(b: bool): StructNode = StructNode(kind: StructBool, bval: b)
6466

65-
proc newStructInt*[T: uint|int|int16|uint16|int32|uint32|int64|uint64|BiggestInt](i: T): StructNode =
67+
proc newStructInt*[T: SomeInteger](i: T): StructNode =
6668
result = StructNode(kind: StructInt, num: i.BiggestInt)
6769

6870
proc newStructFloat*(d: BiggestFloat): StructNode = StructNode(kind: StructFloat, fval: d)
@@ -98,19 +100,19 @@ proc getShort*(node: StructNode): int16 {.noSideEffect, inline.} =
98100
node.num.int16
99101

100102
proc getUShort*(node: StructNode): uint16 {.noSideEffect, inline.} =
101-
node.num.uint16
103+
node.num.uint.uint16
102104

103105
proc getInt*(node: StructNode): int32 {.noSideEffect, inline.} =
104106
node.num.int32
105107

106108
proc getUInt*(node: StructNode): uint32 {.noSideEffect, inline.} =
107-
node.num.uint32
109+
node.num.uint.uint32
108110

109111
proc getQuad*(node: StructNode): int64 {.noSideEffect, inline.} =
110112
node.num.int64
111113

112114
proc getUQuad*(node: StructNode): uint64 {.noSideEffect, inline.} =
113-
node.num.uint64
115+
node.num.uint.uint64
114116

115117
proc getFloat*(node: StructNode): float32 {.noSideEffect, inline.} =
116118
node.fval.float32
@@ -145,19 +147,19 @@ proc parse_prefix(ctx: var StructContext, f: char) =
145147
else:
146148
ctx.byteOrder = system.cpuEndian
147149

148-
proc load_16*[T:byte|char|int8|uint8](a, b: T, endian: Endianness): int16 {.inline.} =
150+
proc load_16*[T: SomeByte](a, b: T, endian: Endianness): int16 {.inline.} =
149151
if endian == littleEndian:
150152
a.int16 + b.int16 shl 8
151153
else:
152154
b.int16 + a.int16 shl 8
153155

154-
proc load_32*[T:byte|char|int8|uint8](a, b, c, d: T, endian: Endianness): int32 {.inline.} =
156+
proc load_32*[T: SomeByte](a, b, c, d: T, endian: Endianness): int32 {.inline.} =
155157
if endian == littleEndian:
156158
a.int32 + b.int32 shl 8 + c.int32 shl 16 + d.int32 shl 24
157159
else:
158160
d.int32 + c.int32 shl 8 + b.int32 shl 16 + a.int32 shl 24
159161

160-
proc load_32f*[T:byte|char|int8|uint8](a, b, c, d: T, endian: Endianness): float32 {.inline.} =
162+
proc load_32f*[T: SomeByte](a, b, c, d: T, endian: Endianness): float32 {.inline.} =
161163
var o = cast[cstring](addr result)
162164
if endian == littleEndian:
163165
o[0] = a
@@ -257,7 +259,6 @@ proc unpack_string(vars: var seq[StructNode], ctx: var StructContext) =
257259
inc(ctx.offset, ctx.repeat)
258260

259261

260-
261262
proc unpack*(fmt, buf: string): seq[StructNode] =
262263
result = @[]
263264

@@ -458,7 +459,7 @@ proc add*(s: var Struct, c: char) {.inline.} =
458459
proc add*(s: var Struct, b: bool) {.inline.} =
459460
s.vars.add(newStructBool(b))
460461

461-
proc add*[T: uint|int|int16|uint16|int32|uint32|int64|uint64|BiggestInt](s: var Struct, d: T) {.inline.} =
462+
proc add*[T: SomeNumber|BiggestInt](s: var Struct, d: T) {.inline.} =
462463
s.vars.add(newStructInt(d))
463464

464465
proc add*(s: var Struct, d: float) {.inline.} =

struct.nimble

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

8-
skipFiles = "test.nim test2.nim"
8+
skipFiles = "test.nim"
99

1010
[Deps]
11-
Requires: "nim >= 0.11.2"
11+
Requires: "nim >= 0.20.2"

test.nim

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# >>> from struct import *
2-
import struct
2+
import struct, strutils
33

44
let buf ="\x41\x42\x43\x44\x45\x01\x00\x07\x08\x01\x02\x03\x04\x0D\x00\x00\x00"
55
let result1 = unpack("<5b2?h2i", buf)
@@ -35,7 +35,7 @@ assert out1 == "VietNam"
3535
out1 = pack("5s6s4s", "Ho", "Chi", "Minh")
3636
assert out1 == "Ho\x00\x00\x00Chi\x00\x00\x00Minh"
3737
out1 = pack("6sxxxxx3s", "Viet", "Nam")
38-
assert out1.len== 14
38+
assert out1.len == 14
3939

4040

4141
# >>> pack('hhi', 1, 2, 3)
@@ -49,3 +49,9 @@ var result = unpack("hhi", output);
4949
echo result[0].getShort
5050
echo result[1].getShort
5151
echo result[2].getInt
52+
53+
assert struct.unpack(">H", parseHexStr("FFFF"))[0].getShort == int16(-1)
54+
assert struct.unpack(">H", parseHexStr("FFFF"))[0].getUShort == 65535
55+
56+
assert struct.unpack(">I", parseHexStr("FFFFFFFF"))[0].getInt == int32(-1)
57+
assert struct.unpack(">I", parseHexStr("FFFFFFFF"))[0].getUInt == uint32(4294967295)

0 commit comments

Comments
 (0)