diff options
-rw-r--r-- | liblali.c | 23 |
1 files changed, 13 insertions, 10 deletions
@@ -1040,27 +1040,30 @@ Object *primitiveJoin(Object **args, GC_PARAM) { else if ((*args)->car->type != TYPE_CONS) exceptionWithObject((*args)->car, "is not a list"); - unsigned long i = 1; Object *tmpCons = (*args)->car; + unsigned long i = strlen(tmpCons->car->string); for (; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) - i += strlen(tmpCons->car->string); + i += strlen(tmpCons->cdr->car->string); - char symbolResult[i]; + char symbolResult[i+1]; tmpCons = (*args)->car; unsigned long wordLength = strlen(tmpCons->car->string); + unsigned long j = 0; // first list element - for (int j = 0; j < wordLength; j++) + for (; j < wordLength; j++) symbolResult[j] = tmpCons->car->string[j]; // rest of list elements - for (i = wordLength; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) { - wordLength = strlen(tmpCons->cdr->car->string); - for (int j = 0; j < wordLength; j++) - symbolResult[i+j] = tmpCons->cdr->car->string[j]; - i += wordLength; + if (tmpCons->cdr->car != nil) { + for (i = wordLength; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) { + wordLength = strlen(tmpCons->cdr->car->string); + for (j = 0; j < wordLength; j++) + symbolResult[i+j] = tmpCons->cdr->car->string[j]; + i += j; + } } - + symbolResult[i] = '\0'; return newSymbol(symbolResult, GC_ROOTS); } |