|
9 | 9 | ## This module implements Python struct for Nim
|
10 | 10 |
|
11 | 11 | import strutils
|
12 |
| -import tables |
13 | 12 | import endians
|
14 | 13 | import macros
|
15 | 14 |
|
|
51 | 50 | const
|
52 | 51 | VERSION* = "0.1.0"
|
53 | 52 |
|
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 |
68 | 60 |
|
69 | 61 | proc newStructChar*(c: char): StructNode =
|
70 | 62 | result.kind = StructChar
|
@@ -158,9 +150,9 @@ proc calcsize(format: string): int =
|
158 | 150 | repeat.add($f)
|
159 | 151 | else:
|
160 | 152 | if repeat == "":
|
161 |
| - inc(result, TYPE_LENGTHS[f]) |
| 153 | + inc(result, getSize(f)) |
162 | 154 | else:
|
163 |
| - inc(result, parseInt(repeat) * TYPE_LENGTHS[f]) |
| 155 | + inc(result, parseInt(repeat) * getSize(f)) |
164 | 156 | repeat = ""
|
165 | 157 |
|
166 | 158 | 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
|
250 | 242 | for i in 0..ctx.repeat-1:
|
251 | 243 | var value = load_16(ctx.buffer[ctx.offset], ctx.buffer[ctx.offset+1], ctx.byteOrder)
|
252 | 244 | vars.add(newStructInt(value))
|
253 |
| - inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f]) |
| 245 | + inc(ctx.offset, ctx.repeat * getSize(f)) |
254 | 246 |
|
255 | 247 | proc unpack_int(vars: var seq[StructNode], ctx: var StructContext, f: char, signed: bool = false) =
|
256 | 248 | for i in 0..ctx.repeat-1:
|
257 | 249 | 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)
|
258 | 250 | vars.add(newStructInt(value))
|
259 |
| - inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f]) |
| 251 | + inc(ctx.offset, ctx.repeat * getSize(f)) |
260 | 252 |
|
261 | 253 | proc unpack_quad(vars: var seq[StructNode], ctx: var StructContext, f: char, signed: bool = false) =
|
262 | 254 | for i in 0..ctx.repeat-1:
|
263 | 255 | var value = load_64(ctx.buffer[ctx.offset..ctx.offset+7], ctx.byteOrder)
|
264 | 256 | vars.add(newStructInt(value))
|
265 |
| - inc(ctx.offset, ctx.repeat * TYPE_LENGTHS[f]) |
| 257 | + inc(ctx.offset, ctx.repeat * getSize(f)) |
266 | 258 |
|
267 | 259 | proc unpack_float(vars: var seq[StructNode], ctx: var StructContext) =
|
268 | 260 | for i in 0..ctx.repeat-1:
|
269 | 261 | 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)
|
270 | 262 | vars.add(newStructFloat(value.float32))
|
271 |
| - inc(ctx.offset, ctx.repeat * TYPE_LENGTHS['f']) |
| 263 | + inc(ctx.offset, ctx.repeat * getSize('f')) |
272 | 264 |
|
273 | 265 | proc unpack_double(vars: var seq[StructNode], ctx: var StructContext) =
|
274 | 266 | for i in 0..ctx.repeat-1:
|
275 | 267 | var value = load_64f(ctx.buffer[ctx.offset..ctx.offset+7], ctx.byteOrder)
|
276 | 268 | vars.add(newStructFloat(value))
|
277 |
| - inc(ctx.offset, ctx.repeat * TYPE_LENGTHS['d']) |
| 269 | + inc(ctx.offset, ctx.repeat * getSize('d')) |
278 | 270 |
|
279 | 271 | proc unpack_string(vars: var seq[StructNode], ctx: var StructContext) =
|
280 | 272 | var value: string
|
@@ -337,7 +329,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
|
337 | 329 | of 's':
|
338 | 330 | unpack_string(result, context)
|
339 | 331 | of 'x':
|
340 |
| - context.offset += context.repeat * TYPE_LENGTHS[f] |
| 332 | + context.offset += context.repeat * getSize(f) |
341 | 333 | else:
|
342 | 334 | raise newException(ValueError, "bad char in struct format")
|
343 | 335 |
|
|
0 commit comments