10
10
11
11
import strutils
12
12
import tables
13
+ import endians
13
14
14
15
type
15
16
StructError* = object of OSError
@@ -171,47 +172,47 @@ proc `$`*( node: StructNode ): string =
171
172
of StructString:
172
173
$ node.str
173
174
174
- proc getChar* (node: StructNode): char =
175
+ proc getChar* (node: StructNode): char {.noSideEffect, procVar.} =
175
176
assert node.kind == StructChar
176
177
result = node.chr
177
178
178
- proc getBool* (node: StructNode): bool =
179
+ proc getBool* (node: StructNode): bool {.noSideEffect, procVar.} =
179
180
assert node.kind == StructBool
180
181
result = node.bval
181
182
182
- proc getShort* (node: StructNode): int16 =
183
+ proc getShort* (node: StructNode): int16 {.noSideEffect, procVar.} =
183
184
assert node.kind == StructShort
184
185
result = node.sval
185
186
186
- proc getUShort* (node: StructNode): uint16 =
187
+ proc getUShort* (node: StructNode): uint16 {.noSideEffect, procVar.} =
187
188
assert node.kind == StructUShort
188
189
result = node.usval
189
190
190
- proc getInt* (node: StructNode): int32 =
191
+ proc getInt* (node: StructNode): int32 {.noSideEffect, procVar.} =
191
192
assert node.kind == StructInt
192
193
result = node.ival
193
194
194
- proc getUInt* (node: StructNode): uint32 =
195
+ proc getUInt* (node: StructNode): uint32 {.noSideEffect, procVar.} =
195
196
assert node.kind == StructUInt
196
197
result = node.uival
197
198
198
- proc getQuad* (node: StructNode): int64 =
199
+ proc getQuad* (node: StructNode): int64 {.noSideEffect, procVar.} =
199
200
assert node.kind == StructQuad
200
201
result = node.qval
201
202
202
- proc getUQuad* (node: StructNode): uint64 =
203
+ proc getUQuad* (node: StructNode): uint64 {.noSideEffect, procVar.} =
203
204
assert node.kind == StructUQuad
204
205
result = node.uqval
205
206
206
- proc getFloat* (node: StructNode): float32 =
207
+ proc getFloat* (node: StructNode): float32 {.noSideEffect, procVar.} =
207
208
assert node.kind == StructFloat
208
209
result = node.fval
209
210
210
- proc getDouble* (node: StructNode): float64 =
211
+ proc getDouble* (node: StructNode): float64 {.noSideEffect, procVar.} =
211
212
assert node.kind == StructDouble
212
213
result = node.dval
213
214
214
- proc getString* (node: StructNode): string =
215
+ proc getString* (node: StructNode): string {.noSideEffect, procVar.} =
215
216
assert node.kind == StructString
216
217
return node.str
217
218
@@ -280,35 +281,26 @@ proc load_64f*(s: string, endian: Endianness): float64 {.inline.} =
280
281
else :
281
282
o[i] = s[8 - i - 1 ]
282
283
283
- proc extract_16* [T:int16 | uint16 ](v: T, endian: Endianness): string {.inline.} =
284
- result = " "
284
+ proc extract_16* [T:int16 | uint16 ](v: T, endian: Endianness): array [0 .. 1 , char ] {.inline.} =
285
285
var v = v
286
- var o = cast [cstring ](addr v)
287
-
288
286
if endian == littleEndian:
289
- result &= $ o[ 0 ] & $ o[ 1 ]
287
+ littleEndian16( addr result , addr v)
290
288
else :
291
- result &= $ o[ 1 ] & $ o[ 0 ]
289
+ bigEndian16( addr result , addr v)
292
290
293
- proc extract_32* [T:float32 | int32 | uint32 ](v: T, endian: Endianness): string {.inline.} =
294
- result = " "
291
+ proc extract_32* [T:float32 | int32 | uint32 ](v: T, endian: Endianness): array [0 .. 3 , char ] {.inline.} =
295
292
var v = v
296
- var o = cast [cstring ](addr v)
297
- for i in 0 .. 3 :
298
- if endian == littleEndian:
299
- result &= $ o[i]
300
- else :
301
- result &= $ o[3 - i]
293
+ if endian == littleEndian:
294
+ littleEndian32(addr result , addr v)
295
+ else :
296
+ bigEndian32(addr result , addr v)
302
297
303
- proc extract_64* [T:float64 | int64 | uint64 ](v: T, endian: Endianness): string {.inline.} =
304
- result = " "
298
+ proc extract_64* [T:float64 | int64 | uint64 ](v: T, endian: Endianness): array [0 .. 7 , char ] {.inline.} =
305
299
var v = v
306
- var o = cast [cstring ](addr v)
307
- for i in 0 .. 7 :
308
- if endian == littleEndian:
309
- result &= $ o[i]
310
- else :
311
- result &= $ o[7 - i]
300
+ if endian == littleEndian:
301
+ littleEndian64(addr result , addr v)
302
+ else :
303
+ bigEndian64(addr result , addr v)
312
304
313
305
proc unpack_char(vars: var seq [StructNode], ctx: StructContext) =
314
306
for i in 0 .. ctx.repeat- 1 :
@@ -463,7 +455,7 @@ proc pack_16(vars: varargs[StructNode], ctx: StructContext): string =
463
455
proc pack_32(vars: varargs [StructNode], ctx: StructContext) : string =
464
456
result = newString(4 * ctx.repeat)
465
457
for i in 0 .. ctx.repeat- 1 :
466
- var value: string
458
+ var value: array [ 0 .. 3 , char ]
467
459
case vars[ctx.offset].kind:
468
460
of StructFloat:
469
461
value = extract_32(vars[ctx.offset].fval, ctx.byteOrder)
@@ -482,7 +474,7 @@ proc pack_32(vars: varargs[StructNode], ctx: StructContext): string =
482
474
proc pack_64(vars: varargs [StructNode], ctx: StructContext) : string =
483
475
result = newString(8 * ctx.repeat)
484
476
for i in 0 .. ctx.repeat- 1 :
485
- var value: string
477
+ var value: array [ 0 .. 7 , char ]
486
478
case vars[ctx.offset].kind:
487
479
of StructDouble:
488
480
value = extract_64(vars[ctx.offset].dval, ctx.byteOrder)
0 commit comments