MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / str_rep

Function str_rep

lib/lua/src/lstrlib.c:150–174  ·  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)
156 lua_pushliteral(L, "");
157 else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n))
158 return luaL_error(L, "resulting string too large");
159 else {
160 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
161 luaL_Buffer b;
162 char *p = luaL_buffinitsize(L, &b, totallen);
163 while (n-- > 1) { /* first n-1 copies (followed by separator) */
164 memcpy(p, s, l * sizeof(char)); p += l;
165 if (lsep > 0) { /* empty 'memcpy' is not that cheap */
166 memcpy(p, sep, lsep * sizeof(char));
167 p += lsep;
168 }
169 }
170 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
171 luaL_pushresultsize(&b, totallen);
172 }
173 return 1;
174}
175
176
177static 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