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 | 13.6k | pm_line_offset_list_init(pm_arena_t *arena, pm_line_offset_list_t *list, size_t capacity) { |
13 | 13.6k | 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 | 13.6k | list->offsets[0] = 0; |
17 | 13.6k | list->size = 1; |
18 | 13.6k | list->capacity = capacity; |
19 | 13.6k | } |
20 | | |
21 | | /** |
22 | | * Clear out the newlines that have been appended to the list. |
23 | | */ |
24 | | void |
25 | 1.25k | pm_line_offset_list_clear(pm_line_offset_list_t *list) { |
26 | 1.25k | list->size = 1; |
27 | 1.25k | } |
28 | | |
29 | | /** |
30 | | * Append a new offset to the newline list (slow path: resize and store). |
31 | | */ |
32 | | void |
33 | 29.4k | pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) { |
34 | 29.4k | size_t new_capacity = (list->capacity * 3) / 2; |
35 | 29.4k | uint32_t *new_offsets = (uint32_t *) pm_arena_alloc(arena, new_capacity * sizeof(uint32_t), PRISM_ALIGNOF(uint32_t)); |
36 | | |
37 | 29.4k | memcpy(new_offsets, list->offsets, list->size * sizeof(uint32_t)); |
38 | | |
39 | 29.4k | list->offsets = new_offsets; |
40 | 29.4k | list->capacity = new_capacity; |
41 | | |
42 | 29.4k | list->offsets[list->size++] = cursor; |
43 | 29.4k | } |
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 | 523 | pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) { |
51 | 523 | size_t left = 0; |
52 | 523 | size_t right = list->size - 1; |
53 | | |
54 | 2.62k | while (left <= right) { Branch (54:12): [True: 2.16k, False: 464]
|
55 | 2.16k | size_t mid = left + (right - left) / 2; |
56 | | |
57 | 2.16k | if (list->offsets[mid] == cursor) { Branch (57:13): [True: 59, False: 2.10k]
|
58 | 59 | return ((int32_t) mid) + start_line; |
59 | 59 | } |
60 | | |
61 | 2.10k | if (list->offsets[mid] < cursor) { Branch (61:13): [True: 1.16k, False: 935]
|
62 | 1.16k | left = mid + 1; |
63 | 1.16k | } else { |
64 | 935 | right = mid - 1; |
65 | 935 | } |
66 | 2.10k | } |
67 | | |
68 | 464 | return ((int32_t) left) + start_line - 1; |
69 | 523 | } |
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 | 3.49M | pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) { |
78 | 3.49M | size_t left = 0; |
79 | 3.49M | size_t right = list->size - 1; |
80 | | |
81 | 20.5M | while (left <= right) { Branch (81:12): [True: 17.6M, False: 2.88M]
|
82 | 17.6M | size_t mid = left + (right - left) / 2; |
83 | | |
84 | 17.6M | if (list->offsets[mid] == cursor) { Branch (84:13): [True: 605k, False: 17.0M]
|
85 | 605k | return ((pm_line_column_t) { ((int32_t) mid) + start_line, 0 }); |
86 | 605k | } |
87 | | |
88 | 17.0M | if (list->offsets[mid] < cursor) { Branch (88:13): [True: 9.32M, False: 7.72M]
|
89 | 9.32M | left = mid + 1; |
90 | 9.32M | } else { |
91 | 7.72M | right = mid - 1; |
92 | 7.72M | } |
93 | 17.0M | } |
94 | | |
95 | 2.88M | return ((pm_line_column_t) { |
96 | 2.88M | .line = ((int32_t) left) + start_line - 1, |
97 | 2.88M | .column = cursor - list->offsets[left - 1] |
98 | 2.88M | }); |
99 | 3.49M | } |