vim-patch:9.1.1983: Vim9: class_name definition can be improved

Problem:  Vim9: class_name definition can be improved
Solution: Use string_T to store class_name, avoid using STRLEN() calls,
          simplify code, remove unused definition of struct
          oc_newmember_S (John Marriott)

Use struct string_T to store the field class_name in struct class_T,
which means we can just use the .length field in struct string_T
instead of measuring it.

In addition:
1. In eval.c use string_T to store class_name and s in function
   class_tv2string().
2. In vim9type.c change some calls from ga_concat() to ga_concat_len()
   where the length is known.
3. In vim9class.c remove unused struct definition oc_newmember_S.
   Change some calls from ga_concat() to ga_concat_len() where the
   length is known.
4. In scriptfile.c use string_T to store type_name, class_name and
   es_name in function estack_sfile().
5. In function estack_sfile() simplify construction of the grow array ga
   and change some calls from ga_concat() to ga_concat_len() when the
   length is known.

closes: vim/vim#18925

2019321e0b

Co-authored-by: John Marriott <basilisk@internode.on.net>
This commit is contained in:
zeertzjq
2025-12-16 08:13:18 +08:00
parent 53fd3657cc
commit 6061169183

View File

@@ -195,38 +195,41 @@ char *estack_sfile(estack_arg_T which)
for (int idx = 0; idx < exestack.ga_len; idx++) {
entry = ((estack_T *)exestack.ga_data) + idx;
if (entry->es_name != NULL) {
char *type_name = "";
String type_name = STATIC_CSTR_AS_STRING("");
String es_name = cstr_as_string(entry->es_name);
if (entry->es_type != last_type) {
switch (entry->es_type) {
case ETYPE_SCRIPT:
type_name = "script "; break;
type_name = STATIC_CSTR_AS_STRING("script "); break;
case ETYPE_UFUNC:
type_name = "function "; break;
type_name = STATIC_CSTR_AS_STRING("function "); break;
default:
type_name = ""; break;
break;
}
last_type = entry->es_type;
}
linenr_T lnum = idx == exestack.ga_len - 1
? which == ESTACK_STACK ? SOURCING_LNUM : 0
: entry->es_lnum;
size_t len = strlen(entry->es_name) + strlen(type_name) + 26;
size_t len = es_name.size + type_name.size + 26;
ga_grow(&ga, (int)len);
ga_concat(&ga, type_name);
ga_concat(&ga, entry->es_name);
ga_concat_len(&ga, type_name.data, type_name.size);
ga_concat_len(&ga, es_name.data, es_name.size);
// For the bottom entry of <sfile>: do not add the line number, it is used in
// <slnum>. Also leave it out when the number is not set.
if (lnum != 0) {
ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len, 23,
"[%" PRIdLINENR "]", lnum);
ga.ga_len += (int)vim_snprintf_safelen((char *)ga.ga_data + ga.ga_len,
len - (size_t)ga.ga_len,
"[%" PRIdLINENR "]", lnum);
}
if (idx != exestack.ga_len - 1) {
ga_concat(&ga, "..");
ga_concat_len(&ga, S_LEN(".."));
}
}
}
ga_append(&ga, '\0');
ga_append(&ga, NUL);
return (char *)ga.ga_data;
}