Merge "ustring: Handle empty charset like Lua does"

This commit is contained in:
jenkins-bot 2015-04-16 00:48:57 +00:00 committed by Gerrit Code Review
commit 5a74dd4a50
3 changed files with 16 additions and 0 deletions

View file

@ -463,6 +463,10 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
$re .= preg_quote( $pat[$i], '/' );
}
}
if ( $re === '[' || $re === '[^' ) {
// This is the error Lua string functions throws in this situation
throw new Scribunto_LuaError( "malformed pattern (missing ']')" );
}
if ( $i >= $len ) {
throw new Scribunto_LuaError( "Missing close-bracket for character set beginning at pattern character $ii" );
}

View file

@ -567,6 +567,10 @@ local function find( s, cps, rawpat, pattern, init, noAnchor )
epp = ep + 1
until S.byte( rawpat, ep - 1 ) ~= 0x25 or S.byte( rawpat, ep - 2 ) == 0x25
local key = S.sub( rawpat, pattern.bytepos[pp], ep )
if key == '[]' or key == '[^]' then
-- This is the error Lua string functions throws in this situation
error( "malformed pattern (missing ']')" )
end
if charset_cache[key] then
local pl, cs = unpack( charset_cache[key] )
return pp + pl, cs

View file

@ -429,6 +429,14 @@ return testframework.getTestProvider( {
args = { "fóó", '([^a-z])' },
expect = { 2, 2, 'ó' }
},
{ name = 'find: empty character set', func = mw.ustring.find,
args = { "fóó", '[]' },
expect = "malformed pattern (missing ']')"
},
{ name = 'find: empty negated character set', func = mw.ustring.find,
args = { "fóó", '[^]' },
expect = "malformed pattern (missing ']')"
},
{ name = 'match: (1)', func = mw.ustring.match,
args = { "bar fóo bar", 'f%a+' },