A non-naive implementation of argvJoin()

- The previous version was as naive as they get, for reasonable behavior
  with non-trivial amount of data precalculating string lengths and
  allocating at one go is necessary.
This commit is contained in:
Panu Matilainen 2013-02-18 10:44:40 +02:00
parent 713273858d
commit 11ba21e2ab
1 changed files with 24 additions and 4 deletions

View File

@ -213,12 +213,32 @@ int argvSplit(ARGV_t * argvp, const char * str, const char * seps)
char *argvJoin(ARGV_const_t argv, const char *sep)
{
int argc = 0;
size_t argvlen = 0;
char *dest = NULL;
char * const *arg;
for (arg = argv; arg && *arg; arg++) {
rstrscat(&dest, *arg, *(arg+1) ? sep : "", NULL);
}
if (argv) {
ARGV_const_t arg;
for (arg = argv; *arg; arg++)
argvlen += strlen(*arg);
argc = arg - argv;
}
if (argc > 0) {
size_t seplen = (sep != NULL) ? strlen(sep) : 0;
char *p;
dest = xmalloc(argvlen + (seplen * (argc - 1)) + 1);
p = stpcpy(dest, argv[0]);
for (int i = 1; i < argc; i++) {
if (seplen)
p = stpcpy(p, sep);
p = stpcpy(p, argv[i]);
}
*p = '\0';
}
return dest;
}