patch 9.1.1076: vim_strnchr() is strange and unnecessary
Commit:
https://github.com/vim/vim/commit/34e1e8de91ff4a8922d454e3147ea425784aa0a0
Author: zeertzjq <[email protected]>
Date: Tue Feb 4 16:48:36 2025 +0100
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 <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/src/linematch.c b/src/linematch.c
index c1463fdf1..17e932d00 100644
--- a/src/linematch.c
+++ b/src/linematch.c
@@ -34,14 +34,10 @@ static size_t test_charmatch_paths(diffcmppath_T *node, int
lastdecision);
line_len(const mmfile_t *m)
{
char *s = m->ptr;
- size_t n = (size_t)m->size;
char *end;
- end = vim_strnchr(s, &n, '
');
- if (end)
- return (size_t)(end - s);
-
- return (size_t)m->size;
+ end = memchr(s, '
', (size_t)m->size);
+ return end ? (size_t)(end - s) : (size_t)m->size;
}
#define MATCH_CHAR_MAX_LEN 800
@@ -171,10 +167,11 @@ fastforward_buf_to_lnum(mmfile_t s, linenr_T lnum)
{
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, '
');
- s.size = (int)n;
+ line_end = memchr(s.ptr, '
', (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++;
diff --git a/src/strings.c b/src/strings.c
index 120d39355..5de3162bb 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -674,22 +674,6 @@ vim_strchr(char_u *string, int c)
return NULL;
}
-// Sized version of strchr that can handle embedded NULs.
-// Adjusts n to the new size.
- char *
-vim_strnchr(const char *p, size_t *n, int c)
-{
- while (*n > 0)
- {
- if (*p == c)
- return (char *)p;
- p++;
- (*n)--;
- }
-
- return NULL;
-}
-
/*
* Version of strchr() that only works for bytes and handles unsigned char
* strings with characters above 128 correctly. It also doesn't return a
@@ -3558,8 +3542,6 @@ vim_vsnprintf_typval(
str_arg_l = 0;
else
{
- // Don't put the #if inside memchr(), it can be a
- // macro.
// memchr on HP does not like n > 2^31 !!!
char *q = memchr(str_arg, '
precision <= (size_t)0x7fffffffL ? precision
diff --git a/src/version.c b/src/version.c
index fe4e41810..d71d4d170 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1076,
/**/
1075,
/**/
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1tfLLG-005y17-L0%40256bit.org.