** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top - total' up to 'L->top - 1'. */
| 472 | ** from 'L->top - total' up to 'L->top - 1'. |
| 473 | */ |
| 474 | void luaV_concat (lua_State *L, int total) { |
| 475 | lua_assert(total >= 2); |
| 476 | do { |
| 477 | StkId top = L->top; |
| 478 | int n = 2; /* number of elements handled in this pass (at least 2) */ |
| 479 | if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) |
| 480 | luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); |
| 481 | else if (isemptystr(top - 1)) /* second operand is empty? */ |
| 482 | cast_void(tostring(L, top - 2)); /* result is first operand */ |
| 483 | else if (isemptystr(top - 2)) { /* first operand is an empty string? */ |
| 484 | setobjs2s(L, top - 2, top - 1); /* result is second op. */ |
| 485 | } |
| 486 | else { |
| 487 | /* at least two non-empty string values; get as many as possible */ |
| 488 | size_t tl = vslen(top - 1); |
| 489 | TString *ts; |
| 490 | /* collect total length and number of strings */ |
| 491 | for (n = 1; n < total && tostring(L, top - n - 1); n++) { |
| 492 | size_t l = vslen(top - n - 1); |
| 493 | if (l >= (MAX_SIZE/sizeof(char)) - tl) |
| 494 | luaG_runerror(L, "string length overflow"); |
| 495 | tl += l; |
| 496 | } |
| 497 | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ |
| 498 | char buff[LUAI_MAXSHORTLEN]; |
| 499 | copy2buff(top, n, buff); /* copy strings to buffer */ |
| 500 | ts = luaS_newlstr(L, buff, tl); |
| 501 | } |
| 502 | else { /* long string; copy strings directly to final result */ |
| 503 | ts = luaS_createlngstrobj(L, tl); |
| 504 | copy2buff(top, n, getstr(ts)); |
| 505 | } |
| 506 | setsvalue2s(L, top - n, ts); /* create result */ |
| 507 | } |
| 508 | total -= n-1; /* got 'n' strings to create 1 new */ |
| 509 | L->top -= n-1; /* popped 'n' strings and pushed one */ |
| 510 | } while (total > 1); /* repeat until only 1 result left */ |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* |
no test coverage detected