MCPcopy Create free account
hub / github.com/F-Stack/f-stack / str_rep

Function str_rep

freebsd/contrib/openzfs/module/lua/lstrlib.c:118–140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116#define MAXSIZE ((~(size_t)0) >> 1)
117
118static int str_rep (lua_State *L) {
119 size_t l, lsep;
120 const char *s = luaL_checklstring(L, 1, &l);
121 int n = luaL_checkint(L, 2);
122 const char *sep = luaL_optlstring(L, 3, "", &lsep);
123 if (n <= 0) lua_pushliteral(L, "");
124 else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
125 return luaL_error(L, "resulting string too large");
126 else {
127 size_t totallen = n * l + (n - 1) * lsep;
128 luaL_Buffer b;
129 char *p = luaL_buffinitsize(L, &b, totallen);
130 while (n-- > 1) { /* first n-1 copies (followed by separator) */
131 memcpy(p, s, l * sizeof(char)); p += l;
132 if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
133 memcpy(p, sep, lsep * sizeof(char)); p += lsep;
134 }
135 }
136 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
137 luaL_pushresultsize(&b, totallen);
138 }
139 return 1;
140}
141
142
143static int str_byte (lua_State *L) {

Callers

nothing calls this directly

Calls 6

luaL_buffinitsizeFunction · 0.85
luaL_pushresultsizeFunction · 0.85
luaL_checklstringFunction · 0.70
luaL_optlstringFunction · 0.70
luaL_errorFunction · 0.70
memcpyFunction · 0.50

Tested by

no test coverage detected