lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br 
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Chris Marrin
> Sent: Wednesday, July 20, 2005 6:39 PM
> To: Lua list
> Subject: Re: Concatenating strings with '+'
> 
> Ben Sunshine-Hill wrote:
> > On 7/20/05, Ignacio Burgueño <ignacio@tecnolink.com.uy> wrote:
> > 
> Adding this logic to Lua would be very easy. You would simply 
> go into lvm.c and change the Arith() function to test for 
> strings if op is TM_ADD and then jump out to luaV_concat() if 
> so. This adds a bit of logic to Arith() which slows down 
> every arithmetic operation. But, at worst, this is something like:
> 
>      if (op == TM_ADD &&
>              (ttype(rb) == LUA_TSTRING || ttype(rc) == LUA_TSTRING))
>          luaV_concat(...);
> 
> For all ops other than ADD this adds one integer compare and 
> for ADD it adds an additional one or two integer compares. 
> Not bad for what it gives you IMHO...
> 

Since I don't know the Lua internals quite well, I'm not sure if this code
is OK.
I modified the luaV_execute function in lvm.c. Do you see anything obviously
wrong in it?
Thanks in advance

case OP_ADD: {
        TObject *rb = RKB(i);
        TObject *rc = RKC(i);
        if (ttisnumber(rb) && ttisnumber(rc)) {
          setnvalue(ra, nvalue(rb) + nvalue(rc));
        }
/* agregado por mi => inicio (para que pueda concatenar strings con '+' */
		else if(ttisstring(rb) && ttisstring(rc)) {
			char *buffer;
			int iLen1 = tsvalue(rb)->tsv.len;
			int iLen2 = tsvalue(rc)->tsv.len;
			if ((iLen1 + iLen2) > MAX_SIZET) luaG_runerror(L,
"string size overflow");
			buffer = luaZ_openspace(L, &G(L)->buff, iLen1 +
iLen2);
			memcpy(buffer, svalue(rb), iLen1);
			memcpy(buffer + iLen1, svalue(rc), iLen2);
			setsvalue2s(ra, luaS_newlstr(L, buffer, iLen1 +
iLen2));
			luaC_checkGC(L);
		}
/* agregado por mi => fin */
        else
          Arith(L, ra, rb, rc, TM_ADD);
        break;
      }