Module:Convert: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(new style: 4-column tabs)
(an output combination unit can be user-defined, consisting of unit codes separated by "+" or " " (space))
Line 590: Line 590:
end
end
end
end
-- Accept user-defined combo like "MT+LT" (convert output to MT and LT).
-- Accept user-defined combinations like "acre+m2+ha" or "acre m2 ha" for 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()
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 (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)
combo:add(item)
end
end
end
end
if combo.n > 1 then
if combo.n > 1 then
local function lookup_combo()
if what == 'no_combination' or what == 'only_multiple' then
if what == 'no_combination' or what == 'only_multiple' then
return false, { 'cvt_bad_unit', unitcode }
return false, { 'cvt_bad_unit', unitcode }
end
end
local result = { combination = {} }
local cvt = result.combination
local result = { combination = {} }
local cvt = result.combination
for i, v in ipairs(combo) do
for i, v in ipairs(combo) do
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
local success, t = lookup(v, opt_sp_us, 'no_combination', utable, fails, depth)
if not success then return false, t end
if i == 1 then
if not success then return false, t end
result.utype = t.utype
if i == 1 then
result.utype = t.utype
else
else
local mismatch = check_mismatch(result, t)
if mismatch then
local mismatch = check_mismatch(result, t)
return false, mismatch
if mismatch then
return false, mismatch
end
end
end
cvt[i] = t
end
end
cvt[i] = t
return true, result
end
local success, result = lookup_combo()
if success or err_is_fatal then
return success, result
end
end
return true, result
end
end
if not extra_units then
if not extra_units then
Line 627: Line 644:
-- A unit in one data table might refer to a unit in the other table, so
-- 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.
-- switch between them, relying on fails or depth to terminate loops.
local failed = fails[unitcode]
if not fails[unitcode] then
if not failed then
fails[unitcode] = true
fails[unitcode] = true
local other = (utable == all_units) and extra_units or all_units
local other = (utable == all_units) and extra_units or all_units