diff options
| author | WormHeamer | 2025-12-29 20:41:07 -0500 |
|---|---|---|
| committer | WormHeamer | 2025-12-29 20:41:07 -0500 |
| commit | 3dcd083d8d0e49238813e0835a05ae87e6bdb937 (patch) | |
| tree | f32c4e2abd5ead7d3c9c7e943adc94410a80f616 /txt.c | |
| parent | 9f2ce437ab6657152effe9f5ed64c8f9daaa2e73 (diff) | |
fix some logic errors in line navigation
Diffstat (limited to 'txt.c')
| -rw-r--r-- | txt.c | 26 |
1 files changed, 14 insertions, 12 deletions
@@ -418,8 +418,9 @@ TxtLoc prev_newline(TxtLoc l) { } TxtLoc start_of_line(TxtLoc l) { + if (at_start(l)) return l; l = prev_newline(l); - if (!at_start(l) && txt_byte(l) == '\n') l = cnext(l); + if (txt_byte(l) == '\n') l = cnext(l); return l; } @@ -436,24 +437,25 @@ u32 get_col(TxtLoc l) { TxtLoc at_col(TxtLoc l, u32 col) { l = start_of_line(l); - while (col--) { - if (txt_chr_next(&l) == '\n') { - l = bprev(l); - break; - } - } + while (col-- && txt_byte(l) != '\n') l = cnext(l); return l; } +TxtLoc next_line_start(TxtLoc l) { + if (txt_byte(l) == '\n') return cnext(l); + return cnext(next_newline(l)); +} + +TxtLoc prev_line_start(TxtLoc l) { + return start_of_line(prev_newline(l)); +} + TxtLoc prev_line(TxtLoc l) { - TxtLoc start = start_of_line(l); - return at_col(cprev(start), get_col(l)); + return at_col(prev_line_start(l), get_col(l)); } TxtLoc next_line(TxtLoc l) { - TxtLoc end = end_of_line(l); - if (at_end(end)) return end; - return at_col(cnext(end), get_col(l)); + return at_col(next_line_start(l), get_col(l)); } /* reading chars */ |
