Module:Convert: Difference between revisions

an output combination unit can be user-defined, consisting of unit codes separated by "+" or " " (space)
(new style: 4-column tabs)
(an output combination unit can be user-defined, consisting of unit codes separated by "+" or " " (space))
Line 590:
end
end
-- Accept user-defined combocombinations like "MTacre+LTm2+ha" (convertor output"acre tom2 MTha" andfor LT)output.
-- If '+' is used, each unit code can include a space, and any error is fatal.
-- If ' ' is used and if each space-separated word is a unit code, it is a combo,
-- but errors are not fatal so the unit code can be looked up as an extra unit.
local err_is_fatal
local combo = collection()
if unitcode:find('+', 1, true) then
for item in string.gmatch(unitcode .. '+', '%s*(.-)%s*%+') do
err_is_fatal = true
if item ~= '' then
for item in string.gmatch(unitcode .. '+', ):gmatch('%s*(.-)%s*%+') do
if item ~= '' then
combo:add(item)
end
end
elseif unitcode:find('%s') then
for item in unitcode:gmatch('%S+') do
combo:add(item)
end
end
if combo.n > 1 then
local function lookup_combo()
if what == 'no_combination' or what == 'only_multiple' then
return false, { 'cvt_bad_unit', unitcode }
end
elseend
local result = { combination = {} }
local cvtresult = result.{ combination = {} }
local resultcvt = { result.combination = {} }
for i, v in ipairs(combo) do
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
if inot == 1success then return false, t end
result.utypeif i == 1 t.utypethen
result.utype = t.utype
else
else
local mismatch = check_mismatch(result, t)
if local mismatch then= check_mismatch(result, t)
return false,if mismatch then
return false, mismatch
end
end
cvt[i] = t
end
cvt[i]return =true, tresult
end
local success, result = lookup_combo()
if success or err_is_fatal then
return truesuccess, result
end
return true, result
end
if not extra_units then
Line 627 ⟶ 644:
-- A unit in one data table might refer to a unit in the other table, so
-- switch between them, relying on fails or depth to terminate loops.
localif failed =not fails[unitcode] then
if not failed then
fails[unitcode] = true
local other = (utable == all_units) and extra_units or all_units
Anonymous user