| 148 | |
| 149 | |
| 150 | static 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 | |
| 176 | static int str_byte (lua_State *L) { |
nothing calls this directly
no test coverage detected