/* * Return an allocated string with the string representation of a Dictionary. * May return NULL. */ char_u * dict2string(typval_T *tv, int copyID, int restore_copyID) { garray_T ga; int first = TRUE; char_u *tofree; char_u numbuf[NUMBUFLEN]; hashitem_T *hi; char_u *s; dict_T *d; int todo; if ((d = tv->vval.v_dict) == NULL) return NULL; ga_init2(&ga, (int)sizeof(char), 80); ga_append(&ga, '{'); todo = (int)d->dv_hashtab.ht_used; for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) { if (!HASHITEM_EMPTY(hi)) { --todo; if (first) first = FALSE; else ga_concat(&ga, (char_u *)", "); tofree = string_quote(hi->hi_key, FALSE); if (tofree != NULL) { ga_concat(&ga, tofree); vim_free(tofree); } ga_concat(&ga, (char_u *)": "); s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID, FALSE, restore_copyID, TRUE); if (s != NULL) ga_concat(&ga, s); vim_free(tofree); if (s == NULL || did_echo_string_emsg) break; line_breakcheck(); } } if (todo > 0) { vim_free(ga.ga_data); return NULL; } ga_append(&ga, '}'); ga_append(&ga, NUL); return (char_u *)ga.ga_data; }
static int list_join_inner( garray_T *gap, /* to store the result in */ list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap) /* to keep each list item string */ { int i; join_T *p; int len; int sumlen = 0; int first = TRUE; char_u *tofree; char_u numbuf[NUMBUFLEN]; listitem_T *item; char_u *s; /* Stringify each item in the list. */ for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) { s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID, echo_style, restore_copyID, !echo_style); if (s == NULL) return FAIL; len = (int)STRLEN(s); sumlen += len; (void)ga_grow(join_gap, 1); p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++); if (tofree != NULL || s != numbuf) { p->s = s; p->tofree = tofree; } else { p->s = vim_strnsave(s, len); p->tofree = p->s; } line_breakcheck(); if (did_echo_string_emsg) /* recursion error, bail out */ break; } /* Allocate result buffer with its total size, avoid re-allocation and * multiple copy operations. Add 2 for a tailing ']' and NUL. */ if (join_gap->ga_len >= 2) sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1); if (ga_grow(gap, sumlen + 2) == FAIL) return FAIL; for (i = 0; i < join_gap->ga_len && !got_int; ++i) { if (first) first = FALSE; else ga_concat(gap, sep); p = ((join_T *)join_gap->ga_data) + i; if (p->s != NULL) ga_concat(gap, p->s); line_breakcheck(); } return OK; }