From 171baaee93c8e257ef593b30c05405d03ac30c96 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 6 Apr 2017 21:31:37 +0300 Subject: strings: Remove vim_strbyte Ref #1476 --- src/nvim/regexp.c | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 9baa53d2a2..e9c9b491fd 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -3429,32 +3429,26 @@ static long bt_regexec_both(char_u *line, c = *prog->regmust; s = line + col; - /* - * This is used very often, esp. for ":global". Use three versions of - * the loop to avoid overhead of conditions. - */ - if (!ireg_ic - && !has_mbyte - ) - while ((s = vim_strbyte(s, c)) != NULL) { - if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) - break; /* Found it. */ - ++s; - } - else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1)) + // This is used very often, esp. for ":global". Use two versions of + // the loop to avoid overhead of conditions. + if (!ireg_ic) { while ((s = vim_strchr(s, c)) != NULL) { - if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) - break; /* Found it. */ + if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) { + break; // Found it. + } mb_ptr_adv(s); } - else + } else { while ((s = cstrchr(s, c)) != NULL) { - if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) - break; /* Found it. */ + if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) { + break; // Found it. + } mb_ptr_adv(s); } - if (s == NULL) /* Not present. */ + } + if (s == NULL) { // Not present. goto theend; + } } regline = line; @@ -3484,14 +3478,8 @@ static long bt_regexec_both(char_u *line, /* Messy cases: unanchored match. */ while (!got_int) { if (prog->regstart != NUL) { - /* Skip until the char we know it must start with. - * Used often, do some work to avoid call overhead. */ - if (!ireg_ic - && !has_mbyte - ) - s = vim_strbyte(regline + col, prog->regstart); - else - s = cstrchr(regline + col, prog->regstart); + // Skip until the char we know it must start with. + s = cstrchr(regline + col, prog->regstart); if (s == NULL) { retval = 0; break; -- cgit From ac1cb1c72fa762b39ff457153c7fa6ecf1eaedc3 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 6 Apr 2017 21:56:49 +0300 Subject: regexp: Refactor cstrchr Ref #1476 --- src/nvim/regexp.c | 55 +++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) (limited to 'src/nvim/regexp.c') 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); } /*************************************************************** -- cgit From caeff6e1aff227bb5826ad575362d2a24adebaa9 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 7 Apr 2017 23:18:36 +0300 Subject: regexp: Do not use locale-dependent functions in cstrchr --- src/nvim/regexp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 175aa1b970..893089f06d 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -6309,10 +6309,10 @@ static inline char_u *cstrchr(const char_u *const s, const int c) } int cc; - if (vim_isupper(c)) { - cc = vim_tolower(c); - } else if (vim_islower(c)) { - cc = vim_toupper(c); + if (ASCII_ISUPPER(c)) { + cc = TOLOWER_ASC(c); + } else if (ASCII_ISLOWER(c)) { + cc = TOUPPER_ASC(c); } else { return vim_strchr(s, c); } -- cgit From acc52a953b99f78435c34337b8ca9b6716a057a1 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 8 Apr 2017 02:55:51 +0300 Subject: regexp: Update comment in cstrchr() --- src/nvim/regexp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 893089f06d..96884aa87f 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -6295,9 +6295,8 @@ static inline char_u *cstrchr(const char_u *const s, const int c) 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. + // Use folded case for UTF-8, slow! For ASCII use libc strpbrk which is + // expected to be highly optimized. if (c > 0x80) { const int folded_c = utf_fold(c); for (const char_u *p = s; *p != NUL; p += utfc_ptr2len(p)) { -- cgit From 69ebfb8d8ea26061a4fbfe9ee07d7bfff49e6a2b Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 16 Apr 2017 21:35:50 +0300 Subject: regexp: Fix warning about octal constant --- src/nvim/regexp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 4b5e17b00b..e15a2eebe5 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -2398,7 +2398,7 @@ collection: regc('\b'); break; case CLASS_ESCAPE: - regc('\033'); + regc(ESC); break; } } else { -- cgit From b7118a008af1f0a15f7cfbe070a1f590310cc7b5 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 16 Apr 2017 21:36:53 +0300 Subject: regexp: Remove another has_mbyte/…-checking stuff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nvim/regexp.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index e15a2eebe5..284cb27c40 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -2923,13 +2923,8 @@ static void skipchr(void) else prevchr_len = 0; if (regparse[prevchr_len] != NUL) { - if (enc_utf8) - /* exclude composing chars that mb_ptr2len does include */ - prevchr_len += utf_ptr2len(regparse + prevchr_len); - else if (has_mbyte) - prevchr_len += (*mb_ptr2len)(regparse + prevchr_len); - else - ++prevchr_len; + // Exclude composing chars that utfc_ptr2len does include. + prevchr_len += utf_ptr2len(regparse + prevchr_len); } regparse += prevchr_len; prev_at_start = at_start; -- cgit From 30561afe4128ab0c5371af00641d5c5a682d6270 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 16 Apr 2017 21:38:06 +0300 Subject: regexp: Silence octal constant warning --- src/nvim/regexp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 284cb27c40..7be89c2d7e 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -3047,7 +3047,7 @@ static int getoctchrs(void) int c; int i; - for (i = 0; i < 3 && nr < 040; ++i) { + for (i = 0; i < 3 && nr < 040; i++) { // -V536 c = regparse[0]; if (c < '0' || c > '7') break; -- cgit From c2f3e361c52ec4e7149ea1d8c6a1202e0873da8e Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 19 Apr 2017 19:11:50 +0300 Subject: *: Add comment to all C files --- src/nvim/regexp.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/regexp.c') diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 7be89c2d7e..7a00ee27bb 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -1,3 +1,6 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + /* * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub() * -- cgit