Fix bug 3495802 to return correct offset of substrings from string.find()

This commit is contained in:
James Roseborough
2013-01-27 16:14:03 +00:00
parent 8a5e811c7f
commit 1f89f30239
3 changed files with 13 additions and 3 deletions

View File

@@ -822,6 +822,7 @@ and LuaForge:
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-alpha3</b></td><td><ul>
<li>Fix bug 3597515 memory leak due to string caching by simplifying caching logic.</li>
<li>Fix bug 3565008 so that short substrings are backed by short arrays.</li>
<li>Fix bug 3495802 to return correct offset of substrings from string.find().</li>
</ul></td></tr>
</table></td></tr></table>

View File

@@ -516,9 +516,9 @@ public class LuaString extends LuaValue {
*/
public int indexOf( LuaString s, int start ) {
final int slen = s.length();
final int limit = m_offset + m_length - slen;
for ( int i = m_offset + start; i <= limit; ++i ) {
if ( equals( m_bytes, i, s.m_bytes, s.m_offset, slen ) ) {
final int limit = m_length - slen;
for ( int i = start; i <= limit; ++i ) {
if ( equals( m_bytes, m_offset + i, s.m_bytes, s.m_offset, slen ) ) {
return i;
}
}

View File

@@ -560,5 +560,14 @@ public class FragmentsTest extends TestSuite {
runFragment( LuaValue.varargsOf(LuaValue.FALSE, LuaValue.NIL),
"return pcall(error)\n");
}
public void testFindWithOffset() {
runFragment(LuaValue.varargsOf(LuaValue.valueOf(8), LuaValue.valueOf(5)),
"string = \"abcdef:ghi\"\n" +
"substring = string:sub(3)\n" +
"idx = substring:find(\":\")\n" +
"return #substring, idx\n");
}
}
}