| 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 | #include "util/string.h" | ||
| 13 | |||
| 14 | typedef struct { | ||
| 15 | size_t bytes; // Size in bytes | ||
| 16 | size_t width; // Width in columns | ||
| 17 | size_t level; // Number of whole `indent-width` levels | ||
| 18 | bool wsonly; // Empty or whitespace-only line | ||
| 19 | |||
| 20 | // Only spaces or tabs, depending on `use_spaces_for_indent()`. | ||
| 21 | // Note that a "sane" line can contain spaces after tabs for alignment. | ||
| 22 | bool sane; | ||
| 23 | } IndentInfo; | ||
| 24 | |||
| 25 | // Divide `x` by `d`, to obtain the number of whole indent levels. | ||
| 26 | // If `d` is a power of 2, shift right by `u32_ctz(d)` instead, to | ||
| 27 | // avoid the expensive divide operation. This optimization applies | ||
| 28 | // to widths of 1, 2, 4 and 8, which covers all of the sensible ones. | ||
| 29 | 229 | static inline size_t indent_level(size_t x, size_t d) | |
| 30 | { | ||
| 31 | 229 | BUG_ON(d - 1 >= INDENT_WIDTH_MAX); | |
| 32 |
2/2✓ Branch 4 → 5 taken 157 times.
✓ Branch 4 → 7 taken 72 times.
|
229 | return likely(IS_POWER_OF_2(d)) ? x >> u32_ctz(d) : x / d; |
| 33 | } | ||
| 34 | |||
| 35 | 751 | static inline size_t indent_remainder(size_t x, size_t m) | |
| 36 | { | ||
| 37 | 751 | BUG_ON(m - 1 >= INDENT_WIDTH_MAX); | |
| 38 |
2/2✓ Branch 4 → 5 taken 679 times.
✓ Branch 4 → 6 taken 72 times.
|
751 | return likely(IS_POWER_OF_2(m)) ? x & (m - 1) : x % m; |
| 39 | } | ||
| 40 | |||
| 41 | 224 | static inline size_t next_indent_width(size_t x, size_t m) | |
| 42 | { | ||
| 43 | 224 | BUG_ON(m - 1 >= INDENT_WIDTH_MAX); | |
| 44 |
2/2✓ Branch 4 → 5 taken 139 times.
✓ Branch 4 → 6 taken 85 times.
|
224 | return likely(IS_POWER_OF_2(m)) ? next_multiple(x + 1, m) : ((x + m) / m) * m; |
| 45 | } | ||
| 46 | |||
| 47 | String make_indent(const LocalOptions *options, size_t width) WARN_UNUSED_RESULT NONNULL_ARGS; | ||
| 48 | String get_indent_for_next_line(const LocalOptions *options, StringView line) WARN_UNUSED_RESULT NONNULL_ARGS; | ||
| 49 | IndentInfo get_indent_info(const LocalOptions *options, StringView line) WARN_UNUSED_RESULT NONNULL_ARGS; | ||
| 50 | size_t get_indent_width(StringView line, unsigned int tab_width); | ||
| 51 | size_t get_indent_level_bytes_left(const LocalOptions *options, const BlockIter *cursor) NONNULL_ARGS; | ||
| 52 | size_t get_indent_level_bytes_right(const LocalOptions *options, const BlockIter *cursor) NONNULL_ARGS; | ||
| 53 | |||
| 54 | #endif | ||
| 55 |