vim/backport-patch-9.1.0038-Unnecessary-loop-in-getvcol.patch
2025-02-10 11:48:16 +08:00

94 lines
2.5 KiB
Diff

From 4ea37f88e8345ca830271636a2e197a1a46114d2 Mon Sep 17 00:00:00 2001
From: zeertzjq <zeertzjq@outlook.com>
Date: Wed, 17 Jan 2024 20:52:13 +0100
Subject: [PATCH] patch 9.1.0038: Unnecessary loop in getvcol()
Problem: Unnecessary loop in getvcol().
Solution: Compare next char position with pos->col directly.
(zeertzjq)
The loop below already handles end of line before checking for posptr,
and the next char is after pos->col whether pos->col is at the start or
in the middle of the char in question, so neither the NUL check nor the
mb_head_off() are needed when comparing the position of the next char
with pos->col directly.
closes: #13878
Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
---
src/charset.c | 29 ++++++-----------------------
1 file changed, 6 insertions(+), 23 deletions(-)
diff --git a/src/charset.c b/src/charset.c
index 3ea2ecb8e216c2..eef2e8983c280e 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -1178,7 +1178,6 @@ getvcol(
{
colnr_T vcol;
char_u *ptr; // points to current char
- char_u *posptr; // points to char at pos->col
char_u *line; // start of the line
int incr;
int head;
@@ -1190,24 +1189,6 @@ getvcol(
vcol = 0;
line = ptr = ml_get_buf(wp->w_buffer, pos->lnum, FALSE);
- if (pos->col == MAXCOL)
- posptr = NULL; // continue until the NUL
- else
- {
- colnr_T i;
-
- // In a few cases the position can be beyond the end of the line.
- for (i = 0; i < pos->col; ++i)
- if (ptr[i] == NUL)
- {
- pos->col = i;
- break;
- }
- posptr = ptr + pos->col;
- if (has_mbyte)
- // always start on the first byte
- posptr -= (*mb_head_off)(line, posptr);
- }
/*
* This function is used very often, do some speed optimizations.
@@ -1263,11 +1244,12 @@ getvcol(
incr = g_chartab[c] & CT_CELL_MASK;
}
- if (posptr != NULL && ptr >= posptr) // character at pos->col
+ char_u *next_ptr = ptr + (*mb_ptr2len)(ptr);
+ if (next_ptr - line > pos->col) // character at pos->col
break;
vcol += incr;
- MB_PTR_ADV(ptr);
+ ptr = next_ptr;
}
}
else
@@ -1284,11 +1266,12 @@ getvcol(
break;
}
- if (posptr != NULL && ptr >= posptr) // character at pos->col
+ char_u *next_ptr = ptr + (*mb_ptr2len)(ptr);
+ if (next_ptr - line > pos->col) // character at pos->col
break;
vcol += incr;
- MB_PTR_ADV(ptr);
+ ptr = next_ptr;
}
}
if (start != NULL)
--
2.43.0