MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / b_pack

Function b_pack

deps/lua/src/lua_struct.c:208–263  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

206
207
208static int b_pack (lua_State *L) {
209 luaL_Buffer b;
210 const char *fmt = luaL_checkstring(L, 1);
211 Header h;
212 int arg = 2;
213 size_t totalsize = 0;
214 defaultoptions(&h);
215 lua_pushnil(L); /* mark to separate arguments from string buffer */
216 luaL_buffinit(L, &b);
217 while (*fmt != '\0') {
218 int opt = *fmt++;
219 size_t size = optsize(L, opt, &fmt);
220 int toalign = gettoalign(totalsize, &h, opt, size);
221 totalsize += toalign;
222 while (toalign-- > 0) luaL_addchar(&b, '\0');
223 switch (opt) {
224 case 'b': case 'B': case 'h': case 'H':
225 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
226 putinteger(L, &b, arg++, h.endian, size);
227 break;
228 }
229 case 'x': {
230 luaL_addchar(&b, '\0');
231 break;
232 }
233 case 'f': {
234 float f = (float)luaL_checknumber(L, arg++);
235 correctbytes((char *)&f, size, h.endian);
236 luaL_addlstring(&b, (char *)&f, size);
237 break;
238 }
239 case 'd': {
240 double d = luaL_checknumber(L, arg++);
241 correctbytes((char *)&d, size, h.endian);
242 luaL_addlstring(&b, (char *)&d, size);
243 break;
244 }
245 case 'c': case 's': {
246 size_t l;
247 const char *s = luaL_checklstring(L, arg++, &l);
248 if (size == 0) size = l;
249 luaL_argcheck(L, l >= (size_t)size, arg, "string too short");
250 luaL_addlstring(&b, s, size);
251 if (opt == 's') {
252 luaL_addchar(&b, '\0'); /* add zero at the end */
253 size++;
254 }
255 break;
256 }
257 default: controloptions(L, opt, &fmt, &h);
258 }
259 totalsize += size;
260 }
261 luaL_pushresult(&b);
262 return 1;
263}
264
265

Callers

nothing calls this directly

Calls 11

lua_pushnilFunction · 0.85
luaL_buffinitFunction · 0.85
optsizeFunction · 0.85
gettoalignFunction · 0.85
putintegerFunction · 0.85
luaL_checknumberFunction · 0.85
correctbytesFunction · 0.85
luaL_addlstringFunction · 0.85
luaL_checklstringFunction · 0.85
controloptionsFunction · 0.85
luaL_pushresultFunction · 0.85

Tested by

no test coverage detected