Skip to content

Instantly share code, notes, and snippets.

View VVX7's full-sized avatar

Octavia Hexe VVX7

View GitHub Profile
@VVX7
VVX7 / ptrmath.nim
Created November 12, 2021 01:56 — forked from oltolm/ptrmath.nim
Nim pointer arithmetic
# from https://forum.nim-lang.org/t/1188#7366 by Jehan
# most of the time only the dereference operator is needed, import it like this:
# from ptrmath import `[]`
template `+`*[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) +% off * sizeof(p[]))
template `+=`*[T](p: ptr T, off: int) =
p = p + off