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

Java Error

Started by HPWebcamAble, 10 April 2015 - 11:07 PM
HPWebcamAble #1
Posted 11 April 2015 - 01:07 AM
Is this a bug or just me?

Trying to find "[" in a string with string.find errors?


string.find( "hi" , "[" ) --#> vm error: java.lang.ArrayIndexOutOfBoundsException

string.find( "[" , "[" ) --#> vm error: java.lang.ArrayIndexOutOfBoundsException

string.find( "hi" , "]" ) --#> nil

Happens in both CCEmuRedux (CC 1.6) and Full CC 1.7
Edited on 10 April 2015 - 11:07 PM
Dragon53535 #2
Posted 11 April 2015 - 01:09 AM
I believe your problem stems from [ being a "magic character" for patterns, try doing

string.find("hi","%[")
HPWebcamAble #3
Posted 11 April 2015 - 01:13 AM
I believe your problem stems from [ being a "magic character" for patterns, try doing

string.find("hi","%[")

Yup, that fixed it

Would it be considered a bug that it errors?
Dragon53535 #4
Posted 11 April 2015 - 01:16 AM
I believe your problem stems from [ being a "magic character" for patterns, try doing

string.find("hi","%[")

Yup, that fixed it

Would it be considered a bug that it errors?

Probably not, considering that [ denotes a character set. Since you didn't close it with ], it errored in a way that it knows, someone with more experience than me would know more and give a better answer. The % counts as an escape character
Edited on 10 April 2015 - 11:16 PM
Lupus590 #5
Posted 11 April 2015 - 01:18 AM
no as it's a special character, it has special meaning in the string.find function, so if you want to use [ as you would normally then you have to preface it with % to tell the function that it should treat the [ as a [ and not as the special character that it usually does

it's almost the opposite of \n in print/write, where in print the \ says the next character will have a special meaning the % in string.find tells it " this next character normally has special meaning, don't treat it as special in this case"
Edited on 10 April 2015 - 11:19 PM
HPWebcamAble #6
Posted 11 April 2015 - 01:38 AM
Well by error, I mean a Java VM error, I'd be fine with a Lua error.
InDieTasten #7
Posted 11 April 2015 - 04:30 AM
Well by error, I mean a Java VM error, I'd be fine with a Lua error.
Well, I guess you can't do anything about it. Dan or Cloudy won't fix this issue anyways, since it's in the lua libraries deep inside.
I haven't tested this in the official c++ implementation, but I could imagine, that there would be a normal lua exception. I think it's luas implementation for java, that this comes from.
Maybe if it does even throw a lower level exception in the host software, error checking pattern recognition is fairly complicated and would destroy the leightweight'ness of lua.
Standard POSIX RegEx has as many code lines as half of all Lua standard libraries together