Moduł:utils: Różnice pomiędzy wersjami

Z Wikisłownika – wolnego słownika wielojęzycznego
[wersja przejrzana][wersja przejrzana]
Usunięta treść Dodana treść
Nie podano opisu zmian
(test) optymalizacja kodu w p.countSubstring(), uzycie mw.ustring powodowalo przekroczenie limitu czasu pracy Lua w Olafbot/SK (Specjalna:Diff/5014055)
Linia 78: Linia 78:


function p.countSubstring( mainString, subString )
function p.countSubstring( mainString, subString )
local count, offset, _ = -1, 0
local tab = mw.text.split( mainString, subString, true )
local find
return #tab - 1
if mw.ustring.len( subString ) == string.len( subString ) then
find = string.find
else
find = mw.ustring.find
end
repeat
_, offset = find( mainString, subString, offset + 1, true )
count = count + 1
until offset == nil
return count
end
end



Wersja z 14:43, 8 kwi 2016

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:utils/opis

local p = {}

local encodeTables = {
	PWN = {
		[ 'ą' ] = '%B1',
		[ 'ć' ] = '%E6',
		[ 'ę' ] = '%EA',
		[ 'ł' ] = '%B3',
		[ 'ń' ] = '%F1',
		[ 'ó' ] = '%F3',
		[ 'ś' ] = '%B6',
		[ 'ź' ] = '%BC',
		[ 'ż' ] = '%BF',
		[ ' ' ] = '+'
	},
	RAE = {
		[ 'á' ] = '%E1',
		[ 'é' ] = '%E9',
		[ 'í' ] = '%ED',
		[ 'ó' ] = '%F3',
		[ 'ú' ] = '%FA', [ 'ü' ] = '%FC',
		[ 'ñ' ] = '%F1',
		[ ' ' ] = '%20'
	},
	diccionari_cat = {
		[ 'é' ] = '%E9', [ 'è' ] = '%E8',
		[ 'ó' ] = '%F3', [ 'ò' ] = '%F2',
		[ 'ç' ] = '%E7',
		[ 'ü' ] = '%FC',
		[ '·' ] = '%B7',
		[ ' ' ] = '+',
	}
}

function p.encodeUri( frame )
	local text = frame.args[ 1 ] or ''
	local data = encodeTables[ frame.args[ 2 ] ]
	
	if not data then return text end
	
	local output = {}
	
	for codepoint in mw.ustring.gcodepoint( text ) do
		local char = mw.ustring.char( codepoint )
		table.insert( output, data[ char ] or char )
	end
	
	return table.concat( output )
end

function p.trimString( frame )
	local text = frame.args[ 1 ] or ''
	local pos  = tonumber( frame.args[ 2 ] or '' )
	local pos2 = tonumber( frame.args[ 3 ] or '' )
	
	assert( type( pos ) == 'number', 'drugi parametr nie jest liczbą' )

	return mw.ustring.sub( text, pos, pos2 or -1 )
end

function p.indexOf( frame )
	local text = frame.args[ 1 ] or ''
	local sub = frame.args[ 2 ] or ''
	
	return mw.ustring.find( text, sub, 1, true ) or 0
end

function p.stringLength( frame )
	local text = frame.args[ 1 ] or ''
	local bytes = frame.args[ 2 ] == 'B'
	
	if bytes then
		return string.len( text )
	else
		return mw.ustring.len( text )
	end
end

function p.countSubstring( mainString, subString )
	local count, offset, _ = -1, 0
	local find
	
	if mw.ustring.len( subString ) == string.len( subString ) then
		find = string.find
	else
		find = mw.ustring.find
	end
	
	repeat
		_, offset = find( mainString, subString, offset + 1, true )
		count = count + 1
	until offset == nil
	
	return count
end

function p.countSubstringInPage( subString, pageName )
	local title = mw.title.makeTitle( '', pageName )
	return title and p.countSubstring( title:getContent(), subString ) or ''
end

function p.countLinksInPage( frame )
	local pageName = frame.args[ 1 ] or ''
	return p.countSubstringInPage( '[[', pageName )
end

function p.countSectionsInPage( frame )
	local pageName = frame.args[ 1 ] or ''
	return p.countSubstringInPage( '\n=', pageName )
end

function p.countOrderedItemsInPage( frame )
	local pageName = frame.args[ 1 ] or ''
	return p.countSubstringInPage( '\#', pageName )
end

return p