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 |
-- 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 |
|||
⚫ | |||
err_is_fatal = true |
|||
⚫ | |||
⚫ | |||
⚫ | |||
combo:add(item) |
|||
⚫ | |||
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 } |
||
⚫ | |||
⚫ | |||
⚫ | |||
local |
local 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 |
if not success then return false, t end |
||
if i == 1 then |
|||
result.utype = t.utype |
|||
⚫ | |||
else |
|||
local mismatch = check_mismatch(result, t) |
|||
local mismatch = check_mismatch(result, t) |
|||
if mismatch then |
|||
return false, mismatch |
|||
end |
|||
end |
end |
||
cvt[i] = t |
|||
end |
end |
||
return true, result |
|||
end |
|||
local success, result = lookup_combo() |
|||
if success or err_is_fatal then |
|||
⚫ | |||
end |
end |
||
⚫ | |||
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. |
||
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 |