Skip to content

Commit b584bdb

Browse files
committed
remove tables module dependence
1 parent 764e7b0 commit b584bdb

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

struct.nim

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
## This module implements Python struct for Nim
1010

1111
import strutils
12-
import tables
1312
import endians
1413
import macros
1514

@@ -51,20 +50,13 @@ type
5150
const
5251
VERSION* = "0.1.0"
5352

54-
TYPE_LENGTHS = {
55-
'x': sizeof(char),
56-
'b': sizeof(char),
57-
'h': sizeof(int16),
58-
'H': sizeof(uint16),
59-
'i': sizeof(int32),
60-
'I': sizeof(uint32),
61-
'q': sizeof(int64),
62-
'Q': sizeof(uint64),
63-
'f': sizeof(float32),
64-
'd': sizeof(float64),
65-
's': sizeof(char),
66-
'?': sizeof(bool)
67-
}.toTable
53+
proc getSize(t: char): int {.noSideEffect, inline.} =
54+
case t
55+
of 'x', 'b', 's', '?': 1
56+
of 'h', 'H': 2
57+
of 'i', 'I', 'f': 4
58+
of 'q', 'Q', 'd': 8
59+
else: 0
6860

6961
proc newStructChar*(c: char): StructNode =
7062
result.kind = StructChar
@@ -158,9 +150,9 @@ proc calcsize(format: string): int =
158150
repeat.add($f)
159151
else:
160152
if repeat == "":
161-
inc(result, TYPE_LENGTHS[f])
153+
inc(result, getSize(f))
162154
else:
163-
inc(result, parseInt(repeat) * TYPE_LENGTHS[f])
155+
inc(result, parseInt(repeat) * getSize(f))
164156
repeat = ""
165157
166158
proc parse_prefix(ctx: var StructContext, f: char) =
@@ -250,31 +242,31 @@ proc unpack_short(vars: var seq[StructNode], ctx: var StructContext, f: char, si
250242
for i in 0..ctx.repeat-1:
251243
var value = load_16(ctx.buffer[ctx.offset], ctx.buffer[ctx.offset+1], ctx.byteOrder)
252244
vars.add(newStructInt(value))
253-
inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f])
245+
inc(ctx.offset, ctx.repeat * getSize(f))
254246

255247
proc unpack_int(vars: var seq[StructNode], ctx: var StructContext, f: char, signed: bool = false) =
256248
for i in 0..ctx.repeat-1:
257249
var value = load_32(ctx.buffer[ctx.offset], ctx.buffer[ctx.offset+1], ctx.buffer[ctx.offset+2], ctx.buffer[ctx.offset+3], ctx.byteOrder)
258250
vars.add(newStructInt(value))
259-
inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f])
251+
inc(ctx.offset, ctx.repeat * getSize(f))
260252

261253
proc unpack_quad(vars: var seq[StructNode], ctx: var StructContext, f: char, signed: bool = false) =
262254
for i in 0..ctx.repeat-1:
263255
var value = load_64(ctx.buffer[ctx.offset..ctx.offset+7], ctx.byteOrder)
264256
vars.add(newStructInt(value))
265-
inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f])
257+
inc(ctx.offset, ctx.repeat * getSize(f))
266258

267259
proc unpack_float(vars: var seq[StructNode], ctx: var StructContext) =
268260
for i in 0..ctx.repeat-1:
269261
var value = load_32f(ctx.buffer[ctx.offset], ctx.buffer[ctx.offset+1], ctx.buffer[ctx.offset+2], ctx.buffer[ctx.offset+3], ctx.byteOrder)
270262
vars.add(newStructFloat(value.float32))
271-
inc(ctx.offset, ctx.repeat * TYPE_LENGTHS['f'])
263+
inc(ctx.offset, ctx.repeat * getSize('f'))
272264

273265
proc unpack_double(vars: var seq[StructNode], ctx: var StructContext) =
274266
for i in 0..ctx.repeat-1:
275267
var value = load_64f(ctx.buffer[ctx.offset..ctx.offset+7], ctx.byteOrder)
276268
vars.add(newStructFloat(value))
277-
inc(ctx.offset, ctx.repeat * TYPE_LENGTHS['d'])
269+
inc(ctx.offset, ctx.repeat * getSize('d'))
278270

279271
proc unpack_string(vars: var seq[StructNode], ctx: var StructContext) =
280272
var value: string
@@ -337,7 +329,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
337329
of 's':
338330
unpack_string(result, context)
339331
of 'x':
340-
context.offset += context.repeat * TYPE_LENGTHS[f]
332+
context.offset += context.repeat * getSize(f)
341333
else:
342334
raise newException(ValueError, "bad char in struct format")
343335

0 commit comments

Comments
 (0)