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

Function str_unpack

3rd/lua-5.4.3/src/lstrlib.c:1697–1768  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1695
1696
1697static int str_unpack (lua_State *L) {
1698 Header h;
1699 const char *fmt = luaL_checkstring(L, 1);
1700 size_t ld;
1701 const char *data = luaL_checklstring(L, 2, &ld);
1702 size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
1703 int n = 0; /* number of results */
1704 luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
1705 initheader(L, &h);
1706 while (*fmt != '\0') {
1707 int size, ntoalign;
1708 KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
1709 luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
1710 "data string too short");
1711 pos += ntoalign; /* skip alignment */
1712 /* stack space for item + next position */
1713 luaL_checkstack(L, 2, "too many results");
1714 n++;
1715 switch (opt) {
1716 case Kint:
1717 case Kuint: {
1718 lua_Integer res = unpackint(L, data + pos, h.islittle, size,
1719 (opt == Kint));
1720 lua_pushinteger(L, res);
1721 break;
1722 }
1723 case Kfloat: {
1724 float f;
1725 copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1726 lua_pushnumber(L, (lua_Number)f);
1727 break;
1728 }
1729 case Knumber: {
1730 lua_Number f;
1731 copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1732 lua_pushnumber(L, f);
1733 break;
1734 }
1735 case Kdouble: {
1736 double f;
1737 copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1738 lua_pushnumber(L, (lua_Number)f);
1739 break;
1740 }
1741 case Kchar: {
1742 lua_pushlstring(L, data + pos, size);
1743 break;
1744 }
1745 case Kstring: {
1746 size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
1747 luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
1748 lua_pushlstring(L, data + pos + size, len);
1749 pos += len; /* skip string */
1750 break;
1751 }
1752 case Kzstr: {
1753 size_t len = strlen(data + pos);
1754 luaL_argcheck(L, pos + len < ld, 2,

Callers

nothing calls this directly

Calls 11

luaL_checklstringFunction · 0.85
posrelatIFunction · 0.85
luaL_optintegerFunction · 0.85
initheaderFunction · 0.85
getdetailsFunction · 0.85
luaL_checkstackFunction · 0.85
unpackintFunction · 0.85
lua_pushintegerFunction · 0.85
copywithendianFunction · 0.85
lua_pushnumberFunction · 0.85
lua_pushlstringFunction · 0.85

Tested by

no test coverage detected