patch 9.1.1076: vim_strnchr() is strange and unnecessary
Problem: vim_strnchr() is strange and unnecessary (after v9.1.1009)
Solution: Remove vim_strnchr() and use memchr() instead. Also remove a
comment referencing an #if that is no longer present.
vim_strnchr() is strange in several ways:
- It's named like vim_strchr(), but unlike vim_strchr() it doesn't
support finding a multibyte char.
- Its logic is similar to vim_strbyte(), but unlike vim_strbyte() it
uses char instead of char_u.
- It takes a pointer as its size argument, which isn't convenient for
all its callers.
- It allows embedded NULs, unlike other "strn*" functions which stop
when encountering a NUL byte.
In comparison, memchr() also allows embedded NULs, and it converts bytes
in the string to (unsigned char).
closes: #16579
Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/linematch.c b/src/linematch.c
index c1463fd..17e932d 100644
--- a/src/linematch.c
+++ b/src/linematch.c
@@ -34,14 +34,10 @@
line_len(const mmfile_t *m)
{
char *s = m->ptr;
- size_t n = (size_t)m->size;
char *end;
- end = vim_strnchr(s, &n, '\n');
- if (end)
- return (size_t)(end - s);
-
- return (size_t)m->size;
+ end = memchr(s, '\n', (size_t)m->size);
+ return end ? (size_t)(end - s) : (size_t)m->size;
}
#define MATCH_CHAR_MAX_LEN 800
@@ -171,10 +167,11 @@
{
for (int i = 0; i < lnum - 1; i++)
{
- size_t n = (size_t)s.size;
+ char *line_end;
- s.ptr = vim_strnchr(s.ptr, &n, '\n');
- s.size = (int)n;
+ line_end = memchr(s.ptr, '\n', (size_t)s.size);
+ s.size = line_end ? (int)(s.size - (line_end - s.ptr)) : 0;
+ s.ptr = line_end;
if (!s.ptr)
break;
s.ptr++;