This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Tinyboss's profile picture

[1.481] string.find() pattern %b throws Java error unless matched

Started by Tinyboss, 15 January 2013 - 11:19 AM
Tinyboss #1
Posted 15 January 2013 - 12:19 PM
Say I'm trying to match something like a function call. Here's what I expect to happen (based on Lua's behavior outside Minecraft):

lua> print(string.find("abc(123)", "([%w_]+)(%b())"))
1   8   abc   (123)

lua> print(string.find("abc", "([%w_]+)(%b())"))
nil

But here's what I get in CC:

lua> print(string.find("abc(123)", "([%w_]+)(%b())"))
18abc(123)

lua> print(string.find("abc", "([%w_]+)(%b())"))
lua:1: vm error:
java.lang.ArrayIndexOutOfBoundsException

From what I can tell, using %b in your search pattern always throws that Java error unless a match is found (in which case it does what it's supposed to).
Lyqyd #2
Posted 15 January 2013 - 01:55 PM
Yep, this is one of the bugs in LuaJ's string library. The workaround is to append a match to the end of the string and discard the match if it's the one you appended. Obviously, this works better when you're ONLY looking for %b() and no other matches, since you can use the numeric returns and check whether or not they're #str + 1 and #str + 2 respectively (given str being the string to match, and the find being run against str.."()").
Tinyboss #3
Posted 16 January 2013 - 03:37 AM
That worked perfectly to fix a show-stopping bug with my replacement Lua shell. Can't have your shell crashing every time you give the wrong number of parameters to a macro.

Thanks for the workaround Lyqyd!