4848
4949
5050const
51- VERSION * = " 0.1.0 "
51+ VERSION * = " 0.1.1 "
5252
5353proc getSize (t: char ): int {.noSideEffect , inline .} =
5454 case t
@@ -141,7 +141,7 @@ proc calcsize(format: string): int =
141141 inc (result , getSize (f))
142142 else :
143143 inc (result , parseInt (repeat) * getSize (f))
144- repeat = " "
144+ repeat = newString ( 0 )
145145
146146proc parse_prefix (ctx: var StructContext , f: char ) =
147147 case f
@@ -277,7 +277,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
277277 var context = newStructContext ()
278278 context.buffer = buf
279279
280- var repeat = " "
280+ var repeat = newString ( 0 )
281281 for i in 0 .. fmt.len- 1 :
282282 let f: char = fmt[i]
283283
@@ -289,7 +289,7 @@ proc unpack*(fmt, buf: string): seq[StructNode] =
289289 context.repeat = 1
290290 else :
291291 context.repeat = parseInt (repeat)
292- repeat = " "
292+ repeat = newString ( 0 )
293293
294294 case f
295295 of '=' , '<' , '>' , '!' , '@' :
@@ -417,7 +417,7 @@ proc pack_pad(result: var string, ctx: var StructContext) =
417417proc pack * (fmt: string , vars: varargs [StructNode ]): string =
418418 result = newString (calcsize (fmt))
419419 var context = newStructContext ()
420- var repeat = " "
420+ var repeat = newString ( 0 )
421421 for i in 0 .. fmt.len- 1 :
422422 let f: char = fmt[i]
423423
@@ -429,7 +429,7 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
429429 context.repeat = 1
430430 else :
431431 context.repeat = parseInt (repeat)
432- repeat = " "
432+ repeat = newString ( 0 )
433433
434434 case f
435435 of '=' , '<' , '>' , '!' , '@' :
@@ -444,15 +444,11 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
444444 pack_16 (result , vars, context, false )
445445 of 'i' :
446446 pack_32 (result , vars, context, true )
447- of 'I' :
448- pack_32 (result , vars, context, false )
449- of 'f' :
447+ of 'I' , 'f' :
450448 pack_32 (result , vars, context, false )
451449 of 'q' :
452450 pack_64 (result , vars, context, true )
453- of 'Q' :
454- pack_64 (result , vars, context, false )
455- of 'd' :
451+ of 'Q' , 'd' :
456452 pack_64 (result , vars, context, false )
457453 of 's' :
458454 pack_string (result , vars, context)
@@ -461,39 +457,37 @@ proc pack*(fmt: string, vars: varargs[StructNode]): string =
461457 else :
462458 raise newException (ValueError , " bad char in struct format" )
463459
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 = @ []
467463
468- proc add * (s: var Struct , c: char ) =
464+ proc add * (s: var Struct , c: char ) {. inline .} =
469465 s.vars.add (newStructChar (c))
470466
471- proc add * (s: var Struct , b: bool ) =
467+ proc add * (s: var Struct , b: bool ) {. inline .} =
472468 s.vars.add (newStructBool (b))
473469
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 .} =
475471 s.vars.add (newStructInt (d))
476472
477- proc add * (s: var Struct , d: float ) =
473+ proc add * (s: var Struct , d: float ) {. inline .} =
478474 s.vars.add (newStructFloat (d))
479475
480- proc add * (s: var Struct , str: string ) =
476+ proc add * (s: var Struct , str: string ) {. inline .} =
481477 s.vars.add (newStructString (str))
482478
483- proc pack * (s: Struct ): string =
484- result = pack (s.fmt, s.vars)
485-
486479macro pack_m (n: openarray [expr]): stmt =
487480 result = newNimNode (nnkStmtList, n)
488- result .add (newVarStmt ( ident ( " s " ), newCall ( " newStruct " , n[0 ]) ))
481+ result .add (newCall ( " initStruct " , ident ( " s " ) , n[0 ]))
489482 if n.len > 1 :
490483 for i in 1 .. n.len- 1 :
491484 result .add (newCall (ident (" add" ), ident (" s" ), n[i]))
492485
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)
497491
498492
499493when isMainModule :
0 commit comments