aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-04-06 21:56:49 +0300
committerZyX <kp-pav@yandex.ru>2017-04-07 23:15:56 +0300
commitac1cb1c72fa762b39ff457153c7fa6ecf1eaedc3 (patch)
treeb23c5139655b2a229b6809ea6469e92f3110624a /src
parent171baaee93c8e257ef593b30c05405d03ac30c96 (diff)
downloadrneovim-ac1cb1c72fa762b39ff457153c7fa6ecf1eaedc3.tar.gz
rneovim-ac1cb1c72fa762b39ff457153c7fa6ecf1eaedc3.tar.bz2
rneovim-ac1cb1c72fa762b39ff457153c7fa6ecf1eaedc3.zip
regexp: Refactor cstrchr
Ref #1476
Diffstat (limited to 'src')
-rw-r--r--src/nvim/regexp.c55
1 files changed, 25 insertions, 30 deletions
diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c
index e9c9b491fd..175aa1b970 100644
--- a/src/nvim/regexp.c
+++ b/src/nvim/regexp.c
@@ -6287,43 +6287,38 @@ static int cstrncmp(char_u *s1, char_u *s2, int *n)
/*
* cstrchr: This function is used a lot for simple searches, keep it fast!
*/
-static char_u *cstrchr(char_u *s, int c)
+static inline char_u *cstrchr(const char_u *const s, const int c)
+ FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
+ FUNC_ATTR_ALWAYS_INLINE
{
- char_u *p;
- int cc;
-
- if (!ireg_ic
- || (!enc_utf8 && mb_char2len(c) > 1)
- )
+ if (!ireg_ic) {
return vim_strchr(s, c);
+ }
+
+ // tolower() and toupper() can be slow, comparing twice should be a lot
+ // faster (esp. when using MS Visual C++!).
+ // For UTF-8 need to use folded case.
+ if (c > 0x80) {
+ const int folded_c = utf_fold(c);
+ for (const char_u *p = s; *p != NUL; p += utfc_ptr2len(p)) {
+ if (utf_fold(utf_ptr2char(p)) == folded_c) {
+ return (char_u *)p;
+ }
+ }
+ return NULL;
+ }
- /* tolower() and toupper() can be slow, comparing twice should be a lot
- * faster (esp. when using MS Visual C++!).
- * For UTF-8 need to use folded case. */
- if (enc_utf8 && c > 0x80)
- cc = utf_fold(c);
- else if (vim_isupper(c))
+ int cc;
+ if (vim_isupper(c)) {
cc = vim_tolower(c);
- else if (vim_islower(c))
+ } else if (vim_islower(c)) {
cc = vim_toupper(c);
- else
+ } else {
return vim_strchr(s, c);
+ }
- if (has_mbyte) {
- for (p = s; *p != NUL; p += (*mb_ptr2len)(p)) {
- if (enc_utf8 && c > 0x80) {
- if (utf_fold(utf_ptr2char(p)) == cc)
- return p;
- } else if (*p == c || *p == cc)
- return p;
- }
- } else
- /* Faster version for when there are no multi-byte characters. */
- for (p = s; *p != NUL; ++p)
- if (*p == c || *p == cc)
- return p;
-
- return NULL;
+ char tofind[] = { (char)c, (char)cc, NUL };
+ return (char_u *)strpbrk((const char *)s, tofind);
}
/***************************************************************