Skip to content

Commit

Permalink
oids sticks to 24 length strings; fixes breaking changes (nim-lang#20546
Browse files Browse the repository at this point in the history
)

oids sticks 24 length strings
  • Loading branch information
ringabout authored and capocasa committed Mar 31, 2023
1 parent e824066 commit 0c7f99c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/pure/oids.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ proc hexbyte*(hex: char): int {.inline.} =

proc parseOid*(str: cstring): Oid =
## Parses an OID.
var bytes = cast[cstring](addr(result.time))
var bytes = cast[cstring](cast[pointer](cast[ByteAddress](addr(result.time)) + 4))
var i = 0
while i < 16:
while i < 12:
bytes[i] = chr((hexbyte(str[2 * i]) shl 4) or hexbyte(str[2 * i + 1]))
inc(i)

proc `$`*(oid: Oid): string =
## Converts an OID to a string.
const hex = "0123456789abcdef"

result.setLen 32
result.setLen 24

var o = oid
var bytes = cast[cstring](addr(o))
var bytes = cast[cstring](cast[pointer](cast[ByteAddress](addr(o)) + 4))
var i = 0
while i < 16:
while i < 12:
let b = bytes[i].ord
result[2 * i] = hex[(b and 0xF0) shr 4]
result[2 * i + 1] = hex[b and 0xF]
Expand All @@ -83,9 +83,9 @@ template genOid(result: var Oid, incr: var int, fuzz: int32) =
proc genOid*(): Oid =
## Generates a new OID.
runnableExamples:
doAssert ($genOid()).len == 32
doAssert ($genOid()).len == 24
runnableExamples("-r:off"):
echo $genOid() # for example, "00000000632c452db08c3d19ee9073e5"
echo $genOid() # for example, "5fc7f546ddbbc84800006aaf"
genOid(result, incr, fuzz)

proc generatedTime*(oid: Oid): Time =
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/toids.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import std/assertions

block: # genOid
let x = genOid()
doAssert ($x).len == 32
doAssert ($x).len == 24

block:
let x = genOid()
Expand Down

0 comments on commit 0c7f99c

Please sign in to comment.