dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 75.0% 36 / 0 / 48
Functions: 92.9% 13 / 0 / 14
Branches: 57.7% 15 / 2 / 28

src/block-iter.h
Line Branch Exec Source
1 #ifndef BLOCK_ITER_H
2 #define BLOCK_ITER_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include "block.h"
7 #include "util/list.h"
8 #include "util/macros.h"
9 #include "util/string-view.h"
10 #include "util/unicode.h"
11
12 // An iterator used to represent the cursor position for each View of a
13 // Buffer (see View::cursor) and also as a means for accessing/iterating
14 // the Blocks that make up the text contents of a Buffer (see Buffer::blocks).
15 typedef struct {
16 Block *blk; // The Block this iterator/cursor currently points to
17 const ListHead *head; // A pointer to the Buffer::blocks that owns `blk`
18 size_t offset; // The current position within `blk->data`
19 } BlockIter;
20
21 typedef struct {
22 StringView line;
23 size_t cursor_offset;
24 } CurrentLineRef;
25
26 15 static inline Block *block_iter_get_first_block(BlockIter *bi)
27 {
28 15 return BLOCK(bi->head->next);
29 }
30
31 8 static inline Block *block_iter_get_last_block(BlockIter *bi)
32 {
33 8 return BLOCK(bi->head->prev);
34 }
35
36 11 static inline void block_iter_bof(BlockIter *bi)
37 {
38 11 bi->blk = block_iter_get_first_block(bi);
39 11 bi->offset = 0;
40 11 }
41
42 8 static inline void block_iter_eof(BlockIter *bi)
43 {
44 8 bi->blk = block_iter_get_last_block(bi);
45 8 bi->offset = bi->blk->size;
46 8 }
47
48 101 static inline bool block_iter_is_bof(const BlockIter *bi)
49 {
50
3/4
✓ Branch 2 → 3 taken 10 times.
✓ Branch 2 → 5 taken 91 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 10 times.
101 return bi->offset == 0 && !block_has_prev(bi->blk, bi->head);
51 }
52
53 405 static inline bool block_iter_is_eof(const BlockIter *bi)
54 {
55
3/4
✓ Branch 2 → 3 taken 74 times.
✓ Branch 2 → 5 taken 331 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 74 times.
405 return bi->offset == bi->blk->size && !block_has_next(bi->blk, bi->head);
56 }
57
58 472 static inline bool block_iter_is_bol(const BlockIter *bi)
59 {
60 // See also: Block invariants mentioned in sanity_check_blocks()
61
4/4
✓ Branch 2 → 3 taken 373 times.
✓ Branch 2 → 5 taken 99 times.
✓ Branch 3 → 4 taken 211 times.
✓ Branch 3 → 5 taken 162 times.
472 return bi->offset == 0 || bi->blk->data[bi->offset - 1] == '\n';
62 }
63
64 static inline bool block_iter_is_eol(const BlockIter *bi)
65 {
66 const Block *blk = bi->blk;
67 if (bi->offset == blk->size) {
68 bool is_last_block = !block_has_next(bi->blk, bi->head);
69 return is_last_block || block_next(blk)->data[0] == '\n';
70 }
71
72 return blk->data[bi->offset] == '\n';
73 }
74
75 143 static inline bool block_iter_next_block(BlockIter *bi)
76 {
77
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 143 times.
143 if (!block_has_next(bi->blk, bi->head)) {
78 return false; // Already on last Block
79 }
80
81 bi->blk = block_next(bi->blk);
82 bi->offset = 0; // Beginning of next Block
83 return true;
84 }
85
86 6 static inline bool block_iter_end_of_prev_block(BlockIter *bi)
87 {
88
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 6 times.
6 if (!block_has_prev(bi->blk, bi->head)) {
89 return false; // Already on first Block
90 }
91
92 bi->blk = block_prev(bi->blk);
93 bi->offset = bi->blk->size; // End of previous Block
94 return true;
95 }
96
97 1520 static inline bool block_iter_normalize(BlockIter *bi)
98 {
99 1520 BUG_ON(bi->offset > bi->blk->size);
100
3/4
✓ Branch 4 → 5 taken 103 times.
✓ Branch 4 → 8 taken 1417 times.
✓ Branch 6 → 7 taken 103 times.
✗ Branch 6 → 8 not taken.
1520 return (bi->offset == bi->blk->size) && block_iter_next_block(bi);
101 }
102
103 size_t block_iter_eat_line(BlockIter *bi) NONNULL_ARGS;
104 size_t block_iter_next_line(BlockIter *bi) NONNULL_ARGS;
105 size_t block_iter_prev_line(BlockIter *bi) NONNULL_ARGS;
106 size_t block_iter_next_char(BlockIter *bi, CodePoint *up) NONNULL_ARGS READWRITE(1) WRITEONLY(2);
107 size_t block_iter_prev_char(BlockIter *bi, CodePoint *up) NONNULL_ARGS READWRITE(1) WRITEONLY(2);
108 size_t block_iter_next_column(BlockIter *bi) NONNULL_ARGS;
109 size_t block_iter_prev_column(BlockIter *bi) NONNULL_ARGS;
110 size_t block_iter_bol(BlockIter *bi) NONNULL_ARGS;
111 size_t block_iter_eol(BlockIter *bi) NONNULL_ARGS;
112 size_t block_iter_skip_blanks_fwd(BlockIter *bi) NONNULL_ARGS;
113 size_t block_iter_skip_blanks_bwd(BlockIter *bi) NONNULL_ARGS;
114 bool block_iter_find_non_empty_line_bwd(BlockIter *bi) NONNULL_ARGS;
115 void block_iter_back_bytes(BlockIter *bi, size_t count) NONNULL_ARGS;
116 void block_iter_skip_bytes(BlockIter *bi, size_t count) NONNULL_ARGS;
117 void block_iter_goto_offset(BlockIter *bi, size_t offset) NONNULL_ARGS;
118 void block_iter_goto_line(BlockIter *bi, size_t line) NONNULL_ARGS;
119 size_t block_iter_get_offset(const BlockIter *bi) WARN_UNUSED_RESULT NONNULL_ARGS;
120 size_t block_iter_get_char(const BlockIter *bi, CodePoint *up) WARN_UNUSED_RESULT NONNULL_ARGS READONLY(1) WRITEONLY(2);
121 char *block_iter_get_bytes(BlockIter bi, size_t len) WARN_UNUSED_RESULT;
122 StringView block_iter_get_line_with_nl(BlockIter *bi) NONNULL_ARGS;
123
124 // Like block_iter_get_line_with_nl(), but excluding the newline
125 386 static inline StringView block_iter_get_line(BlockIter *bi)
126 {
127 386 StringView line = block_iter_get_line_with_nl(bi);
128 386 line.length -= (line.length > 0); // Trim the newline (if any)
129 386 return line;
130 }
131
132 // Like block_iter_get_line(), but always returning whole lines
133 18 static inline StringView get_current_line(BlockIter tmp)
134 {
135 18 block_iter_bol(&tmp);
136 18 return block_iter_get_line(&tmp);
137 }
138
139 // Like get_current_line(), but returning a struct that also includes the
140 // cursor offset (relative to BOL)
141 55 static inline CurrentLineRef get_current_line_and_offset(BlockIter tmp)
142 {
143 55 size_t offset = block_iter_bol(&tmp);
144 110 return (CurrentLineRef) {
145 55 .line = block_iter_get_line(&tmp),
146 .cursor_offset = offset,
147 };
148 }
149
150 #endif
151