pygy / strung.lua

Lua string patterns rewritten in Lua + FFI, for LuaJIT.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

index errors in charclass

CapsAdmin opened this issue · comments

Sorry for not diving too much into the code to figure out why but on line 336 if C[func](i) ~= 0 then there is an error that can occur if func is one of the following letters:
s,
u,
z,
l,
a,

simply checking if func exists seems to work but I don't know if this is correct or not.
if func C[func](i) then

Thanks for the report, I'll have a look at it tomorrow.

FYI, if you want GitHub not to parse code as markdown (as it occurred in your first post), wrap it in backticks. [func](i) is otherwise interpreted as a hyperlink.

Hi again CapsAdmin!

I can't reproduce anything, and I'm not sure I understand what the problem is.

Could you give me a concrete test case? How does it differ from the standard Lua patterns?

C[func](i) ~= 0 actually calls C.isalpha(i) and friends, which return boolean-like integers (they are declared just above in a ffi.cdef block).

C[func](i) is thus always truthy. Removing the ~= 0 comparison makes the character classes represented by a lower case letter accept all characters, and their counterparts represented by uppercase letters reject all characters.

What more, %z should not be affected, it doesn't rely on that code.

Something tells me that the problem lies elsewhere.

If you want to experiment, you can have a look at the test.lua file. It provides functions to run strung and string side by side for the same input. It produces an error if their output is different.

try("find", ...) -- runs both string.find(...) and strung.find(...) and compares the output. 
                 -- You can also try it with "match"
gmtry(...)       -- the same for gmatch.

Sorry for the long wait!

It's caused by %z.

strung.gmatch("", "[%z]")

.\strung.lua:336: bad argument #2 to '__index' (string expected, got nil)

this was tested on latest luajit 2.1

Ok, indeed, there was a bug, now fixed in the master branch.

Thanks for the report.