Skip to content

Commit

Permalink
updates for Nim 0.20.2, fix unpack -1 as reported in #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Jul 23, 2019
1 parent ec1dd83 commit 176e72e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
19 changes: 10 additions & 9 deletions struct.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import endians
import macros

type
SomeByte* = byte|char|int8|uint8

StructError* = object of OSError

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

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

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

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

proc getUShort*(node: StructNode): uint16 {.noSideEffect, inline.} =
node.num.uint16
node.num.uint.uint16

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

proc getUInt*(node: StructNode): uint32 {.noSideEffect, inline.} =
node.num.uint32
node.num.uint.uint32

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

proc getUQuad*(node: StructNode): uint64 {.noSideEffect, inline.} =
node.num.uint64
node.num.uint.uint64

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

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

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

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



proc unpack*(fmt, buf: string): seq[StructNode] =
result = @[]

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

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

proc add*(s: var Struct, d: float) {.inline.} =
Expand Down
6 changes: 3 additions & 3 deletions struct.nimble
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Package]
name = "struct"
version = "0.1.2"
version = "0.2.0"
author = "Huy Doan"
description = "Python-like 'struct' for Nim"
license = "MIT"

skipFiles = "test.nim test2.nim"
skipFiles = "test.nim"

[Deps]
Requires: "nim >= 0.11.2"
Requires: "nim >= 0.20.2"
10 changes: 8 additions & 2 deletions test.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>> from struct import *
import struct
import struct, strutils

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


# >>> pack('hhi', 1, 2, 3)
Expand All @@ -49,3 +49,9 @@ var result = unpack("hhi", output);
echo result[0].getShort
echo result[1].getShort
echo result[2].getInt

assert struct.unpack(">H", parseHexStr("FFFF"))[0].getShort == int16(-1)
assert struct.unpack(">H", parseHexStr("FFFF"))[0].getUShort == 65535

assert struct.unpack(">I", parseHexStr("FFFFFFFF"))[0].getInt == int32(-1)
assert struct.unpack(">I", parseHexStr("FFFFFFFF"))[0].getUInt == uint32(4294967295)

0 comments on commit 176e72e

Please sign in to comment.