Explorar o código

Merge pull request #5224 from Kelimion/ptr_sub

ptr_sub prose clarification
Jeroen van Rijn hai 3 meses
pai
achega
f0061a77c0
Modificáronse 1 ficheiros con 25 adicións e 5 borrados
  1. 25 5
      core/mem/mem.odin

+ 25 - 5
core/mem/mem.odin

@@ -315,18 +315,38 @@ check_zero_ptr :: proc(ptr: rawptr, len: int) -> bool {
 Offset a given pointer by a given amount.
 
 This procedure offsets the pointer `ptr` to an object of type `T`, by the amount
-of bytes specified by `offset*size_of(T)`, and returns the pointer `ptr`.
+of bytes specified by `offset * size_of(T)`, and returns the pointer `ptr`.
 
 **Note**: Prefer to use multipointer types, if possible.
 */
 ptr_offset :: intrinsics.ptr_offset
 
 /*
-Offset a given pointer by a given amount backwards.
+Subtract two pointers of the same type, and return the number of `T` between them.
 
-This procedure offsets the pointer `ptr` to an object of type `T`, by the amount
-of bytes specified by `offset*size_of(T)` in the negative direction, and
-returns the pointer `ptr`.
+This procedure subtracts pointer `b` from pointer `a`, both of type `^T`,
+and returns an integer count of the `T` between them.
+
+**Inputs**
+- `a`: A pointer to a type T
+- `b`: A pointer to a type T
+
+**Returns**
+- `b` - `a` in items of T as an `int`.
+
+Example:
+
+	import "core:mem"
+	import "core:fmt"
+
+	ptr_sub_example :: proc() {
+		arr: [2]int
+		fmt.println(mem.ptr_sub(&arr[1], &arr[0]))
+	}
+
+Output:
+
+	1
 */
 ptr_sub :: intrinsics.ptr_sub