Ruby 4.1.0dev (2026-11-19 revision 1e71e987c206d5d77bf37e883aaa6262747b2458)
line_offset_list.h
1#ifndef PRISM_INTERNAL_LINE_OFFSET_LIST_H
2#define PRISM_INTERNAL_LINE_OFFSET_LIST_H
3
5
6#include "prism/internal/assert.h"
7
8#include "prism/arena.h"
10
11/* Initialize a new line offset list with the given capacity. */
12void pm_line_offset_list_init(pm_arena_t *arena, pm_line_offset_list_t *list, size_t capacity);
13
14/* Clear out the offsets that have been appended to the list. */
15void pm_line_offset_list_clear(pm_line_offset_list_t *list);
16
17/* Append a new offset to the list (slow path with resize). */
18void pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor);
19
20/* Add the zero-based line index to the starting line with defined 32-bit
21 * wraparound. Keep both the addition and conversion free of signed overflow. */
22static PRISM_FORCE_INLINE int32_t
23pm_line_offset_list_line_number(size_t index, int32_t start_line) {
24 uint32_t line = (uint32_t) index + (uint32_t) start_line;
25 if (line <= INT32_MAX) return (int32_t) line;
26
27 return INT32_MIN + (int32_t) (line - ((uint32_t) INT32_MAX + 1));
28}
29
30/* Append a new offset to the list. */
31static PRISM_FORCE_INLINE void
32pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
33 /* Line and column lookups binary search this list, so it must stay strictly
34 * increasing. */
35 PRISM_ASSERT(list->size == 0 || cursor > list->offsets[list->size - 1]);
36
37 if (list->size < list->capacity) {
38 list->offsets[list->size++] = cursor;
39 } else {
40 pm_line_offset_list_append_slow(arena, list, cursor);
41 }
42}
43
44/*
45 * Returns the line of the given offset. If the offset is not in the list, the
46 * line of the closest offset less than the given offset is returned.
47 */
48int32_t pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line);
49
50#endif
A bump allocator for the prism parser.
#define PRISM_FORCE_INLINE
Force a function to be inlined at every call site.
A list of byte offsets of newlines in a string.
A list of offsets of the start of lines in a string.
uint32_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
size_t capacity
The capacity of the list that has been allocated.