Bước tới nội dung

Mô đun:collation

Từ điển mở Wiktionary
localexport={}

localrequire=require
localbyte=string.byte
localconcat=table.concat
localget_plaintext=require("Module:utilities").get_plaintext
localmatch=string.match
localmemoize=require("Module:fun").memoize
localremove=table.remove
localsort=table.sort
localstring_sort-- defined below as export.string_sort
localtrim=mw.text.trim
localtype=type

-- Custom functions for generating a sortkey that will achieve the desired sort
-- order.
-- name of module and name of exported function
localcustom_funcs={
ahk={"Mymr-sortkey","makeSortKey"},
aio={"Mymr-sortkey","makeSortKey"},
blk={"Mymr-sortkey","makeSortKey"},
egy={"egy-utilities","make_sortkey"},
kac={"Mymr-sortkey","makeSortKey"},
kht={"Mymr-sortkey","makeSortKey"},
ksw={"Mymr-sortkey","makeSortKey"},
kyu={"Mymr-sortkey","makeSortKey"},
["mkh-mmn"]={"Mymr-sortkey","makeSortKey"},
mnw={"Mymr-sortkey","makeSortKey"},
my={"Mymr-sortkey","makeSortKey"},
phk={"Mymr-sortkey","makeSortKey"},
pwo={"Mymr-sortkey","makeSortKey"},
omx={"Mymr-sortkey","makeSortKey"},
shn={"Mymr-sortkey","makeSortKey"},
tjl={"Mymr-sortkey","makeSortKey"},
}

localfunctionis_lang_object(lang)
returntype(lang)=="table"andtype(lang.getCanonicalName)=="function"
end

localfunctioncheck_function(funcName,argIdx,func)
iftype(func)~="function"then
error("bad argument #"..argIdx.."to"..funcName
..":expected function object, got"..type(func)..".",2)
end
returntrue
end

localfunctionmake_sortkey_func(lang,make_sortbase)
locallangcode=lang:getCode()
localmakeDisplayText=lang.makeDisplayText
localcustom_func=custom_funcs[langcode]

localmakeSortKey
ifcustom_functhen
local_makeSortKey=require("Module:"..custom_func[1])[custom_func[2]]
functionmakeSortKey(_,text)
return_makeSortKey(text,langcode)
end
else
makeSortKey=lang.makeSortKey
end

returnmake_sortbaseandcheck_function("make_sortkey_func",2,make_sortbase)andfunction(element)
return(makeSortKey(
lang,
(makeDisplayText(
lang,
get_plaintext(make_sortbase(element))
))
))
endorfunction(element)
return(makeSortKey(
lang,
(makeDisplayText(
lang,
get_plaintext(element)
))
))
end
end

-- When comparing two elements with code points outside the BMP, the less-than
-- operator treats all code points above U+FFFF as equal because of a bug in
-- glibc. See [[phab:T193096#4161287]]. Instead, compares bytes, which always
-- yields the same result as comparing code points in valid UTF-8 strings.
-- UTF-8-encoded characters that do not belong to the Basic Multilingual Plane
-- (that is, with code points greater than U+FFFF) have byte sequences that
-- begin with the bytes 240 to 244.
do
-- Memoize match because we've set the `simple` flag, which means it
-- should only be used with fixed additional arguments (in this case, the
-- pattern).
localmatch=memoize(match,true)

functionexport.string_sort(item1,item2)
ifmatch(item1,"^[^\240-\244]*$ ")andmatch(item2,"^[^\240-\244]*$ ")then
returnitem1<item2
end
locali=0
whiletruedo
i=i+1
localb1,b2=byte(item1,i,i),byte(item2,i,i)
ifnotb1then
returnb2andtrueorfalse
elseifb1~=b2then
returnb2andb1<b2orfalse
end
end
end
string_sort=export.string_sort
end

functionexport.sort(elems,lang,make_sortbase)
ifnotis_lang_object(lang)then
returnsort(elems)
end

localmake_sortkey=memoize(make_sortkey_func(lang,make_sortbase),true)

returnsort(elems,function(elem1,elem2)
returnstring_sort(make_sortkey(elem1),make_sortkey(elem2))
end)
end

functionexport.sort_template(frame)
ifnotmw.isSubsting()then
error("This template must be substed.")
end

localargs
ifframe.args.parentthen
args=frame:getParent().args
else
args=frame.args
end

localelems=require("Module:table").shallowcopy(args)
localm_languages=require("Module:languages")
locallang
ifargs.langthen
lang=m_languages.getByCode(args.lang)orm_languages.err(args.lang,"lang")
else
localcode=remove(elems,1)
code=codeandtrim(code)
lang=m_languages.getByCode(code)orm_languages.err(code,1)
end
export.sort(elems,lang)
returnconcat(elems,args.sepor"|")
end

returnexport