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