Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/line_offset_list.c
Line
Count
Source
1
#include "prism/internal/line_offset_list.h"
2
3
#include "prism/compiler/align.h"
4
#include "prism/internal/arena.h"
5
6
#include <string.h>
7
8
/**
9
 * Initialize a new line offset list with the given capacity.
10
 */
11
void
12
15.1k
pm_line_offset_list_init(pm_arena_t *arena, pm_line_offset_list_t *list, size_t capacity) {
13
15.1k
    list->offsets = (uint32_t *) pm_arena_alloc(arena, capacity * sizeof(uint32_t), PRISM_ALIGNOF(uint32_t));
14
15
    // The first line always has offset 0.
16
15.1k
    list->offsets[0] = 0;
17
15.1k
    list->size = 1;
18
15.1k
    list->capacity = capacity;
19
15.1k
}
20
21
/**
22
 * Clear out the newlines that have been appended to the list.
23
 */
24
void
25
1.64k
pm_line_offset_list_clear(pm_line_offset_list_t *list) {
26
1.64k
    list->size = 1;
27
1.64k
}
28
29
/**
30
 * Append a new offset to the newline list (slow path: resize and store).
31
 */
32
void
33
27.0k
pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
34
27.0k
    size_t new_capacity = (list->capacity * 3) / 2;
35
27.0k
    uint32_t *new_offsets = (uint32_t *) pm_arena_alloc(arena, new_capacity * sizeof(uint32_t), PRISM_ALIGNOF(uint32_t));
36
37
27.0k
    memcpy(new_offsets, list->offsets, list->size * sizeof(uint32_t));
38
39
27.0k
    list->offsets = new_offsets;
40
27.0k
    list->capacity = new_capacity;
41
42
27.0k
    list->offsets[list->size++] = cursor;
43
27.0k
}
44
45
/**
46
 * Returns the line of the given offset. If the offset is not in the list, the
47
 * line of the closest offset less than the given offset is returned.
48
 */
49
int32_t
50
520
pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) {
51
520
    size_t left = 0;
52
520
    size_t right = list->size - 1;
53
54
2.41k
    while (left <= right) {
  Branch (54:12): [True: 1.98k, False: 431]
55
1.98k
        size_t mid = left + (right - left) / 2;
56
57
1.98k
        if (list->offsets[mid] == cursor) {
  Branch (57:13): [True: 89, False: 1.89k]
58
89
            return ((int32_t) mid) + start_line;
59
89
        }
60
61
1.89k
        if (list->offsets[mid] < cursor) {
  Branch (61:13): [True: 1.07k, False: 820]
62
1.07k
            left = mid + 1;
63
1.07k
        } else {
64
820
            right = mid - 1;
65
820
        }
66
1.89k
    }
67
68
431
    return ((int32_t) left) + start_line - 1;
69
520
}
70
71
/**
72
 * Returns the line and column of the given offset. If the offset is not in the
73
 * list, the line and column of the closest offset less than the given offset
74
 * are returned.
75
 */
76
pm_line_column_t
77
4.09M
pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) {
78
4.09M
    size_t left = 0;
79
4.09M
    size_t right = list->size - 1;
80
81
22.4M
    while (left <= right) {
  Branch (81:12): [True: 18.9M, False: 3.50M]
82
18.9M
        size_t mid = left + (right - left) / 2;
83
84
18.9M
        if (list->offsets[mid] == cursor) {
  Branch (84:13): [True: 588k, False: 18.3M]
85
588k
            return ((pm_line_column_t) { ((int32_t) mid) + start_line, 0 });
86
588k
        }
87
88
18.3M
        if (list->offsets[mid] < cursor) {
  Branch (88:13): [True: 10.3M, False: 8.04M]
89
10.3M
            left = mid + 1;
90
10.3M
        } else {
91
8.04M
            right = mid - 1;
92
8.04M
        }
93
18.3M
    }
94
95
3.50M
    return ((pm_line_column_t) {
96
3.50M
        .line = ((int32_t) left) + start_line - 1,
97
3.50M
        .column = cursor - list->offsets[left - 1]
98
3.50M
    });
99
4.09M
}