zero containing strings

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

zero containing strings

Dan Marks
I remember reading on the mailing list a while back that Lua
3.1 was possibly going to make strings non null terminated so
they could contain nulls.  

I'm writing a way to code up GPIB drivers for our lab devices
in lua, and some devices return null characters in data.  I was
contemplating writing my own block-of-bytes data structure but
since I figured it would be in the next version I'd just wait.

Will the implementation in lua still null terminate strings for
compatibility's sake?  And will there be new API commands to
get and push lua strings for the non null terminated strings?

Another thing is that I noticed that in lua when lua_getstring()
is called, it seems to make a copy of the string that is retrieved,
even if that string is in a variable that is passed as a parameter.
I tried to change the value of a string by directly writing to
the string but the changes do not take effect.  Is this true,
and if so, will there be a way to retrieve strings by reference,
so they don't have to be copied (and possibly could be modified
in place?).  Some strings would be rather long, and it would
good to avoid copying them excessively, and also it would be useful
to use a string as preallocated space to store information.

Dan Marks
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: zero containing strings

David Jeske-2
On Tue, Jun 23, 1998 at 01:46:43AM -0300, Dan Marks wrote:
> Another thing is that I noticed that in lua when lua_getstring()
> is called, it seems to make a copy of the string that is retrieved,
> even if that string is in a variable that is passed as a parameter.
> I tried to change the value of a string by directly writing to
> the string but the changes do not take effect.  Is this true,
> and if so, will there be a way to retrieve strings by reference,
> so they don't have to be copied (and possibly could be modified
> in place?).  Some strings would be rather long, and it would
> good to avoid copying them excessively, and also it would be useful
> to use a string as preallocated space to store information.

Lua has one global string table. If you modified a string in place you
wouldn't necessarily know all the places it would take effect. For
example, if you happened to modify a string which contained "draw" you
would end up changing the string lookup of any "draw" method names
contained in tables.  (Someone please validate that this is correct)

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: zero containing strings

Luiz Henrique de Figueiredo
In reply to this post by Dan Marks
>From [hidden email] Tue Jun 23 01:46:43 1998
>
>I remember reading on the mailing list a while back that Lua
>3.1 was possibly going to make strings non null terminated so
>they could contain nulls.  
>
>I'm writing a way to code up GPIB drivers for our lab devices
>in lua, and some devices return null characters in data.  I was
>contemplating writing my own block-of-bytes data structure but
>since I figured it would be in the next version I'd just wait.

yes, please wait. 3.1 is going to be out real-soon-now.

>Will the implementation in lua still null terminate strings for
>compatibility's sake?  And will there be new API commands to
>get and push lua strings for the non null terminated strings?

yes and yes.

>Another thing is that I noticed that in lua when lua_getstring()
>is called, it seems to make a copy of the string that is retrieved,
>even if that string is in a variable that is passed as a parameter.
>I tried to change the value of a string by directly writing to
>the string but the changes do not take effect.  Is this true,
>and if so,

all strings sent to Lua are duplicated inside Lua.
so, there's not way to change a string, once it's in Lua.

>will there be a way to retrieve strings by reference,
>so they don't have to be copied (and possibly could be modified
>in place?).  Some strings would be rather long, and it would
>good to avoid copying them excessively, and also it would be useful
>to use a string as preallocated space to store information.

you can always store the pointer as userdata, but you won't be able to
use it as string without duplicating the string in Lua.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: zero containing strings

Luiz Henrique de Figueiredo
>From [hidden email] Tue Jun 23 03:01:38 1998
>
>Lua has one global string table. If you modified a string in place you
>wouldn't necessarily know all the places it would take effect. For
>example, if you happened to modify a string which contained "draw" you
>would end up changing the string lookup of any "draw" method names
>contained in tables.  (Someone please validate that this is correct)

right. Lua stores strings in hash tables. If you change the string in place
after it's there, then hashing stops working correctly.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: zero containing strings

Norman Ramsey-3
In reply to this post by Luiz Henrique de Figueiredo
 > >will there be a way to retrieve strings by reference,
 > >so they don't have to be copied (and possibly could be modified
 > >in place?).  Some strings would be rather long, and it would
 > >good to avoid copying them excessively, and also it would be useful
 > >to use a string as preallocated space to store information.
 > 
 > you can always store the pointer as userdata, but you won't be able to
 > use it as string without duplicating the string in Lua.

I'm saddened by this.  My code has two copies of every `unique'
string: one for C code and one for Lua code.  I use the C interface
because it's extremely fast, and I can't find an obvious way to use
the Lua unique-string table from C.  (I don't want these strings
garbage-collected.)  I'm probably missing something.  What?

Norman

Reply | Threaded
Open this post in threaded view
|

Re: zero containing strings

David Jeske-2
On Tue, Jun 23, 1998 at 02:15:49PM -0300, Norman Ramsey wrote:
>  > you can always store the pointer as userdata, but you won't be able to
>  > use it as string without duplicating the string in Lua.
> 
> I'm saddened by this.  My code has two copies of every `unique'
> string: one for C code and one for Lua code.  I use the C interface
> because it's extremely fast, and I can't find an obvious way to use
> the Lua unique-string table from C.  (I don't want these strings
> garbage-collected.)  I'm probably missing something.  What?

Yeah, you are missing something. Just do this:

| lua_pushstring(my_string_pointer);
| my_string_ref = lua_ref(1);

Then you will have a handle to the unique string inside Lua. The "1"
argument to lua_ref tells it to lock the string so lua won't GC it.

I do this all the time because this:

| lua_pushobject(a_table);
| lua_pushobject(lua_getref(my_string_ref));
| foo = lua_gettable();

Is faster than this:

| lua_pushobject(a_table);
| lua_pushstring(my_string_pointer);
| foo = lua_gettable();

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]