Fix balanced match for empty string (fixes issue #23)

This commit is contained in:
James Roseborough
2014-03-09 15:33:44 +00:00
parent f17e9d7018
commit 7b97573ac8
4 changed files with 49 additions and 10 deletions

View File

@@ -387,6 +387,12 @@ public class LuaString extends LuaValue {
return this;
}
/** Take a substring using Java zero-based indexes for begin and end or range.
* @param beginIndex The zero-based index of the first character to include.
* @param endIndex The zero-based index of position after the last character.
* @return LuaString which is a substring whose first character is at offset
* beginIndex and extending for (endIndex - beginIndex ) characters.
*/
public LuaString substring( int beginIndex, int endIndex ) {
return valueOf( m_bytes, m_offset + beginIndex, endIndex - beginIndex );
}

View File

@@ -1155,18 +1155,19 @@ public class StringLib extends TwoArgFunction {
if ( poff == plen || poff + 1 == plen ) {
error( "unbalanced pattern" );
}
if ( s.luaByte( soff ) != p.luaByte( poff ) )
final int slen = s.length();
if ( soff >= slen )
return -1;
else {
int b = p.luaByte( poff );
int e = p.luaByte( poff + 1 );
int cont = 1;
while ( ++soff < s.length() ) {
if ( s.luaByte( soff ) == e ) {
if ( --cont == 0 ) return soff + 1;
}
else if ( s.luaByte( soff ) == b ) cont++;
final int b = p.luaByte( poff );
if ( s.luaByte( soff ) != b )
return -1;
final int e = p.luaByte( poff + 1 );
int cont = 1;
while ( ++soff < slen ) {
if ( s.luaByte( soff ) == e ) {
if ( --cont == 0 ) return soff + 1;
}
else if ( s.luaByte( soff ) == b ) cont++;
}
return -1;
}