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

String.gsub error

Started by faubiguy, 09 October 2012 - 08:34 PM
faubiguy #1
Posted 09 October 2012 - 10:34 PM
when I attempt to call string.gsub like so:
string.gsub(a_string, "(%a+)%s*(%b())", "any string, table, or function")

It causes the error: vm error: java.lang.ArrayIndexOutOfBoundsException: N, on the line number of the string.gsub call (N is the number of characters in a_string), when a_string both starts and ends with a letter.

This happens whether the '(' and ')' in %b() are escaped or not.

I think it might be a bug, but I'm posting it here in case I'm just doing something wrong.
Lyqyd #2
Posted 10 October 2012 - 01:24 AM
Interesting. I can reproduce this. I suppose a possible workaround would be to make your own substitution function using string.gmatch or similar, to avoid this issue.

Further investigation suggests that the %bxy pattern is the problematic portion. May I ask what sort of string transformations you're using this for?
faubiguy #3
Posted 10 October 2012 - 01:35 AM
I'm matching parentheses to detect functions in my calculator program and catch nonexistent functions, as well as remove any arguments past the number the function takes.
Lyqyd #4
Posted 10 October 2012 - 01:40 AM
Ah. Well, you may have better luck going through the string a bit more slowly. It might be worth posting a bug report down in the Bugs section, though I think that if this is a bug, it's most likely a bug in LuaJ, so there wouldn't be much that dan200 or Cloudy could do about it.