summaryrefslogtreecommitdiff
path: root/do/join
blob: c5c522009973f89395ef68b1ee44956da5711914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void
do_join(TARG,arglast)
register STR *TARG;
int *arglast;
{
    register STR **st = stack->ary_array;
    int sp = arglast[1];
    register int items = arglast[2] - sp;
    register char *delim = str_get(st[sp]);
    register STRLEN len;
    int delimlen = st[sp]->str_cur;

    st += sp + 1;

    len = (items > 0 ? (delimlen * (items - 1) ) : 0);
    if (TARG->str_len < len + items) {	/* current length is way too short */
	while (items-- > 0) {
	    if (*st)
		len += (*st)->str_cur;
	    st++;
	}
	STR_GROW(TARG, len + 1);		/* so try to pre-extend */

	items = arglast[2] - sp;
	st -= items;
    }

    if (items-- > 0)
	str_sset(TARG, *st++);
    else
	str_set(TARG,"");
    len = delimlen;
    if (len) {
	for (; items > 0; items--,st++) {
	    str_ncat(TARG,delim,len);
	    str_scat(TARG,*st);
	}
    }
    else {
	for (; items > 0; items--,st++)
	    str_scat(TARG,*st);
    }
    STABSET(TARG);
}