mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-12-18 02:41:09 +00:00
mw.ustring functions should accept numbers where string functions do
Lua's string functions tend to auto-convert numbers to strings. We should do the same in mw.ustring. Bug: 67201 Change-Id: Icd3c5e93bac19dafd78d737ec9b315daba9f1729
This commit is contained in:
parent
ccab415701
commit
bf39827980
|
@ -81,6 +81,9 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkString( $name, $s, $checkEncoding = true ) {
|
private function checkString( $name, $s, $checkEncoding = true ) {
|
||||||
|
if ( $this->getLuaType( $s ) == 'number' ) {
|
||||||
|
$s = (string)$s;
|
||||||
|
}
|
||||||
$this->checkType( $name, 1, $s, 'string' );
|
$this->checkType( $name, 1, $s, 'string' );
|
||||||
if ( $checkEncoding && !$this->checkEncoding( $s ) ) {
|
if ( $checkEncoding && !$this->checkEncoding( $s ) ) {
|
||||||
throw new Scribunto_LuaError( "bad argument #1 to '$name' (string is not UTF-8)" );
|
throw new Scribunto_LuaError( "bad argument #1 to '$name' (string is not UTF-8)" );
|
||||||
|
@ -226,6 +229,9 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkPattern( $name, $pattern ) {
|
private function checkPattern( $name, $pattern ) {
|
||||||
|
if ( $this->getLuaType( $pattern ) == 'number' ) {
|
||||||
|
$pattern = (string)$pattern;
|
||||||
|
}
|
||||||
$this->checkType( $name, 2, $pattern, 'string' );
|
$this->checkType( $name, 2, $pattern, 'string' );
|
||||||
if ( !$this->checkEncoding( $pattern ) ) {
|
if ( !$this->checkEncoding( $pattern ) ) {
|
||||||
throw new Scribunto_LuaError( "bad argument #2 to '$name' (string is not UTF-8)" );
|
throw new Scribunto_LuaError( "bad argument #2 to '$name' (string is not UTF-8)" );
|
||||||
|
@ -584,6 +590,7 @@ class Scribunto_LuaUstringLibrary extends Scribunto_LuaLibraryBase {
|
||||||
|
|
||||||
switch ( $this->getLuaType( $repl ) ) {
|
switch ( $this->getLuaType( $repl ) ) {
|
||||||
case 'string':
|
case 'string':
|
||||||
|
case 'number':
|
||||||
$cb = function ( $m ) use ( $repl, $anypos, &$captures ) {
|
$cb = function ( $m ) use ( $repl, $anypos, &$captures ) {
|
||||||
if ( $anypos ) {
|
if ( $anypos ) {
|
||||||
$m = array_shift( $captures );
|
$m = array_shift( $captures );
|
||||||
|
|
|
@ -33,6 +33,9 @@ local function checkType( name, argidx, arg, expecttype, nilok )
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkString( name, s )
|
local function checkString( name, s )
|
||||||
|
if type( s ) == 'number' then
|
||||||
|
s = tostring( s )
|
||||||
|
end
|
||||||
if type( s ) ~= 'string' then
|
if type( s ) ~= 'string' then
|
||||||
local msg = S.format( "bad argument #1 to '%s' (string expected, got %s)",
|
local msg = S.format( "bad argument #1 to '%s' (string expected, got %s)",
|
||||||
name, type( s )
|
name, type( s )
|
||||||
|
@ -48,6 +51,9 @@ local function checkString( name, s )
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkPattern( name, pattern )
|
local function checkPattern( name, pattern )
|
||||||
|
if type( pattern ) == 'number' then
|
||||||
|
pattern = tostring( pattern )
|
||||||
|
end
|
||||||
if type( pattern ) ~= 'string' then
|
if type( pattern ) ~= 'string' then
|
||||||
local msg = S.format( "bad argument #2 to '%s' (string expected, got %s)",
|
local msg = S.format( "bad argument #2 to '%s' (string expected, got %s)",
|
||||||
name, type( pattern )
|
name, type( pattern )
|
||||||
|
@ -915,6 +921,9 @@ function ustring.gsub( s, pattern, repl, n )
|
||||||
tp = 2
|
tp = 2
|
||||||
elseif type( repl ) == 'string' then
|
elseif type( repl ) == 'string' then
|
||||||
tp = 3
|
tp = 3
|
||||||
|
elseif type( repl ) == 'number' then
|
||||||
|
repl = tostring( repl )
|
||||||
|
tp = 3
|
||||||
else
|
else
|
||||||
checkType( 'gsub', 3, repl, 'function or table or string' )
|
checkType( 'gsub', 3, repl, 'function or table or string' )
|
||||||
end
|
end
|
||||||
|
|
|
@ -499,6 +499,18 @@ return testframework.getTestProvider( {
|
||||||
args = { 'foo; fóó', '(%a+)', '%2' },
|
args = { 'foo; fóó', '(%a+)', '%2' },
|
||||||
expect = "invalid capture index %2 in replacement string"
|
expect = "invalid capture index %2 in replacement string"
|
||||||
},
|
},
|
||||||
|
{ name = 'gsub: passing numbers instead of strings (1)', func = mw.ustring.gsub,
|
||||||
|
args = { 12345, '[33]', '9' },
|
||||||
|
expect = { '12945', 1 }
|
||||||
|
},
|
||||||
|
{ name = 'gsub: passing numbers instead of strings (2)', func = mw.ustring.gsub,
|
||||||
|
args = { '12345', 3, '9' },
|
||||||
|
expect = { '12945', 1 }
|
||||||
|
},
|
||||||
|
{ name = 'gsub: passing numbers instead of strings (3)', func = mw.ustring.gsub,
|
||||||
|
args = { '12345', '[33]', 9 },
|
||||||
|
expect = { '12945', 1 }
|
||||||
|
},
|
||||||
|
|
||||||
{ name = 'gcodepoint: basic test', func = mw.ustring.gcodepoint,
|
{ name = 'gcodepoint: basic test', func = mw.ustring.gcodepoint,
|
||||||
args = { str1 },
|
args = { str1 },
|
||||||
|
|
Loading…
Reference in a new issue