MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / str_pack

Function str_pack

third-party/lua-5.5.0/src/lstrlib.c:1611–1714  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1609
1610
1611static int str_pack (lua_State *L) {
1612 luaL_Buffer b;
1613 Header h;
1614 const char *fmt = luaL_checkstring(L, 1); /* format string */
1615 int arg = 1; /* current argument to pack */
1616 size_t totalsize = 0; /* accumulate total size of result */
1617 initheader(L, &h);
1618 lua_pushnil(L); /* mark to separate arguments from string buffer */
1619 luaL_buffinit(L, &b);
1620 while (*fmt != '\0') {
1621 unsigned ntoalign;
1622 size_t size;
1623 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1624 luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg,
1625 "result too long");
1626 totalsize += ntoalign + size;
1627 while (ntoalign-- > 0)
1628 luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
1629 arg++;
1630 switch (opt) {
1631 case Kint: { /* signed integers */
1632 lua_Integer n = luaL_checkinteger(L, arg);
1633 if (size < SZINT) { /* need overflow check? */
1634 lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
1635 luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
1636 }
1637 packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0));
1638 break;
1639 }
1640 case Kuint: { /* unsigned integers */
1641 lua_Integer n = luaL_checkinteger(L, arg);
1642 if (size < SZINT) /* need overflow check? */
1643 luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
1644 arg, "unsigned overflow");
1645 packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0);
1646 break;
1647 }
1648 case Kfloat: { /* C float */
1649 float f = (float)luaL_checknumber(L, arg); /* get argument */
1650 char *buff = luaL_prepbuffsize(&b, sizeof(f));
1651 /* move 'f' to final result, correcting endianness if needed */
1652 copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
1653 luaL_addsize(&b, size);
1654 break;
1655 }
1656 case Knumber: { /* Lua float */
1657 lua_Number f = luaL_checknumber(L, arg); /* get argument */
1658 char *buff = luaL_prepbuffsize(&b, sizeof(f));
1659 /* move 'f' to final result, correcting endianness if needed */
1660 copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
1661 luaL_addsize(&b, size);
1662 break;
1663 }
1664 case Kdouble: { /* C double */
1665 double f = (double)luaL_checknumber(L, arg); /* get argument */
1666 char *buff = luaL_prepbuffsize(&b, sizeof(f));
1667 /* move 'f' to final result, correcting endianness if needed */
1668 copywithendian(buff, (char *)&f, sizeof(f), h.islittle);

Callers

nothing calls this directly

Calls 12

initheaderFunction · 0.70
lua_pushnilFunction · 0.70
luaL_buffinitFunction · 0.70
getdetailsFunction · 0.70
luaL_checkintegerFunction · 0.70
packintFunction · 0.70
luaL_checknumberFunction · 0.70
luaL_prepbuffsizeFunction · 0.70
copywithendianFunction · 0.70
luaL_checklstringFunction · 0.70
luaL_addlstringFunction · 0.70
luaL_pushresultFunction · 0.70

Tested by

no test coverage detected