Skip to content

Commit d55c624

Browse files
committed
update endians
1 parent 190d3ba commit d55c624

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

struct.nim

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import strutils
1212
import tables
13+
import endians
1314

1415
type
1516
StructError* = object of OSError
@@ -171,47 +172,47 @@ proc `$`*( node: StructNode ): string =
171172
of StructString:
172173
$node.str
173174

174-
proc getChar*(node: StructNode): char =
175+
proc getChar*(node: StructNode): char {.noSideEffect, procVar.} =
175176
assert node.kind == StructChar
176177
result = node.chr
177178

178-
proc getBool*(node: StructNode): bool =
179+
proc getBool*(node: StructNode): bool {.noSideEffect, procVar.} =
179180
assert node.kind == StructBool
180181
result = node.bval
181182

182-
proc getShort*(node: StructNode): int16 =
183+
proc getShort*(node: StructNode): int16 {.noSideEffect, procVar.} =
183184
assert node.kind == StructShort
184185
result = node.sval
185186

186-
proc getUShort*(node: StructNode): uint16 =
187+
proc getUShort*(node: StructNode): uint16 {.noSideEffect, procVar.} =
187188
assert node.kind == StructUShort
188189
result = node.usval
189190

190-
proc getInt*(node: StructNode): int32 =
191+
proc getInt*(node: StructNode): int32 {.noSideEffect, procVar.} =
191192
assert node.kind == StructInt
192193
result = node.ival
193194

194-
proc getUInt*(node: StructNode): uint32 =
195+
proc getUInt*(node: StructNode): uint32 {.noSideEffect, procVar.} =
195196
assert node.kind == StructUInt
196197
result = node.uival
197198

198-
proc getQuad*(node: StructNode): int64 =
199+
proc getQuad*(node: StructNode): int64 {.noSideEffect, procVar.} =
199200
assert node.kind == StructQuad
200201
result = node.qval
201202

202-
proc getUQuad*(node: StructNode): uint64 =
203+
proc getUQuad*(node: StructNode): uint64 {.noSideEffect, procVar.} =
203204
assert node.kind == StructUQuad
204205
result = node.uqval
205206

206-
proc getFloat*(node: StructNode): float32 =
207+
proc getFloat*(node: StructNode): float32 {.noSideEffect, procVar.} =
207208
assert node.kind == StructFloat
208209
result = node.fval
209210

210-
proc getDouble*(node: StructNode): float64 =
211+
proc getDouble*(node: StructNode): float64 {.noSideEffect, procVar.} =
211212
assert node.kind == StructDouble
212213
result = node.dval
213214

214-
proc getString*(node: StructNode): string =
215+
proc getString*(node: StructNode): string {.noSideEffect, procVar.} =
215216
assert node.kind == StructString
216217
return node.str
217218

@@ -280,35 +281,26 @@ proc load_64f*(s: string, endian: Endianness): float64 {.inline.} =
280281
else:
281282
o[i] = s[8 - i - 1]
282283

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.} =
285285
var v = v
286-
var o = cast[cstring](addr v)
287-
288286
if endian == littleEndian:
289-
result &= $o[0] & $o[1]
287+
littleEndian16(addr result, addr v)
290288
else:
291-
result &= $o[1] & $o[0]
289+
bigEndian16(addr result, addr v)
292290

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.} =
295292
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)
302297

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.} =
305299
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)
312304

313305
proc unpack_char(vars: var seq[StructNode], ctx: StructContext) =
314306
for i in 0..ctx.repeat-1:
@@ -463,7 +455,7 @@ proc pack_16(vars: varargs[StructNode], ctx: StructContext): string =
463455
proc pack_32(vars: varargs[StructNode], ctx: StructContext): string =
464456
result = newString(4*ctx.repeat)
465457
for i in 0..ctx.repeat-1:
466-
var value: string
458+
var value: array[0..3, char]
467459
case vars[ctx.offset].kind:
468460
of StructFloat:
469461
value = extract_32(vars[ctx.offset].fval, ctx.byteOrder)
@@ -482,7 +474,7 @@ proc pack_32(vars: varargs[StructNode], ctx: StructContext): string =
482474
proc pack_64(vars: varargs[StructNode], ctx: StructContext): string =
483475
result = newString(8*ctx.repeat)
484476
for i in 0..ctx.repeat-1:
485-
var value: string
477+
var value: array[0..7, char]
486478
case vars[ctx.offset].kind:
487479
of StructDouble:
488480
value = extract_64(vars[ctx.offset].dval, ctx.byteOrder)

struct.nimble

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[Package]
22
name = "struct"
3-
version = "0.0.2"
4-
author = "Huy "
3+
version = "0.0.3"
4+
author = "Huy Doan"
55
description = "Python-like 'struct' for Nim"
66
license = "MIT"
77

0 commit comments

Comments
 (0)