strings: Replace vim_strchr implementation with a saner one

Removes dead code (enc_utf8, enc_dbcs and has_mbyte now have hardcoded values),
relies on libc implementation being more optimized. Also where previously
negative character just would never be found it is an assertion error now.

Ref #1476
This commit is contained in:
ZyX 2017-04-06 21:21:11 +03:00
parent c501d7c432
commit 19044a15f9

View File

@ -401,54 +401,28 @@ int vim_strnicmp(const char *s1, const char *s2, size_t len)
} }
#endif #endif
/* /// strchr() version which handles multibyte strings
* Version of strchr() and strrchr() that handle unsigned char strings ///
* with characters from 128 to 255 correctly. It also doesn't return a /// @param[in] string String to search in.
* pointer to the NUL at the end of the string. /// @param[in] c Character to search for. Must be a valid character.
*/ ///
char_u *vim_strchr(const char_u *string, int c) /// @return Pointer to the first byte of the found character in string or NULL
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE /// if it was not found. NUL character is never found, use `strlen()`
/// instead.
char_u *vim_strchr(const char_u *const string, const int c)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{ {
int b; assert(c >= 0);
if (c == 0) {
const char_u *p = string;
if (enc_utf8 && c >= 0x80) {
while (*p != NUL) {
int l = (*mb_ptr2len)(p);
// Avoid matching an illegal byte here.
if (l > 1 && utf_ptr2char(p) == c) {
return (char_u *) p;
}
p += l;
}
return NULL; return NULL;
} else if (c < 0x80) {
return (char_u *)strchr((const char *)string, c);
} else {
char u8char[MB_MAXBYTES + 1];
const int len = utf_char2bytes(c, (char_u *)u8char);
u8char[len] = NUL;
return (char_u *)strstr((const char *)string, u8char);
} }
if (enc_dbcs != 0 && c > 255) {
int n2 = c & 0xff;
c = ((unsigned)c >> 8) & 0xff;
while ((b = *p) != NUL) {
if (b == c && p[1] == n2)
return (char_u *) p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
if (has_mbyte) {
while ((b = *p) != NUL) {
if (b == c)
return (char_u *) p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
while ((b = *p) != NUL) {
if (b == c)
return (char_u *) p;
++p;
}
return NULL;
} }
/* /*