From 970d1279c082c8d9e7a6bde32644a32322e2e0d2 Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Wed, 29 Jul 2020 10:30:06 -0700 Subject: =?UTF-8?q?=E5=BD=A1=EF=BE=9F=E2=97=89=CF=89=E2=97=89=20)=E3=81=A4?= =?UTF-8?q?=E3=83=BC=E2=98=86*=20MATHS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TILE --- src/ptrmath.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/ptrmath.nim (limited to 'src/ptrmath.nim') diff --git a/src/ptrmath.nim b/src/ptrmath.nim new file mode 100644 index 0000000..7de2fa6 --- /dev/null +++ b/src/ptrmath.nim @@ -0,0 +1,19 @@ +# from http://forum.nim-lang.org/t/1188#7366 + +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 + +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 + +template `[]`*[T](p: ptr T, off: int): T = + (p + off)[] + +template `[]=`*[T](p: ptr T, off: int, val: T) = + (p + off)[] = val \ No newline at end of file -- cgit v1.2.3