Module:ISO 3166/sandbox: Difference between revisions

Content deleted Content added
Made luaname() also use the new logic; substantial perf hit
Try to handle strings more efficiently
Line 65:
"PROVINCE OF","PROVINCE","TERRITORY"
}
local patterns = {[".+:"]="",["|.+"]="",["%(.-%)"]="", -- patterns to follow (order may matter)
["%..*"]="",["^THE "]="",["%_"]=" ",["%-+:"]=" ",
["|.+"]="",
["%(.-%)"]="",
["%..*"]="",
["^THE "]="",
["%_"]=" ",
["%d%d?%d?PX"]="",
}
text = mw.ustring.upper(text) -- Case insensitivity
 
text = mw.ustring.gsub(text,"[À-Ý]",accents) -- Deaccent
local capture = string.match(text, '[ -~]*'); -- Keep only ASCII printable characters
if capture ~= text then -- If same there are no Unicode characters
text = mw.ustring.gsub(text,"[À-Ý]",accents) -- Deaccent
end
for pattern,value in pairs(patterns) do -- Follow patterns
text = mw.ustringstring.gsub(text,pattern,value)
end
for _,words in pairs(remove) do -- Remove unneeded words
text = mw.ustring.gsub(text,words,"")
end
-- Hyphen-sensitive
text = mw.ustringstring.gsub(text,"[^%w%W-]","") -- Remove non alpha-numeric
return text
end
 
Line 100 ⟶ 109:
 
function p.luacode(args)
if country(args[1] ~= nil) then countryargs[1] = p.strip(countryargs[1]) end
if subdivision(args[2] ~= nil) then subdivisionargs[2] = p.strip(subdivisionargs[2]) end
local eota1, a2 = args.error[1], or ""args[2]
local hyphensplitresult = p.luacodeimpl(args) -- Prioritize splitting on hyphen (subdivision)
local hyphenPos = string.find(a1 or '', '-', 1, true)
if not hyphenPos then --No hyphen
return p.luacodeimpl(args)
end
args[1] = string.sub(a1, 1, hyphenPos-1) --Try splitting on hyphen
args[2] = string.sub(a1, hyphenPos+1)
local hyphensplitresult = p.luacodeimpl(args) -- Prioritize splitting on hyphen (subdivision)
if hyphensplitresult ~= nil and hyphensplitresult ~= '' then
return hyphensplitresult
end
if string.find(args[1], orargs[2] = ''a1, "%-") thena2 -- If it might make a difference, tryTry again without splitting
local keephyphenresult = return p.luacodeimpl(args,) true)or ''
return keephyphenresult
end
return ''
end
 
function p.luacodeimpl(args, keephyphen)
local country, subdivision = args[1], args[2]
if not keephyphen then
if string.find(country or '',"%-") then
country, subdivision = string.match(country or '',"^([^%-]*)%-(.*)$")
end
end
 
if country then country = p.strip(country) end
if subdivision then subdivision = p.strip(subdivision) end
if args["codetype"]=="3" then
args["codetype"]="alpha3"
end
local eot = args.error or ""
local catnocountry = (args.nocat and args.nocat == 'true') and ''
Line 207 ⟶ 211:
local code2 = ''
ifhyphenPos = string.find(code1, "%-"), then1, true)
if hyphenPos ~= nil then
code1, code2 = string.matchsub(code1,"^([^%-]*)%-(.*)$" hyphenPos+1)
code1 = string.sub(code1, 1, hyphenPos-1)
end
if string.findlen(code1,"^%u%u$") == 2 then
if code2=="" then --3166-1 alpha-2 code
if data[code1] then
Line 261 ⟶ 267:
targs[1] = args[1] or ''
for i, v in pairs(args) do
if i == 'country' and not mw.ustringstring.find(targs[1], 'region:', 1, true) then
local country = v
local k, region = 1, ''