mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-27 17:50:06 +00:00
Merge "Fix mw.ustring.gmatch and patterns with '^'"
This commit is contained in:
commit
2980cf4c11
|
@ -232,7 +232,7 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
|||
}
|
||||
|
||||
/* Convert a Lua pattern into a PCRE regex */
|
||||
private function patternToRegex( $pattern ) {
|
||||
private function patternToRegex( $pattern, $noAnchor = false ) {
|
||||
$pat = preg_split( '//us', $pattern, null, PREG_SPLIT_NO_EMPTY );
|
||||
|
||||
static $charsets = null, $brcharsets = null;
|
||||
|
@ -295,7 +295,7 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
|||
switch ( $pat[$i] ) {
|
||||
case '^':
|
||||
$q = $i;
|
||||
$re .= $q ? '\\^' : '^';
|
||||
$re .= ( $noAnchor || $q ) ? '\\^' : '^';
|
||||
break;
|
||||
|
||||
case '$':
|
||||
|
@ -497,7 +497,7 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
|||
$this->checkString( 'gmatch', $s );
|
||||
$this->checkPattern( 'gmatch', $pattern );
|
||||
|
||||
list( $re, $capt ) = $this->patternToRegex( $pattern );
|
||||
list( $re, $capt ) = $this->patternToRegex( $pattern, true );
|
||||
return array( $re, $capt );
|
||||
}
|
||||
|
||||
|
|
|
@ -14,10 +14,6 @@ local function php_gmatch( s, pattern )
|
|||
checkType( 'gmatch', 1, s, 'string' )
|
||||
checkType( 'gmatch', 2, pattern, 'string' )
|
||||
|
||||
if string.sub( pattern, 1, 1 ) == '^' then
|
||||
return function() return nil end, nil, nil
|
||||
end
|
||||
|
||||
local re, capt = gmatch_init( s, pattern )
|
||||
local pos = 0
|
||||
return function()
|
||||
|
|
|
@ -397,10 +397,11 @@ setmetatable( charset_cache, { __weak = 'kv' } )
|
|||
-- @param rawpat string Pattern
|
||||
-- @param pattern table Exploded pattern
|
||||
-- @param init int Starting index
|
||||
-- @param noAnchor boolean True to ignore '^'
|
||||
-- @return int starting index of the match
|
||||
-- @return int ending index of the match
|
||||
-- @return string|int* captures
|
||||
local function find( s, cps, rawpat, pattern, init )
|
||||
local function find( s, cps, rawpat, pattern, init, noAnchor )
|
||||
local charsets = require 'ustring/charsets'
|
||||
local anchor = false
|
||||
local ncapt, captures
|
||||
|
@ -676,7 +677,7 @@ local function find( s, cps, rawpat, pattern, init )
|
|||
-- match.
|
||||
local sp = init
|
||||
local pp = 1
|
||||
if pattern.codepoints[1] == 0x5e then -- '^': Pattern is anchored
|
||||
if not noAnchor and pattern.codepoints[1] == 0x5e then -- '^': Pattern is anchored
|
||||
anchor = true
|
||||
pp = 2
|
||||
end
|
||||
|
@ -838,15 +839,8 @@ function ustring.gmatch( s, pattern )
|
|||
end
|
||||
local init = 1
|
||||
|
||||
if pat.codepoints[1] == 0x5e then -- '^': Pattern is anchored
|
||||
-- Lua special-cases this to never match
|
||||
return function ()
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
return function ()
|
||||
local m = { find( s, cps, pattern, pat, init ) }
|
||||
local m = { find( s, cps, pattern, pat, init, true ) }
|
||||
if not m[1] then
|
||||
return nil
|
||||
end
|
||||
|
|
|
@ -396,8 +396,8 @@ return testframework.getTestProvider( {
|
|||
type = 'Iterator'
|
||||
},
|
||||
{ name = 'gmatch: anchored', func = mw.ustring.gmatch,
|
||||
args = { str2, '^f%a+' },
|
||||
expect = {},
|
||||
args = { "fóó1 ^fóó2 fóó3 ^fóó4", '^fóó%d+' },
|
||||
expect = { { "^fóó2" }, { "^fóó4" } },
|
||||
type = 'Iterator'
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue