MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / str_rep

Function str_rep

third-party/lua-5.5.0/src/lstrlib.c:139–163  ·  view source on GitHub ↗

** MAX_SIZE is limited both by size_t and lua_Integer. ** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer. */

Source from the content-addressed store, hash-verified

137** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer.
138*/
139static int str_rep (lua_State *L) {
140 size_t len, lsep;
141 const char *s = luaL_checklstring(L, 1, &len);
142 lua_Integer n = luaL_checkinteger(L, 2);
143 const char *sep = luaL_optlstring(L, 3, "", &lsep);
144 if (n <= 0)
145 lua_pushliteral(L, "");
146 else if (l_unlikely(len > MAX_SIZE - lsep ||
147 cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n))
148 return luaL_error(L, "resulting string too large");
149 else {
150 size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep;
151 luaL_Buffer b;
152 char *p = luaL_buffinitsize(L, &b, totallen);
153 while (n-- > 1) { /* first n-1 copies (followed by separator) */
154 memcpy(p, s, len * sizeof(char)); p += len;
155 if (lsep > 0) { /* empty 'memcpy' is not that cheap */
156 memcpy(p, sep, lsep * sizeof(char)); p += lsep;
157 }
158 }
159 memcpy(p, s, len * sizeof(char)); /* last copy without separator */
160 luaL_pushresultsize(&b, totallen);
161 }
162 return 1;
163}
164
165
166static int str_byte (lua_State *L) {

Callers

nothing calls this directly

Calls 6

luaL_checklstringFunction · 0.70
luaL_checkintegerFunction · 0.70
luaL_optlstringFunction · 0.70
luaL_errorFunction · 0.70
luaL_buffinitsizeFunction · 0.70
luaL_pushresultsizeFunction · 0.70

Tested by

no test coverage detected