vim-patch:9.1.0218: Unnecessary multiplications in backspace code (#28075)

Problem:  Unnecessary multiplications in backspace code, as
          "col / ts * ts" is the same as "col - col % ts".
Solution: Change "col / ts * ts" to "col - col % ts".  Adjust the loop
          and the comments ins_bs() to be easier to understand.  Update
          tests to reset 'smarttab' properly.
          (zeertzjq)

closes: vim/vim#14308

8ede7a0694
This commit is contained in:
zeertzjq
2024-03-28 18:20:38 +08:00
committed by GitHub
parent 6364fc617d
commit 08b8ccd733
3 changed files with 39 additions and 17 deletions

View File

@@ -3838,7 +3838,7 @@ static bool ins_bs(int c, int mode, int *inserted_space_p)
bool const use_ts = !curwin->w_p_list || curwin->w_p_lcs_chars.tab1;
char *const line = get_cursor_line_ptr();
char *const end_ptr = line + curwin->w_cursor.col;
char *const cursor_ptr = line + curwin->w_cursor.col;
colnr_T vcol = 0;
colnr_T space_vcol = 0;
@@ -3846,9 +3846,10 @@ static bool ins_bs(int c, int mode, int *inserted_space_p)
StrCharInfo space_sci = sci;
bool prev_space = false;
// Find the last whitespace that is preceded by non-whitespace.
// Compute virtual column of cursor position, and find the last
// whitespace before cursor that is preceded by non-whitespace.
// Use charsize_nowrap() so that virtual text and wrapping are ignored.
while (sci.ptr < end_ptr) {
while (sci.ptr < cursor_ptr) {
bool cur_space = ascii_iswhite(sci.chr.value);
if (!prev_space && cur_space) {
space_sci = sci;
@@ -3860,11 +3861,9 @@ static bool ins_bs(int c, int mode, int *inserted_space_p)
}
// Compute the virtual column where we want to be.
colnr_T want_vcol = vcol - 1;
if (want_vcol <= 0) {
want_vcol = 0;
} else if (p_sta && in_indent) {
want_vcol = want_vcol - want_vcol % get_sw_value(curbuf);
colnr_T want_vcol = vcol > 0 ? vcol - 1 : 0;
if (p_sta && in_indent) {
want_vcol -= want_vcol % get_sw_value(curbuf);
} else {
want_vcol = tabstop_start(want_vcol, get_sts_value(), curbuf->b_p_vsts_array);
}

View File

@@ -177,7 +177,7 @@ colnr_T tabstop_start(colnr_T col, int ts, colnr_T *vts)
colnr_T tabcol = 0;
if (vts == NULL || vts[0] == 0) {
return ((col / ts) * ts);
return col - col % ts;
}
const int tabcount = vts[0];
@@ -189,7 +189,7 @@ colnr_T tabstop_start(colnr_T col, int ts, colnr_T *vts)
}
const int excess = (tabcol % vts[tabcount]);
return (excess + ((col - excess) / vts[tabcount]) * vts[tabcount]);
return col - (col - excess) % vts[tabcount];
}
/// Find the number of tabs and spaces necessary to get from one column