Refactor string.rep().

This commit is contained in:
Mike Pall
2013-04-26 19:57:25 +02:00
parent a98e6a70c1
commit 9b8db403f2
8 changed files with 39 additions and 184 deletions

View File

@@ -144,6 +144,30 @@ SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s)
return sb;
}
SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep)
{
MSize len = s->len;
if (rep > 0 && len) {
uint64_t tlen = (uint64_t)rep * len;
char *p;
if (LJ_UNLIKELY(tlen > LJ_MAX_STR))
lj_err_mem(sbufL(sb));
p = lj_buf_more(sb, (MSize)tlen);
if (len == 1) { /* Optimize a common case. */
uint32_t c = strdata(s)[0];
do { *p++ = c; } while (--rep > 0);
} else {
const char *e = strdata(s) + len;
do {
const char *q = strdata(s);
do { *p++ = *q++; } while (q < e);
} while (--rep > 0);
}
setsbufP(sb, p);
}
return sb;
}
GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb)
{
return lj_str_new(sbufL(sb), sbufB(sb), sbuflen(sb));