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

[1.3] string.find after string.sub

Started by shcipwa, 01 September 2012 - 11:27 AM
shcipwa #1
Posted 01 September 2012 - 01:27 PM
ComputerCraft Version Information: 1.3 Client / 1.3 Server

Description of Bug:
If you call stirng.find on a string that has been returned by string.sub, instead of the index of the match in the newly created substring, you get the index in the original string.

I came across this problem when trying to substring command line arguments, I assume these are partitioned by substring of the full shell string as string.find returns the position in the shell string instead of the argument.

Steps to Reproduce Bug:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);

local indexOfSample = string.find(testSubString,"sample");
print(indexOfSample);

Returns 11 instead of the expected 7

Workaround:
Concatenate a blank string to cause memory re-allocation


local testString = "This is a sample string";
local testSubString = string.sub(testString,5);

local indexOfSample = string.find(testSubString.."","sample");
print(indexOfSample);
Cloudy #2
Posted 01 September 2012 - 01:34 PM
Yep - this is a bug in LuaJ. We haven't had time to work round it - however, as you said appending a blank string fixes it. We may be moving away from LuaJ in the future anyway, so this isn't a priority right now.