MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / str_rep

Function str_rep

extlibs/lua/src/lstrlib.c:150–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148
149
150static int str_rep (lua_State *L) {
151 size_t l, lsep;
152 const char *s = luaL_checklstring(L, 1, &l);
153 lua_Integer n = luaL_checkinteger(L, 2);
154 const char *sep = luaL_optlstring(L, 3, "", &lsep);
155 if (n <= 0) lua_pushliteral(L, "");
156 else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
157 return luaL_error(L, "resulting string too large");
158 else {
159 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
160 luaL_Buffer b;
161 char *p = luaL_buffinitsize(L, &b, totallen);
162 while (n-- > 1) { /* first n-1 copies (followed by separator) */
163 memcpy(p, s, l * sizeof(char)); p += l;
164 if (lsep > 0) { /* empty 'memcpy' is not that cheap */
165 memcpy(p, sep, lsep * sizeof(char));
166 p += lsep;
167 }
168 }
169 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
170 luaL_pushresultsize(&b, totallen);
171 }
172 return 1;
173}
174
175
176static int str_byte (lua_State *L) {

Callers

nothing calls this directly

Calls 6

luaL_checklstringFunction · 0.85
luaL_checkintegerFunction · 0.85
luaL_optlstringFunction · 0.85
luaL_errorFunction · 0.85
luaL_buffinitsizeFunction · 0.85
luaL_pushresultsizeFunction · 0.85

Tested by

no test coverage detected