[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Concatenating strings with '+'
- From: Chris Marrin <chris@...>
- Date: Wed, 20 Jul 2005 18:10:47 -0700
Ignacio Burgueño wrote:
-----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;
}
Let me first say that I am pretty new to Lua. There are many who are
much more experienced than me, and better suited to answer your
question. But I have special needs of the engine like you, so I have
been in this section of the code. It seems like you could have used
luaV_concat() to do this rather than recoding it in-place.
--
chris marrin ,""$,
cmarrin@arch.sel.sony.com b` $ ,,.
(408) 955-3049 mP b' , 1$'
Sony ,.` ,b` ,` :$$'
,|` mP ,` ,mm
,b" b" ,` ,mm m$$ ,m ,`P$$
m$` ,b` .` ,mm ,'|$P ,|"1$` ,b$P ,` :$1
b$` ,$: :,`` |$$ ,` $$` ,|` ,$$,,`"$$ .` :$|
b$| _m$`,:` :$1 ,` ,$Pm|` ` :$$,..;"' |$:
P$b, _;b$$b$1" |$$ ,` ,$$" ``' $$
```"```'" `"` `""` ""` ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'