dte test coverage


Directory: ./
File: src/indent.h
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 12 12 100.0%
Functions: 3 3 100.0%
Branches: 6 6 100.0%

Line Branch Exec Source
1 #ifndef INDENT_H
2 #define INDENT_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include "block-iter.h"
7 #include "options.h"
8 #include "util/bit.h"
9 #include "util/debug.h"
10 #include "util/macros.h"
11 #include "util/string-view.h"
12
13 typedef struct {
14 size_t bytes; // Size in bytes
15 size_t width; // Width in columns
16 size_t level; // Number of whole `indent-width` levels
17 bool wsonly; // Empty or whitespace-only line
18
19 // Only spaces or tabs, depending on `use_spaces_for_indent()`.
20 // Note that a "sane" line can contain spaces after tabs for alignment.
21 bool sane;
22 } IndentInfo;
23
24 // Divide `x` by `d`, to obtain the number of whole indent levels.
25 // If `d` is a power of 2, shift right by `u32_ctz(d)` instead, to
26 // avoid the expensive divide operation. This optimization applies
27 // to widths of 1, 2, 4 and 8, which covers all of the sensible ones.
28 229 static inline size_t indent_level(size_t x, size_t d)
29 {
30 229 BUG_ON(d - 1 > 7);
31
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 72 times.
229 return likely(IS_POWER_OF_2(d)) ? x >> u32_ctz(d) : x / d;
32 }
33
34 728 static inline size_t indent_remainder(size_t x, size_t m)
35 {
36 728 BUG_ON(m - 1 > 7);
37
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 72 times.
728 return likely(IS_POWER_OF_2(m)) ? x & (m - 1) : x % m;
38 }
39
40 188 static inline size_t next_indent_width(size_t x, size_t mul)
41 {
42 188 BUG_ON(mul - 1 > 7);
43
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 85 times.
188 if (likely(IS_POWER_OF_2(mul))) {
44 103 size_t mask = ~(mul - 1);
45 103 return (x & mask) + mul;
46 }
47 85 return ((x + mul) / mul) * mul;
48 }
49
50 char *make_indent(const LocalOptions *options, size_t width);
51 char *get_indent_for_next_line(const LocalOptions *options, const StringView *line);
52 IndentInfo get_indent_info(const LocalOptions *options, const StringView *line);
53 size_t get_indent_width(const StringView *line, unsigned int tab_width);
54 size_t get_indent_level_bytes_left(const LocalOptions *options, BlockIter *cursor);
55 size_t get_indent_level_bytes_right(const LocalOptions *options, BlockIter *cursor);
56
57 #endif
58