lua-users home
lua-l archive

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


Yes, it's release 13 of my C libraries!

My regular expression C library for Lua 4 & 5, rex, has long advertised
support for pcre (Perl-Compatible Regular Expressions), via pcre's POSIX
API. I had never actually tried it, but when I did, I came across a
problem: since pcre shadows the existing POSIX functions, it's hard to
make it work when the rex library is dynamically loaded (with loadlib):
the dynamic linking machinery tends to revert to using glibc's functions,
which means that nothing works (as the rex library was compiled against
one set of functions, and then linked against another).

Of course, this could be cured by building the rex library into
liblua.{so,a}, but one of the things I like about Lua 5 is precisely that
I don't have to rebuild it to add my libraries.

Also, it occurred to me that it would be nice to have access to both POSIX
and Perl-style regexs.

So, I rewrote my rex library, using pcre's native API and the POSIX API
side by side. Fortunately, this was easy as pcre's own API is quite
similar to the POSIX API.

The interface in Lua changes only slightly: instead of the single
constructor rex.comp, there are now two: rex.newPOSIX and rex.newPCRE.
Once you have built a regular expression object, you can use the match and
gmatch methods as before, without worrying about whether it's a POSIX or
pcre regex.

The library can optionally be built with just pcre or just POSIX support
(it will complain if you try to build with neither!).

If anyone's interested, it would be easy to add a third constructor,
rex.newPCREstudy, that uses pcre's pcre_study call to improve performance
on heavily-used regexs. The other thing missing is a fast string.gsub
equivalent, with the string construction in C, and I may add that soon,
but it's a bit harder.

As an aside, I've found and fixed a couple of errors in the way the
library is opened, by more careful reading of Lua 5's standard C
libraries. These would have been unlikely to cause any problems in normal
use (especially via the lua standalone, which discards results from
opening a library). These fixes have also been applied to the bit library.

As ever, downloads from

http://www.mupsych.org/~rrt/Lua

-- 
http://www.mupsych.org/~rrt/ | wisdom, n.  knowing when to be foolish