Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaushalmodi/ptr_math
Pointer arithmetic in Nim
https://github.com/kaushalmodi/ptr_math
arithmetic c ffi nim pointer
Last synced: 4 months ago
JSON representation
Pointer arithmetic in Nim
- Host: GitHub
- URL: https://github.com/kaushalmodi/ptr_math
- Owner: kaushalmodi
- License: mit
- Created: 2021-05-21T02:01:30.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-01T13:43:21.000Z (about 3 years ago)
- Last Synced: 2023-08-13T01:23:07.957Z (over 1 year ago)
- Topics: arithmetic, c, ffi, nim, pointer
- Language: Nim
- Homepage: https://kaushalmodi.github.io/ptr_math/
- Size: 234 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
#+title: Pointer Arithmetic in Nim
#+author: Kaushal Modi[[https://github.com/kaushalmodi/ptr_math/actions/workflows/test.yml][https://github.com/kaushalmodi/ptr_math/actions/workflows/test.yml/badge.svg]]
[[https://github.com/kaushalmodi/ptr_math/actions/workflows/docs.yml][https://github.com/kaushalmodi/ptr_math/actions/workflows/docs.yml/badge.svg]]This module implements basic pointer arithmetic functionality.
* Documentation
[[https://kaushalmodi.github.io/ptr_math/][https://kaushalmodi.github.io/ptr_math/]]
* Installation
#+begin_example
nimble install ptr_math
#+end_example
* Quick Start
#+begin_src nim
import std/[strformat]
import ptr_mathvar
a: array[0 .. 3, int]
p = addr(a[0]) # p is pointing to a[0]for i, _ in a:
a[i] += i
echo &"before : a = {a}"p += 1 # p is now pointing to a[1]
p[] = 100 # p[] is accessing the contents of a[1]
echo &"after p += 1; p[] = 100 : a = {a}"p[0] = 200 # .. so does p[0]
echo &"after p[0] = 200 : a = {a}"p[1] -= 2 # p[1] is accessing the contents of a[2]
echo &"after p[1] -= 2 : a = {a}"p[2] += 50 # p[2] is accessing the contents of a[3]
echo &"after p[2] += 50 : a = {a}"p += 2 # p is now pointing to a[3]
p[-1] += 77 # p[-1] is accessing the contents of a[2]
echo &"after p += 2; p[-1] += 77 : a = {a}"echo &"a[0] = p[-3] = {p[-3]}"
echo &"a[1] = p[-2] = {p[-2]}"
echo &"a[2] = p[-1] = {p[-1]}"
echo &"a[3] = p[0] = {p[0]}"
#+end_src* Development
** Requirement
This library is tested using Nim /devel/ version. The version was
1.5.1 as of writing this on <2021-06-03 Thu>.
** Prep
Once you clone this repo, cd to the cloned directory and run the below
command *first* (before you execute any of the below "nim" commands):
#+begin_example
nim pullConfig
#+end_example
This will clone my global ~config.nims~ file into a
~/nim_config/~ directory. That will enable running the ~nim
test~ and ~nim docs~ commands.
** Testing
#+begin_example
nim test
#+end_example
** Generate doc
Create the HTML page and associated search index in ~/public/~.
#+begin_example
nim docs
#+end_example* Credit
Most of the code in this library is from [[https://forum.nim-lang.org/t/1188#7366][this code snippet]] authored by
Nim Forum user /Jehan/.