Module:Unicode data: Difference between revisions
Jump to navigation
Jump to search
move some data to a data submodule
(Update for Unicode 14.0) |
(move some data to a data submodule) |
||
Line 1: | Line 1: | ||
local | local export = {} | ||
local udata = mw.loadData("Module:Unicode data/data") | |||
local floor = math.floor | local floor = math.floor | ||
local function errorf( | local function errorf(first_arg, ...) | ||
if type( | if type(first_arg) == "number" then | ||
return error(string.format(...), | return error(string.format(...), first_arg + 1) | ||
else | else | ||
return error(string.format( | return error(string.format(first_arg, ...), 2) | ||
end | end | ||
end | end | ||
Line 13: | Line 14: | ||
local function binary_range_search(codepoint, ranges) | local function binary_range_search(codepoint, ranges) | ||
local low, mid, high | local low, mid, high | ||
low, high = 1, ranges.length or require "Module: | low, high = 1, ranges.length or require "Module:table".length(ranges) | ||
while low <= high do | while low <= high do | ||
mid = floor((low + high) / 2) | mid = floor((low + high) / 2) | ||
Line 27: | Line 28: | ||
return nil, mid | return nil, mid | ||
end | end | ||
export.binary_range_search = binary_range_search | |||
local function linear_range_search(codepoint, ranges) | local function linear_range_search(codepoint, ranges) | ||
for i, range in ipairs(ranges) do | for i, range in ipairs(ranges) do | ||
if range[1] | if codepoint < range[1] then | ||
break | |||
elseif codepoint <= range[2] then | |||
return range | return range | ||
end | end | ||
end | end | ||
end | end | ||
-- Load a module by indexing "loader" with the name of the module minus the | -- Load a module by indexing "loader" with the name of the module minus the | ||
Line 57: | Line 58: | ||
-- see "Hangul Syllable Name Generation" in section 3.12 of the | -- see "Hangul Syllable Name Generation" in section 3.12 of the | ||
-- Unicode Specification: | -- Unicode Specification: | ||
-- https://www.unicode.org/versions/ | -- https://www.unicode.org/versions/latest/ch03.pdf | ||
-- For most of the name rules given here, see the subsection | |||
-- "Unicode Name Property" in section 4.8 (Name) and the table 4-8 | |||
-- (Name Derivation Rule Prefix Strings): | |||
-- https://www.unicode.org/versions/latest/ch04.pdf | |||
local name_hooks = { | local name_hooks = { | ||
{ 0x00, 0x1F, "<control-%04X>" }, -- C0 control characters | { 0x00, 0x1F, "<control-%04X>" }, -- C0 control characters | ||
Line 80: | Line 85: | ||
{ 0xF900, 0xFA6D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0xF900, 0xFA6D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0xFA70, 0xFAD9, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0xFA70, 0xFAD9, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0x17000, 0x187F7, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | { 0x17000, 0x187F7, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | ||
{ 0x18800, 0x18AFF, function (codepoint) | { 0x18800, 0x18AFF, function (codepoint) | ||
return ("TANGUT COMPONENT-%03d"):format(codepoint - 0x187FF) | return ("TANGUT COMPONENT-%03d"):format(codepoint - 0x187FF) | ||
end }, | end }, | ||
{ 0x18D00, 0x18D08, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | { 0x18D00, 0x18D08, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | ||
{ 0x18B00, 0x18CD5, "KHITAN SMALL SCRIPT CHARACTER-%04X" }, | |||
{ 0x1B170, 0x1B2FB, "NUSHU CHARACTER-%04X" }, -- Nushu | { 0x1B170, 0x1B2FB, "NUSHU CHARACTER-%04X" }, -- Nushu | ||
{ 0x20000, 0x2A6DF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension B | { 0x20000, 0x2A6DF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension B | ||
{ 0x2A700, 0x2B738, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension C | { 0x2A700, 0x2B738, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension C | ||
{ | { 0x2A740, 0x2B81D, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension D | ||
{ 0x2B820, 0x2CEA1, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension E | { 0x2B820, 0x2CEA1, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension E | ||
{ 0x2CEB0, 0x2EBE0, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension F | { 0x2CEB0, 0x2EBE0, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension F | ||
-- CJK Compatibility Ideographs Supplement (Supplementary Ideographic Plane) | -- CJK Compatibility Ideographs Supplement (Supplementary Ideographic Plane) | ||
{ 0x2F800, 0x2FA1D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0x2F800, 0x2FA1D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0x30000, 0x3134A, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension G | |||
{ 0xE0100, 0xE01EF, function (codepoint) -- Variation Selectors Supplement | { 0xE0100, 0xE01EF, function (codepoint) -- Variation Selectors Supplement | ||
return ("VARIATION SELECTOR-%d"):format(codepoint - 0xE0100 + 17) | return ("VARIATION SELECTOR-%d"):format(codepoint - 0xE0100 + 17) | ||
end}, | end}, | ||
{ 0xF0000, 0xFFFFD, "<private-use-%04X>" }, -- Plane 15 Private Use | { 0xF0000, 0xFFFFD, "<private-use-%04X>" }, -- Plane 15 Private Use | ||
{ 0x100000, 0x10FFFD, "<private-use-%04X>" } -- Plane 16 Private Use | { 0x100000, 0x10FFFD, "<private-use-%04X>" } -- Plane 16 Private Use | ||
Line 124: | Line 130: | ||
--]] | --]] | ||
-- https://www.unicode.org/versions/ | -- https://www.unicode.org/versions/latest/ch04.pdf, section 4.8 | ||
function | function export.lookup_name(codepoint) | ||
-- U+FDD0-U+FDEF and all code points ending in FFFE or FFFF are Unassigned | -- U+FDD0-U+FDEF and all code points ending in FFFE or FFFF are Unassigned | ||
-- (Cn) and specifically noncharacters: | -- (Cn) and specifically noncharacters: | ||
Line 160: | Line 166: | ||
end | end | ||
function export.lookup_image(codepoint) | |||
function | |||
local data = loader[('images/%03X'):format(codepoint / 0x1000)] | local data = loader[('images/%03X'):format(codepoint / 0x1000)] | ||
Line 169: | Line 173: | ||
end | end | ||
end | end | ||
-- Load [[Module:Unicode data/blocks]] if needed and assign it to this variable. | -- Load [[Module:Unicode data/blocks]] if needed and assign it to this variable. | ||
Line 189: | Line 182: | ||
if data then | if data then | ||
-- Unpack doesn't work on tables loaded with mw.loadData. | -- Unpack doesn't work on tables loaded with mw.loadData. | ||
return i, data[ | return i, data[3], data[1], data[2] | ||
end | end | ||
end | end | ||
-- An ipairs-type iterator generator for the list of blocks. | -- An ipairs-type iterator generator for the list of blocks. | ||
function | function export.enum_blocks() | ||
local blocks = loader.blocks | local blocks = loader.blocks | ||
return block_iter, blocks, 0 | return block_iter, blocks, 0 | ||
end | end | ||
function | function export.get_block_range(name) | ||
local range | |||
for i, block in ipairs(loader.blocks) do | |||
if block[3] == name then | |||
range = block | |||
end | |||
end | |||
if range then | |||
return range[1], range[2] | |||
end | |||
end | |||
function export.lookup_plane(codepoint) | |||
local i = floor(codepoint / 0x10000) | local i = floor(codepoint / 0x10000) | ||
return planes[i] or ("Plane %u"):format(i) | return udata.planes[i] or ("Plane %u"):format(i) | ||
end | end | ||
function | function export.lookup_block(codepoint) | ||
local blocks = loader.blocks | local blocks = loader.blocks | ||
local range = binary_range_search(codepoint, blocks) | local range = binary_range_search(codepoint, blocks) | ||
Line 214: | Line 221: | ||
end | end | ||
function | function export.get_block_info(name) | ||
for i, block in ipairs(loader.blocks) do | for i, block in ipairs(loader.blocks) do | ||
if block[3] == name then | if block[3] == name then | ||
Line 222: | Line 229: | ||
end | end | ||
function | function export.is_valid_pagename(pagename) | ||
local has_nonws = false | local has_nonws = false | ||
Line 239: | Line 246: | ||
end | end | ||
local printable, result = | local printable, result = export.is_printable(cp) | ||
if not printable then | if not printable then | ||
return false | return false | ||
Line 328: | Line 335: | ||
end | end | ||
return match_func(codepoint) | return match_func(codepoint, unpack(dots)) | ||
end | end | ||
end | end | ||
Line 338: | Line 345: | ||
-- See https://www.unicode.org/reports/tr44/#Canonical_Combining_Class_Values for | -- See https://www.unicode.org/reports/tr44/#Canonical_Combining_Class_Values for | ||
-- more information. | -- more information. | ||
export.is_combining = memo_lookup( | |||
"combining", | "combining", | ||
function (codepoint, combining_class) | function (codepoint, combining_class) | ||
Line 345: | Line 352: | ||
0) | 0) | ||
function | function export.add_dotted_circle(str) | ||
return (mw.ustring.gsub(str, ".", | return (mw.ustring.gsub(str, ".", | ||
function(char) | function(char) | ||
if | if export.is_combining(mw.ustring.codepoint(char)) then | ||
return '◌' .. char | return '◌' .. char | ||
end | end | ||
Line 360: | Line 367: | ||
end, | end, | ||
"assigned") | "assigned") | ||
export.lookup_control = lookup_control | |||
function | function export.is_assigned(codepoint) | ||
return lookup_control(codepoint) ~= "unassigned" | return lookup_control(codepoint) ~= "unassigned" | ||
end | end | ||
function | function export.is_printable(codepoint) | ||
local result = lookup_control(codepoint) | local result = lookup_control(codepoint) | ||
return (result == "assigned") or (result == "space-separator"), result | return (result == "assigned") or (result == "space-separator"), result | ||
end | end | ||
function | function export.is_whitespace(codepoint) | ||
local result = lookup_control(codepoint) | local result = lookup_control(codepoint) | ||
return (result == "space-separator"), result | return (result == "space-separator"), result | ||
end | end | ||
export.lookup_category = memo_lookup( | |||
"category", | "category", | ||
function (codepoint, category) | function (codepoint, category) | ||
Line 383: | Line 390: | ||
"Cn") | "Cn") | ||
export.lookup_script = memo_lookup( | |||
"scripts", | "scripts", | ||
function (codepoint, | function (codepoint, script) | ||
return | return script | ||
end, | end, | ||
"Zzzz") | "Zzzz") | ||
function export.get_entry_title(codepoint) | |||
if udata.unsupported_title[codepoint] then | |||
return udata.unsupported_title[codepoint] | |||
if | |||
end | end | ||
if lookup_control(codepoint) ~= "assigned" then | |||
return nil | |||
return | |||
end | end | ||
return mw.ustring.char(codepoint) | |||
end | end | ||
return | return export |