summaryrefslogtreecommitdiff
path: root/src/ptrmath.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/ptrmath.nim')
-rw-r--r--src/ptrmath.nim19
1 files changed, 19 insertions, 0 deletions
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