aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/lists.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/lists.h')
-rw-r--r--src/stdlib/lists.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/stdlib/lists.h b/src/stdlib/lists.h
index 74314aa6..1386b2fa 100644
--- a/src/stdlib/lists.h
+++ b/src/stdlib/lists.h
@@ -10,6 +10,16 @@
#include "util.h"
// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1
+#define List_get_checked(list_expr, index_expr, item_type, start, end) \
+ ({ \
+ const List_t list = list_expr; \
+ int64_t index = index_expr; \
+ int64_t off = index + (index < 0) * (list.length + 1) - 1; \
+ if (unlikely(off < 0 || off >= list.length)) \
+ fail_source(__SOURCE_FILE__, start, end, "Invalid list index: ", index, " (list has length ", \
+ (int64_t)list.length, ")\n"); \
+ *(item_type *)(list.data + list.stride * off); \
+ })
#define List_get(list_expr, index_expr, item_type, var, optional_expr, none_expr) \
({ \
const List_t list = list_expr; \