Skip to content

Commit

Permalink
Added rope resolution cache; adjusted string.rep
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Jan 10, 2022
1 parent 521ba23 commit f2f69e6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/lgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ static void traverserope (global_State *g, TRope *r) {
if (r->tsr.right->tsr.tt == LUA_TSTRING) stringmark(cast(TString *, r->tsr.right));
else markobject(g, r->tsr.right);
}
if (r->tsr.res) stringmark(r->tsr.res);
}


Expand Down
1 change: 1 addition & 0 deletions src/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ typedef union TRope {
union TRope * left;
union TRope * right;
size_t len;
TString *res;
} tsr;
} TRope;

Expand Down
3 changes: 3 additions & 0 deletions src/lstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ TRope *luaS_concat (lua_State *L, TRope *l, TRope *r) {
rope->tsr.right = r;
rope->tsr.len = (l->tsr.tt == LUA_TSTRING ? cast(TString *, l)->tsv.len : (l->tsr.tt == LUA_TSUBSTR ? cast(TSubString *, l)->tss.len : l->tsr.len)) +
(r->tsr.tt == LUA_TSTRING ? cast(TString *, r)->tsv.len : (r->tsr.tt == LUA_TSUBSTR ? cast(TSubString *, r)->tss.len : r->tsr.len));
rope->tsr.res = NULL;
return rope;
}

Expand All @@ -125,6 +126,7 @@ TString *luaS_build (lua_State *L, TRope *rope) {
TString *s;
TRope **stack;
if (rope->tsr.tt == LUA_TSTRING || rope->tsr.tt == LUA_TSUBSTR) return cast(TString *, rope);
if (rope->tsr.res) return rope->tsr.res;
buffer = cur = luaZ_openspace(L, &G(L)->buff, rope->tsr.len);
stack = G(L)->ropestack;
do {
Expand Down Expand Up @@ -161,6 +163,7 @@ TString *luaS_build (lua_State *L, TRope *rope) {
rope = rope->tsr.right;
} while (stack >= G(L)->ropestack);
s = luaS_newlstr(L, buffer, cur - buffer);
rope->tsr.res = s;
return s;
}

Expand Down
17 changes: 11 additions & 6 deletions src/lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,18 @@ static int str_upper (lua_State *L) {

static int str_rep (lua_State *L) {
size_t l;
luaL_Buffer b;
char * str;
void * ud;
const char *s = luaL_checklstring(L, 1, &l);
int n = luaL_checkint(L, 2);
luaL_buffinit(L, &b);
while (n-- > 0)
luaL_addlstring(&b, s, l);
luaL_pushresult(&b);
int n = luaL_checkint(L, 2), i;
if (n == 0) lua_pushliteral(L, "");
else if (n == 1) lua_pushvalue(L, 1);
else {
str = lua_getallocf(L, &ud)(ud, NULL, 0, l * n);
for (i = 0; i < n; i++) memcpy(str + (i * l), s, l);
lua_pushlstring(L, str, l * n);
lua_getallocf(L, &ud)(ud, str, l * n, 0);
}
return 1;
}

Expand Down

0 comments on commit f2f69e6

Please sign in to comment.