Skip to content

Commit

Permalink
rename calcsize to computeLength, and expose it. closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Jul 30, 2019
1 parent 176e72e commit ebe0a91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions struct.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ proc getDouble*(node: StructNode): float64 {.noSideEffect, inline.} =
proc getString*(node: StructNode): string {.noSideEffect, inline.} =
node.str

proc calcsize(format: string): int =
proc computeLength*(format: string): int =
## Compute the length for string represent `format`
var repeat = newString(0)
for i in 0..format.len-1:
let f: char = format[i]
Expand Down Expand Up @@ -262,9 +263,9 @@ proc unpack_string(vars: var seq[StructNode], ctx: var StructContext) =
proc unpack*(fmt, buf: string): seq[StructNode] =
result = @[]

let size = calcsize(fmt)
if buf.len < size:
raise newException(ValueError, "unpack requires a string argument of length " & $size & ", input: " & $buf.len)
let length = computeLength(fmt)
if buf.len < length:
raise newException(ValueError, "unpack requires a string argument of length " & $length & ", input: " & $buf.len)

var context = newStructContext()
context.buffer = buf
Expand Down Expand Up @@ -407,7 +408,7 @@ proc pack_pad(result: var string, ctx: var StructContext) =
inc(ctx.index, ctx.repeat)

proc pack*(fmt: string, vars: varargs[StructNode]): string =
result = newString(calcsize(fmt))
result = newString(computeLength(fmt))
var context = newStructContext()
var repeat = newString(0)
for i in 0..fmt.len-1:
Expand Down
2 changes: 1 addition & 1 deletion struct.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Package]
name = "struct"
version = "0.2.0"
version = "0.2.1"
author = "Huy Doan"
description = "Python-like 'struct' for Nim"
license = "MIT"
Expand Down

0 comments on commit ebe0a91

Please sign in to comment.