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

String.find() Twice Bugged?

Started by Paf, 03 February 2012 - 01:59 AM
Paf #1
Posted 03 February 2012 - 02:59 AM
Hello.

This program works fine on Lua.org's demo but doesn't in CC. http://pastebin.com/qWPSqPzb The second string.find call always returns 6 for some reason. I tested it on several times and tought there was a problem in my code, but seems not.

Sup with that?

Thanks,
Paf.
Casper7526 #2
Posted 03 February 2012 - 06:24 AM
I've noticed it too and even I don't know why it's so fucked up lol.

personally I've just resorted to using my own function

function find(text, match)
_ = 1
local _s = nil
local _e = nil
_len = string.len(match)
        while true do
        _t = string.sub(text, _ , _len + _ - 1)
                if _t == match then
                _s = _
                _e = _ + _len - 1
                break
                end
        _ = _ + 1
        if _ > string.len(text) then break end
        end
if _s == nil then return nil else return _s, _e end
end
Hawk777 #3
Posted 22 February 2012 - 12:31 AM
You can also work around this by concatenating the empty string onto the result of sub(). For example:


lua> whole = "helloworld"
lua> part = whole:sub(6)
lua> part
world
lua> part:find("d")
10
10
lua> part = part .. ""
lua> part:find("d")
5
5
lua>
Hawk777 #4
Posted 29 February 2012 - 09:18 PM
I've upstreamed this bug to the LuaJ developers here.