Jump to content

မေႃႇၵျူး:Hani-sortkey/data/serializer

လုၵ်ႉတီႈ ဝိၵ်ႇသျိၼ်ႇၼရီႇ မႃး

Documentation for this module may be created at မေႃႇၵျူး:Hani-sortkey/data/serializer/doc

local insert = table.insert

local export = {}

local byte_lookup = {
	[0x07] = "\\a",
	[0x08] = "\\b",
	[0x09] = "\\t",
	[0x0A] = "\\n",
	[0x0B] = "\\v",
	[0x0C] = "\\f",
	[0x0D] = "\\r",
	[0x22] = "\\\"",
	[0x5C] = "\\\\",
}

function export.main(checker)
	local data = require("Module:Hani-sortkey/data")
	local ret = {}
	
	local keys, radicals, val = data.keys, data.radicals
	for i = 1, 0x323AF do
		if keys[i] then
			val = keys[i]:gsub("(%d+)%.(.*)", function(r, as)
				r = tonumber(r)
				if byte_lookup[r] then
					r = byte_lookup[r]
				elseif r < 0x20 or r >= 0x7F then
					r = "\\" .. ("%03d"):format(r)
				else
					r = string.char(r)
				end
			-- Negative additional stroke counts are subtracted from 10 (i.e. -1 becomes -9, -2 becomes -8 etc.), so that lower values sort before higher ones (i.e. -3, -2, -1 ...).
				as = tonumber(as)
				if as < 0 then
					as = tostring(-10 - as)
				else
					as = ("%02d"):format(as)
				end
				return r .. as
			end)
			insert(ret, val)
		end
	end
	
	insert(ret, "\\254")
	
	for i, radical in ipairs(data.radicals) do
		insert(ret, radical)
	end
	
	insert(ret, "\\255")
	
	for k, v in pairs(data.preconvert) do
		insert(ret, k .. "\\001" .. v .. "\\002")
	end
	
	return table.concat(ret)
end

return export