aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames <89495599+IAKOBVS@users.noreply.github.com>2024-03-13 03:34:59 +0700
committerGitHub <noreply@github.com>2024-03-13 04:34:59 +0800
commit119390e7ce3fe4f4f3da8bdd963ea10ec8976a3a (patch)
tree34a997557975ebe999a29bd882cdeef8f2ddbdf9
parentdd0fc73bbed4fde9db093c8798601a25d290ad1f (diff)
downloadrneovim-119390e7ce3fe4f4f3da8bdd963ea10ec8976a3a.tar.gz
rneovim-119390e7ce3fe4f4f3da8bdd963ea10ec8976a3a.tar.bz2
rneovim-119390e7ce3fe4f4f3da8bdd963ea10ec8976a3a.zip
refactor: avoid copying before vim_strup() if possible (#27830)
Current uses of vim_strup() calls memcpy()/strcpy() before calling vim_strup(). This results in 2 * strlen(string) operations. We can trivially convert to lowercase while copying the string instead.
-rw-r--r--src/nvim/highlight_group.c8
-rw-r--r--src/nvim/strings.c41
2 files changed, 40 insertions, 9 deletions
diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c
index 75c23c5bc4..1474a2ba06 100644
--- a/src/nvim/highlight_group.c
+++ b/src/nvim/highlight_group.c
@@ -1149,9 +1149,8 @@ void do_highlight(const char *line, const bool forceit, const bool init)
error = true;
break;
}
- memcpy(key, key_start, key_len);
- key[key_len] = NUL;
- vim_strup(key);
+ vim_memcpy_up(key, key_start, key_len);
+ key[key_len] = '\0';
linep = skipwhite(linep);
if (strcmp(key, "NONE") == 0) {
@@ -1943,9 +1942,8 @@ int syn_name2id_len(const char *name, size_t len)
// Avoid using stricmp() too much, it's slow on some systems */
// Avoid alloc()/free(), these are slow too.
- memcpy(name_u, name, len);
+ vim_memcpy_up(name_u, name, len);
name_u[len] = '\0';
- vim_strup(name_u);
// map_get(..., int) returns 0 when no key is present, which is
// the expected value for missing highlight group.
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index 01bd610292..f9b945f1da 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -299,8 +299,8 @@ char *vim_strsave_shellescape(const char *string, bool do_special, bool do_newli
char *vim_strsave_up(const char *string)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
- char *p1 = xstrdup(string);
- vim_strup(p1);
+ char *p1 = xmalloc(strlen(string) + 1);
+ vim_strcpy_up(p1, string);
return p1;
}
@@ -309,8 +309,8 @@ char *vim_strsave_up(const char *string)
char *vim_strnsave_up(const char *string, size_t len)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
- char *p1 = xstrnsave(string, len);
- vim_strup(p1);
+ char *p1 = xmalloc(len + 1);
+ vim_strncpy_up(p1, string, len);
return p1;
}
@@ -324,6 +324,39 @@ void vim_strup(char *p)
}
}
+// strcpy plus vim_strup.
+void vim_strcpy_up(char *restrict dst, const char *restrict src)
+ FUNC_ATTR_NONNULL_ALL
+{
+ uint8_t c;
+ while ((c = (uint8_t)(*src++)) != NUL) {
+ *dst++ = (char)(uint8_t)(c < 'a' || c > 'z' ? c : c - 0x20);
+ }
+ *dst = '\0';
+}
+
+// strncpy (NUL-terminated) plus vim_strup.
+void vim_strncpy_up(char *restrict dst, const char *restrict src, size_t n)
+ FUNC_ATTR_NONNULL_ALL
+{
+ uint8_t c;
+ while (n-- && (c = (uint8_t)(*src++)) != NUL) {
+ *dst++ = (char)(uint8_t)(c < 'a' || c > 'z' ? c : c - 0x20);
+ }
+ *dst = '\0';
+}
+
+// memcpy (does not NUL-terminate) plus vim_strup.
+void vim_memcpy_up(char *restrict dst, const char *restrict src, size_t n)
+ FUNC_ATTR_NONNULL_ALL
+{
+ uint8_t c;
+ while (n--) {
+ c = (uint8_t)(*src++);
+ *dst++ = (char)(uint8_t)(c < 'a' || c > 'z' ? c : c - 0x20);
+ }
+}
+
/// Make given string all upper-case or all lower-case
///
/// Handles multi-byte characters as good as possible.