| 106 | #define MAXSIZE ((~(size_t)0) >> 1) |
| 107 | |
| 108 | static int str_rep (lua_State *L) { |
| 109 | size_t l, lsep; |
| 110 | const char *s = luaL_checklstring(L, 1, &l); |
| 111 | int n = luaL_checkint(L, 2); |
| 112 | const char *sep = luaL_optlstring(L, 3, "", &lsep); |
| 113 | if (n <= 0) lua_pushliteral(L, ""); |
| 114 | else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ |
| 115 | return luaL_error(L, "resulting string too large"); |
| 116 | else { |
| 117 | size_t totallen = n * l + (n - 1) * lsep; |
| 118 | luaL_Buffer b; |
| 119 | char *p = luaL_buffinitsize(L, &b, totallen); |
| 120 | while (n-- > 1) { /* first n-1 copies (followed by separator) */ |
| 121 | memcpy(p, s, l * sizeof(char)); p += l; |
| 122 | if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ |
| 123 | memcpy(p, sep, lsep * sizeof(char)); p += lsep; |
| 124 | } |
| 125 | } |
| 126 | memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ |
| 127 | luaL_pushresultsize(&b, totallen); |
| 128 | } |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | static int str_byte (lua_State *L) { |
nothing calls this directly
no test coverage detected