MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / luaV_concat

Function luaV_concat

3rd/lua-5.4.3/src/lvm.c:638–677  ·  view source on GitHub ↗

** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top - total' up to 'L->top - 1'. */

Source from the content-addressed store, hash-verified

636** from 'L->top - total' up to 'L->top - 1'.
637*/
638void luaV_concat (lua_State *L, int total) {
639 if (total == 1)
640 return; /* "all" values already concatenated */
641 do {
642 StkId top = L->top;
643 int n = 2; /* number of elements handled in this pass (at least 2) */
644 if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
645 !tostring(L, s2v(top - 1)))
646 luaT_tryconcatTM(L);
647 else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
648 cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
649 else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
650 setobjs2s(L, top - 2, top - 1); /* result is second op. */
651 }
652 else {
653 /* at least two non-empty string values; get as many as possible */
654 size_t tl = vslen(s2v(top - 1));
655 TString *ts;
656 /* collect total length and number of strings */
657 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
658 size_t l = vslen(s2v(top - n - 1));
659 if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl))
660 luaG_runerror(L, "string length overflow");
661 tl += l;
662 }
663 if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
664 char buff[LUAI_MAXSHORTLEN];
665 copy2buff(top, n, buff); /* copy strings to buffer */
666 ts = luaS_newlstr(L, buff, tl);
667 }
668 else { /* long string; copy strings directly to final result */
669 ts = luaS_createlngstrobj(L, tl);
670 copy2buff(top, n, getstr(ts));
671 }
672 setsvalue2s(L, top - n, ts); /* create result */
673 }
674 total -= n-1; /* got 'n' strings to create 1 new */
675 L->top -= n-1; /* popped 'n' strings and pushed one */
676 } while (total > 1); /* repeat until only 1 result left */
677}
678
679
680/*

Callers 4

luaV_finishOpFunction · 0.85
luaV_executeFunction · 0.85
pushstrFunction · 0.85
lua_concatFunction · 0.85

Calls 5

luaT_tryconcatTMFunction · 0.85
luaG_runerrorFunction · 0.85
copy2buffFunction · 0.85
luaS_newlstrFunction · 0.85
luaS_createlngstrobjFunction · 0.85

Tested by

no test coverage detected