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