Skip to content

Instantly share code, notes, and snippets.

@koron
Created January 12, 2012 13:36
Show Gist options
  • Save koron/1600566 to your computer and use it in GitHub Desktop.
Save koron/1600566 to your computer and use it in GitHub Desktop.
Rev.5
# HG changeset patch
# Parent 2cab0b1a3f2992429db480acd43d852a4c85d95e
diff -r 2cab0b1a3f29 src/eval.c
--- a/src/eval.c Wed Jan 11 18:57:32 2012 +0900
+++ b/src/eval.c Fri Jan 13 00:39:06 2012 +0900
@@ -6572,6 +6572,81 @@
return (char_u *)ga.ga_data;
}
+typedef struct join_S {
+ char_u *s;
+ char_u *tofree;
+} join_T;
+
+ static int
+list_join2(gap, l, sep, echo_style, copyID, gap2)
+ garray_T *gap;
+ list_T *l;
+ char_u *sep;
+ int echo_style;
+ int copyID;
+ garray_T *gap2;
+{
+ 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 items in the list. */
+ for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
+ {
+ if (echo_style)
+ s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
+ else
+ s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
+ if (s == NULL)
+ return FAIL;
+
+ len = (int)STRLEN(s);
+ sumlen += len;
+
+ ga_grow(gap2, 1);
+ p = ((join_T *)gap2->ga_data) + (gap2->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();
+ }
+
+ /* Allocate result buffer with its total size. */
+ if (gap2->ga_len >= 2)
+ sumlen += (int)STRLEN(sep) * (gap2->ga_len - 1);
+ ga_grow(gap, sumlen + 1);
+
+ for (i = 0; i < gap2->ga_len && !got_int; ++i)
+ {
+ if (first)
+ first = FALSE;
+ else
+ ga_concat(gap, sep);
+ p = ((join_T*)gap2->ga_data) + i;
+
+ if (p->s != NULL)
+ ga_concat(gap, p->s);
+ line_breakcheck();
+ }
+
+ return OK;
+}
+
/*
* Join list "l" into a string in "*gap", using separator "sep".
* When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
@@ -6585,31 +6660,27 @@
int echo_style;
int copyID;
{
- int first = TRUE;
- char_u *tofree;
- char_u numbuf[NUMBUFLEN];
- listitem_T *item;
- char_u *s;
-
- for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
- {
- if (first)
- first = FALSE;
- else
- ga_concat(gap, sep);
-
- if (echo_style)
- s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
- else
- s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
- if (s != NULL)
- ga_concat(gap, s);
- vim_free(tofree);
- if (s == NULL)
- return FAIL;
- line_breakcheck();
- }
- return OK;
+ garray_T join_array;
+ int retval;
+ join_T *p;
+ int i;
+
+ ga_init2(&join_array, (int)sizeof(join_T), l->lv_len);
+ retval = list_join2(gap, l, sep, echo_style, copyID, &join_array);
+
+ /* Dispose each items in join_array. */
+ if (join_array.ga_data != NULL)
+ {
+ p = (join_T *)join_array.ga_data;
+ for (i = 0; i < join_array.ga_len; ++i)
+ {
+ vim_free(p->tofree);
+ ++p;
+ }
+ ga_clear(&join_array);
+ }
+
+ return retval;
}
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment