Module:希顶字母数字

此后如竟没有炬火,我便是唯一的光。
跳转到导航 跳转到搜索

本模块用于实现阿拉伯数字转希顶字母数字。


local num = {}
function num.xdi8LexiNum(frame)
    local input = frame.args[1]
    local p, _ = string.find(input, '.', 1, true)
    if not p then
        return num._partialInt(input) or num._error('在转换整数时失败')
    else
        return (num._partialInt(string.sub(input, 1, p - 1)) .. '.' .. num._partialFrac(string.sub(input, p + 1))) or num._error('在转换实数时失败')
    end
end

function num._partialInt(input)
    local pos = 0
    local result = ''
    local len = string.len(input)
    input = string.reverse(input)
    for i = 1, len do
        local dig = string.byte(input, i) - 48
        if pos == 45 then
            result = '′' .. result
            pos = 0
        end
        if dig < 0 or dig > 9 then
            return nil
        elseif dig ~= 0 then
            result = string.char(string.byte('bpmwjqxynzDsrHNldtgkh45vF7BcfuaoeEAYL62T83V1i', pos + dig)) .. result
        end
        pos = pos + 9
    end
    if result == '' then
        result = '′'
    end
    return result
end

function num._partialFrac(input)
    local pos = 0
    local result = ''
    local len = string.len(input)
    for i = 1, len do
        local dig = string.byte(input, i) - 48
        if pos == 45 then
            result = result .. '′'
            pos = 0
        end
        if dig < 0 or dig > 9 then
            return nil
        elseif dig ~= 0 then
            result = result .. string.char(string.byte('bpmwjqxynzDsrHNldtgkh45vF7BcfuaoeEAYL62T83V1i', pos + dig))
        end
        pos = pos + 9
    end
    if result == '' then
        result = '′'
    end
    return result
end

-- 复制自[[模块:String]]
function num._error( error_str )
	local frame = mw.getCurrentFrame();
	local error_category = frame.args.error_category or 'Errors reported by Module 希顶字母数字';
	local ignore_errors = frame.args.ignore_errors or false;
	local no_category = frame.args.no_category or false;
	local error_str = '<strong class="error">希顶字母数字模块错误: ' .. error_str .. '</strong>';
	if error_category ~= '' then
		error_str = '[[Category:' .. error_category .. ']]' .. error_str;
	end
	return error_str;
end

return num