return failed ? FAIL : OK;
}
+/*
+ * Append string "s2" to the string in "tv1".
+ * Returns OK if "tv1" was grown in place, FAIL otherwise.
+ */
+ static int
+grow_string_tv(typval_T *tv1, char_u *s2)
+{
+ if (tv1->v_type != VAR_STRING || tv1->vval.v_string == NULL)
+ return FAIL;
+
+ size_t len1 = STRLEN(tv1->vval.v_string);
+ size_t len2 = STRLEN(s2);
+ char_u *p = vim_realloc(tv1->vval.v_string, len1 + len2 + 1);
+
+ if (p == NULL)
+ return FAIL;
+
+ mch_memmove(p + len1, s2, len2 + 1);
+ tv1->vval.v_string = p;
+ return OK;
+}
+
/*
* Handle "str1 .= str2"
* Returns OK or FAIL.
{
char_u numbuf[NUMBUFLEN];
char_u *s;
+ char_u *s2;
if (tv2->v_type == VAR_FLOAT)
return FAIL;
// str .= str
+ s2 = tv_get_string_buf(tv2, numbuf);
+ if (grow_string_tv(tv1, s2) == OK)
+ return OK;
+
s = tv_get_string(tv1);
- s = concat_str(s, tv_get_string_buf(tv2, numbuf));
+ s = concat_str(s, s2);
clear_tv(tv1);
tv1->v_type = VAR_STRING;
tv1->vval.v_string = s;
return FAIL;
}
+ // When possible, grow the existing string in place to avoid alloc/free.
+ if (grow_string_tv(tv1, s2) == OK)
+ return OK;
+
p = concat_str(s1, s2);
clear_tv(tv1);
tv1->v_type = VAR_STRING;