模組:Sandbox/Al12si/zz
模組解[開]
你可能想去為呢個Scribunto module開一個解版。 編者可以響呢個模組嘅沙盤 (開 | 鏡)同埋試例 (開)版度試驗佢。 請加個類到個/doc嘅細版度。 呢個模組嘅細版。 |
-- vim: set sw=4 ts=4 ai sm :
require ('strict');
local p = {};
local t = require('模組:Sandbox/Al12si/zz/Configuration');
p.strokes = t.strokes;
p.radicals = t.radicals;
t = require('模組:書名');
p.cvs = t.cvs;
local function stroke_count (s0)
local it = 0;
local s = mw.ustring.sub(s0, 1, 1);
for number_of_strokes, all_candidates in pairs(p.strokes) do
for _, candidates in pairs(all_candidates) do
if mw.ustring.match(candidates, s) then
it = number_of_strokes;
end
if it > 0 then break end;
end
if it > 0 then break end;
end
return it;
end
local function by_stroke_count (a, b)
local cmp;
for i = 1, math.max(mw.ustring.len(a), mw.ustring.len(b)) do
local a_i = mw.ustring.sub(a, i, i);
local b_i = mw.ustring.sub(b, i, i);
if a_i:len() == 0 then
cmp = -1;
elseif b_i:len() == 0 then
cmp = 1;
else
local n_a = stroke_count(a_i);
local n_b = stroke_count(b_i);
if n_a < n_b then
cmp = -1;
elseif n_a > n_b then
cmp = 1;
end
end
if cmp then break end;
end
return cmp == -1;
end
local function dup (a)
local b;
if a then
b = {};
for k, v in pairs(a) do
b[k] = v;
end
end
return b;
end
local function create_article_list (items)
local it;
table.sort(items, by_stroke_count);
for _, item in pairs(items) do
item = item:gsub('^%s+', ''):gsub('%s+$', '');
if it then
it = it .. "\n";
else
it = '';
end
it = it .. '*[[' .. item .. ']]';
end
return it;
end
p.test = function ()
assert(stroke_count('一') == 1);
assert(stroke_count('一二') == 1);
assert(by_stroke_count('一', '二') == true);
assert(by_stroke_count('二', '丿') == false);
assert(by_stroke_count('一', '一一') == true);
assert(by_stroke_count('二', '二一') == true);
assert(by_stroke_count('二三', '二一') == false);
assert(create_article_list({'a', 'b'}) == "*[[a]]\n*[[b]]");
assert(create_article_list({'a\n', 'b\n'}) == "*[[a]]\n*[[b]]");
local a = {};
local b = dup(a);
assert(a ~= b);
assert(#a == #b);
a = {123};
b = dup(a);
assert(a ~= b);
assert(#a == #b);
assert(a[0] == b[0]);
end
p.xx = function (frame)
return create_article_list(dup(frame.args));
end
p.stroke_count = stroke_count;
p.by_stroke_count = by_stroke_count;
return p;