48
48
49
49
50
50
const
51
- VERSION * = " 0.1.0 "
51
+ VERSION * = " 0.1.1 "
52
52
53
53
proc getSize (t: char ): int {.noSideEffect , inline .} =
54
54
case t
@@ -141,7 +141,7 @@ proc calcsize(format: string): int =
141
141
inc (result , getSize (f))
142
142
else :
143
143
inc (result , parseInt (repeat) * getSize (f))
144
- repeat = " "
144
+ repeat = newString ( 0 )
145
145
146
146
proc parse_prefix (ctx: var StructContext , f: char ) =
147
147
case f
@@ -277,7 +277,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
277
277
var context = newStructContext ()
278
278
context.buffer = buf
279
279
280
- var repeat = " "
280
+ var repeat = newString ( 0 )
281
281
for i in 0 .. fmt.len- 1 :
282
282
let f: char = fmt[i]
283
283
@@ -289,7 +289,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
289
289
context.repeat = 1
290
290
else :
291
291
context.repeat = parseInt (repeat)
292
- repeat = " "
292
+ repeat = newString ( 0 )
293
293
294
294
case f
295
295
of '=' , '<' , '>' , '!' , '@' :
@@ -417,7 +417,7 @@ proc pack_pad(result: var string, ctx: var StructContext) =
417
417
proc pack * (fmt: string , vars: varargs [StructNode ]): string =
418
418
result = newString (calcsize (fmt))
419
419
var context = newStructContext ()
420
- var repeat = " "
420
+ var repeat = newString ( 0 )
421
421
for i in 0 .. fmt.len- 1 :
422
422
let f: char = fmt[i]
423
423
@@ -429,7 +429,7 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
429
429
context.repeat = 1
430
430
else :
431
431
context.repeat = parseInt (repeat)
432
- repeat = " "
432
+ repeat = newString ( 0 )
433
433
434
434
case f
435
435
of '=' , '<' , '>' , '!' , '@' :
@@ -444,15 +444,11 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
444
444
pack_16 (result , vars, context, false )
445
445
of 'i' :
446
446
pack_32 (result , vars, context, true )
447
- of 'I' :
448
- pack_32 (result , vars, context, false )
449
- of 'f' :
447
+ of 'I' , 'f' :
450
448
pack_32 (result , vars, context, false )
451
449
of 'q' :
452
450
pack_64 (result , vars, context, true )
453
- of 'Q' :
454
- pack_64 (result , vars, context, false )
455
- of 'd' :
451
+ of 'Q' , 'd' :
456
452
pack_64 (result , vars, context, false )
457
453
of 's' :
458
454
pack_string (result , vars, context)
@@ -461,39 +457,37 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
461
457
else :
462
458
raise newException (ValueError , " bad char in struct format" )
463
459
464
- proc newStruct * ( fmt: string ): Struct =
465
- result .fmt = fmt
466
- result .vars = @ []
460
+ proc initStruct * (s: var Struct , fmt: string ) {. inline .} =
461
+ s .fmt = fmt
462
+ s .vars = @ []
467
463
468
- proc add * (s: var Struct , c: char ) =
464
+ proc add * (s: var Struct , c: char ) {. inline .} =
469
465
s.vars.add (newStructChar (c))
470
466
471
- proc add * (s: var Struct , b: bool ) =
467
+ proc add * (s: var Struct , b: bool ) {. inline .} =
472
468
s.vars.add (newStructBool (b))
473
469
474
- proc add * [T: uint | int | int16 | uint16 | int32 | uint32 | int64 | uint64 | BiggestInt ](s: var Struct , d: T) =
470
+ proc add * [T: uint | int | int16 | uint16 | int32 | uint32 | int64 | uint64 | BiggestInt ](s: var Struct , d: T) {. inline .} =
475
471
s.vars.add (newStructInt (d))
476
472
477
- proc add * (s: var Struct , d: float ) =
473
+ proc add * (s: var Struct , d: float ) {. inline .} =
478
474
s.vars.add (newStructFloat (d))
479
475
480
- proc add * (s: var Struct , str: string ) =
476
+ proc add * (s: var Struct , str: string ) {. inline .} =
481
477
s.vars.add (newStructString (str))
482
478
483
- proc pack * (s: Struct ): string =
484
- result = pack (s.fmt, s.vars)
485
-
486
479
macro pack_m (n: openarray [expr]): stmt =
487
480
result = newNimNode (nnkStmtList, n)
488
- result .add (newVarStmt ( ident ( " s " ), newCall ( " newStruct " , n[0 ]) ))
481
+ result .add (newCall ( " initStruct " , ident ( " s " ) , n[0 ]))
489
482
if n.len > 1 :
490
483
for i in 1 .. n.len- 1 :
491
484
result .add (newCall (ident (" add" ), ident (" s" ), n[i]))
492
485
493
- template pack * (n: varargs [expr]): expr =
494
- block p:
495
- pack_m (n)
496
- pack (s.fmt, s.vars)
486
+ template `pack` * (n: varargs [expr]): expr =
487
+ when not declaredInScope (s):
488
+ var s {.inject .}: Struct
489
+ pack_m (n)
490
+ pack (s.fmt, s.vars)
497
491
498
492
499
493
when isMainModule :
0 commit comments