MCPcopy Create free account
hub / github.com/DFHack/dfhack / str_rep

Function str_rep

depends/lua/src/lstrlib.c:122–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120
121
122static int str_rep (lua_State *L) {
123 size_t l, lsep;
124 const char *s = luaL_checklstring(L, 1, &l);
125 lua_Integer n = luaL_checkinteger(L, 2);
126 const char *sep = luaL_optlstring(L, 3, "", &lsep);
127 if (n <= 0) lua_pushliteral(L, "");
128 else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
129 return luaL_error(L, "resulting string too large");
130 else {
131 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
132 luaL_Buffer b;
133 char *p = luaL_buffinitsize(L, &b, totallen);
134 while (n-- > 1) { /* first n-1 copies (followed by separator) */
135 memcpy(p, s, l * sizeof(char)); p += l;
136 if (lsep > 0) { /* empty 'memcpy' is not that cheap */
137 memcpy(p, sep, lsep * sizeof(char));
138 p += lsep;
139 }
140 }
141 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
142 luaL_pushresultsize(&b, totallen);
143 }
144 return 1;
145}
146
147
148static 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