Modul:Collation: Porovnání verzí

Z Wikipedie, otevřené encyklopedie
Smazaný obsah Přidaný obsah
Danny B. (diskuse | příspěvky)
Collation
(Žádný rozdíl)

Verze z 24. 3. 2013, 03:44


-- @brief
--  Collation for cs (Czech) language.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


--[[
	Zde je možno editovat
	
	Prosíme, zachovávejte formát a správné abecední pořadí.
	
	Formát:
		{ sekundární abeceda, primární abeceda },
	Pořadí:
		ČSN 97 6030
		https://cs.wikipedia.org/wiki/Abecedn%C3%AD_%C5%99azen%C3%AD#.C4.8Ce.C5.A1tina
--]]
_module.alphabet = {
	{ " ", " " },
	{ "-", "-" },
	{ "a", "a" },
	{ "á", "a" },
	{ "å", "a" },
	{ "ä", "a" },
	{ "b", "b" },
	{ "c", "c" },
	{ "č", "č" },
	{ "d", "d" },
	{ "ď", "d" },
	{ "e", "e" },
	{ "é", "e" },
	{ "f", "f" },
	{ "g", "g" },
	{ "h", "h" },
	{ "ch", "ch" },
	{ "i", "i" },
	{ "í", "i" },
	{ "j", "j" },
	{ "k", "k" },
	{ "l", "l" },
	{ "m", "m" },
	{ "n", "n" },
	{ "ň", "n" },
	{ "o", "o" },
	{ "ó", "o" },
	{ "p", "p" },
	{ "q", "q" },
	{ "r", "r" },
	{ "ř", "ř" },
	{ "s", "s" },
	{ "š", "š" },
	{ "t", "t" },
	{ "ť", "t" },
	{ "u", "u" },
	{ "ú", "u" },
	{ "ů", "u" },
	{ "ü", "u" },
	{ "v", "v" },
	{ "w", "w" },
	{ "x", "x" },
	{ "y", "y" },
	{ "ý", "y" },
	{ "z", "z" },
	{ "ž", "ž" },
	{ "'", "'" }
}
--[[
	Konec možnosti editace
--]]


function charAt( str, pos )
	
	return mw.ustring.sub( str, pos, pos )
	
end


function getChar( str, pos, alphabet )
	
	local newpos = pos
	local char = charAt( str, newpos )
	
	
	while not alphabet[char] do
		newpos = newpos + 1
		char = charAt( str, newpos )
	end
	
	if ( char == "c" and charAt( str, newpos + 1 ) == "h" ) then
		char = "ch"
		newpos = newpos + 1
	end
	
	return char, newpos + 1
	
end


-- @brief
--  Compare two strings by given alphabet.
-- 
-- @param
--  String left First string
--  String right Second string
--  table alphabet Table of order of chars
-- @return
--  -1 if left < right
--  0 if left = right
--  1 if left > right
function compareByAlphabet( strLeft, strRight, alphabet )
	
	local iLeft = 1
	local iRight = 1
	local charLeft = ""
	local charRight = ""
	
	
	repeat
		charLeft, iLeft = getChar( strLeft, iLeft, alphabet );
		charRight, iRight = getChar( strRight, iRight, alphabet );
		if charLeft ~= "" or charRight ~= "" then
			if charLeft == "" then
				return 1
			elseif charRight == "" then
				return -1
			else
				if alphabet[charLeft] > alphabet[charRight] then
					return -1
				elseif alphabet[charLeft] < alphabet[charRight] then
					return 1
				end
			end
		end
	until ( charLeft == "" or charRight == "" )
	
	return 0
	
end


-- @brief
--  Compare two strings.
-- 
-- @param
--  String left First string
--  String right Second string
-- @return
--  -1 if left < right
--  0 if left = right
--  1 if left > right
function _module.compare( left, right )
	
	local output
	
	local strLeft = mw.ustring.lower( left )
	local strRight = mw.ustring.lower( right )
	
	local primaryAlphabet = {}
	local secondaryAlphabet = {}
	
	
	if strLeft == strRight then
		return 0
	end
	
	for i, letter in ipairs( _module.alphabet ) do
		secondaryAlphabet[letter[1]] = i
		primaryAlphabet[letter[1]] = secondaryAlphabet[letter[2]]
	end
	
	output = compareByAlphabet( strLeft, strRight, primaryAlphabet )
	if output == 0 then
		return compareByAlphabet( strLeft, strRight, secondaryAlphabet )
	else
		return output
	end
	
end


-- @brief
--  Compare two strings for table.sort().
-- 
-- @param
--  String left First string
--  String right Second string
-- @return
--  boolean true if left < right
--  boolean false if left >= right
function _module.sortCompare( left, right )
	
	return _module.compare( left, right ) == 1
	
end


----------------------------------------
return _module