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 | 7 | static inline void block_iter_bof(BlockIter *bi) | |
22 | { | ||
23 | 7 | bi->blk = BLOCK(bi->head->next); | |
24 | 7 | bi->offset = 0; | |
25 | 7 | } | |
26 | |||
27 | 13 | static inline void block_iter_eof(BlockIter *bi) | |
28 | { | ||
29 | 13 | bi->blk = BLOCK(bi->head->prev); | |
30 | 13 | bi->offset = bi->blk->size; | |
31 | 13 | } | |
32 | |||
33 | 202 | static inline bool block_iter_is_eof(const BlockIter *bi) | |
34 | { | ||
35 |
3/4✓ Branch 0 taken 33 times.
✓ Branch 1 taken 169 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
202 | return bi->offset == bi->blk->size && bi->blk->node.next == bi->head; |
36 | } | ||
37 | |||
38 | 215 | static inline bool block_iter_is_bol(const BlockIter *bi) | |
39 | { | ||
40 | // See also: Block invariants mentioned in sanity_check_blocks() | ||
41 |
4/4✓ Branch 0 taken 138 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 87 times.
|
215 | return bi->offset == 0 || bi->blk->data[bi->offset - 1] == '\n'; |
42 | } | ||
43 | |||
44 | ✗ | static inline bool block_iter_is_eol(const BlockIter *bi) | |
45 | { | ||
46 | ✗ | const Block *blk = bi->blk; | |
47 | ✗ | size_t offset = bi->offset; | |
48 | ✗ | if (offset == blk->size) { | |
49 | ✗ | if (blk->node.next == bi->head) { | |
50 | // EOF | ||
51 | return true; | ||
52 | } | ||
53 | // Normalize | ||
54 | ✗ | blk = BLOCK(blk->node.next); | |
55 | ✗ | offset = 0; | |
56 | } | ||
57 | ✗ | return blk->data[offset] == '\n'; | |
58 | } | ||
59 | |||
60 | 864 | static inline void block_iter_normalize(BlockIter *bi) | |
61 | { | ||
62 | 864 | const Block *blk = bi->blk; | |
63 |
3/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 798 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
|
864 | if (bi->offset == blk->size && blk->node.next != bi->head) { |
64 | ✗ | bi->blk = BLOCK(blk->node.next); | |
65 | ✗ | bi->offset = 0; | |
66 | } | ||
67 | 864 | } | |
68 | |||
69 | size_t block_iter_eat_line(BlockIter *bi); | ||
70 | size_t block_iter_next_line(BlockIter *bi); | ||
71 | size_t block_iter_prev_line(BlockIter *bi); | ||
72 | size_t block_iter_next_char(BlockIter *bi, CodePoint *up); | ||
73 | size_t block_iter_prev_char(BlockIter *bi, CodePoint *up); | ||
74 | size_t block_iter_next_column(BlockIter *bi); | ||
75 | size_t block_iter_prev_column(BlockIter *bi); | ||
76 | size_t block_iter_bol(BlockIter *bi); | ||
77 | size_t block_iter_eol(BlockIter *bi); | ||
78 | size_t block_iter_skip_blanks_fwd(BlockIter *bi); | ||
79 | size_t block_iter_skip_blanks_bwd(BlockIter *bi); | ||
80 | bool block_iter_find_non_empty_line_bwd(BlockIter *bi); | ||
81 | void block_iter_back_bytes(BlockIter *bi, size_t count); | ||
82 | void block_iter_skip_bytes(BlockIter *bi, size_t count); | ||
83 | void block_iter_goto_offset(BlockIter *bi, size_t offset); | ||
84 | void block_iter_goto_line(BlockIter *bi, size_t line); | ||
85 | size_t block_iter_get_offset(const BlockIter *bi) WARN_UNUSED_RESULT; | ||
86 | size_t block_iter_get_char(const BlockIter *bi, CodePoint *up) WARN_UNUSED_RESULT; | ||
87 | char *block_iter_get_bytes(const BlockIter *bi, size_t len) WARN_UNUSED_RESULT; | ||
88 | StringView block_iter_get_line(BlockIter *bi); | ||
89 | StringView block_iter_get_line_with_nl(BlockIter *bi); | ||
90 | size_t fetch_this_line(const BlockIter *bi, StringView *line); | ||
91 | |||
92 | #endif | ||
93 |