mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
vim-patch:9.1.1076: vim_strnchr() is strange and unnecessary (#32327)
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: vim/vim#16579
34e1e8de91
This commit is contained in:
@@ -184,7 +184,6 @@ information.
|
|||||||
mch_memmove memmove
|
mch_memmove memmove
|
||||||
vim_memset copy_chars copy_spaces memset
|
vim_memset copy_chars copy_spaces memset
|
||||||
vim_strbyte strchr
|
vim_strbyte strchr
|
||||||
vim_strnchr strnchr
|
|
||||||
vim_strncpy strncpy xstrlcpy/xmemcpyz
|
vim_strncpy strncpy xstrlcpy/xmemcpyz
|
||||||
vim_strcat strncat xstrlcat
|
vim_strcat strncat xstrlcat
|
||||||
VIM_ISWHITE ascii_iswhite
|
VIM_ISWHITE ascii_iswhite
|
||||||
|
@@ -32,12 +32,8 @@ struct diffcmppath_S {
|
|||||||
static size_t line_len(const mmfile_t *m)
|
static size_t line_len(const mmfile_t *m)
|
||||||
{
|
{
|
||||||
char *s = m->ptr;
|
char *s = m->ptr;
|
||||||
size_t n = (size_t)m->size;
|
char *end = memchr(s, '\n', (size_t)m->size);
|
||||||
char *end = strnchr(s, &n, '\n');
|
return end ? (size_t)(end - s) : (size_t)m->size;
|
||||||
if (end) {
|
|
||||||
return (size_t)(end - s);
|
|
||||||
}
|
|
||||||
return (size_t)m->size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MATCH_CHAR_MAX_LEN 800
|
#define MATCH_CHAR_MAX_LEN 800
|
||||||
@@ -148,9 +144,9 @@ static int count_n_matched_chars(mmfile_t **sp, const size_t n, bool iwhite)
|
|||||||
mmfile_t fastforward_buf_to_lnum(mmfile_t s, linenr_T lnum)
|
mmfile_t fastforward_buf_to_lnum(mmfile_t s, linenr_T lnum)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < lnum - 1; i++) {
|
for (int i = 0; i < lnum - 1; i++) {
|
||||||
size_t n = (size_t)s.size;
|
char *line_end = memchr(s.ptr, '\n', (size_t)s.size);
|
||||||
s.ptr = strnchr(s.ptr, &n, '\n');
|
s.size = line_end ? (int)(s.size - (line_end - s.ptr)) : 0;
|
||||||
s.size = (int)n;
|
s.ptr = line_end;
|
||||||
if (!s.ptr) {
|
if (!s.ptr) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@@ -498,20 +498,6 @@ char *vim_strchr(const char *const string, const int c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sized version of strchr that can handle embedded NULs.
|
|
||||||
// Adjusts n to the new size.
|
|
||||||
char *strnchr(const char *p, size_t *n, int c)
|
|
||||||
{
|
|
||||||
while (*n > 0) {
|
|
||||||
if (*p == c) {
|
|
||||||
return (char *)p;
|
|
||||||
}
|
|
||||||
p++;
|
|
||||||
(*n)--;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort an array of strings.
|
// Sort an array of strings.
|
||||||
|
|
||||||
static int sort_compare(const void *s1, const void *s2)
|
static int sort_compare(const void *s1, const void *s2)
|
||||||
|
Reference in New Issue
Block a user