Module:User:Theknightwho/scripts

local byte = string.byte local insert = table.insert local u = require("Module:string/char")

local function advance(patterns, b)	patterns[b] = patterns[b] or {} return patterns[b] end

local function add_range(patterns, b1, b2) if b1 > b2 then return end local i = 0 while true do		i = i + 1 local r = patterns[i] if not r then insert(patterns, b1) return insert(patterns, b2) elseif b2 == r or b2 == r - 1 then patterns[i] = b1			return elseif r > b2 then insert(patterns, i, b1) return insert(patterns, i + 1, b2) end i = i + 1 r = patterns[i] if b1 == r or b1 == r + 1 then patterns[i] = b2			return end end end

local function do_boundary_byte(ch, patterns, head, finish, b, bound1, bound2, offset) while true do		if finish == head then return add_range(patterns, b, b)		elseif ch[finish] ~= bound1 then break end finish = finish - 1 end for i = head + 1, finish do		patterns = advance(patterns, b)		b = ch[i] local b_offset = i == finish and b or b + offset if b_offset < bound2 then -- No range to add if equal. add_range(patterns, b_offset, bound2) elseif bound2 < b_offset then add_range(patterns, bound2, b_offset) end end end

local byte_changes = {0x80, 0x800, 0x10000}

local function iterate(char1, char2, patterns) if char1 > char2 then return end for i = 1, #byte_changes do		local change = byte_changes[i] if char2 >= change and char1 < change then iterate(char1, change - 1, patterns) return iterate(change, char2, patterns) end end char1, char2 = {byte(u(char1), 1, 4)}, {byte(u(char2), 1, 4)} local patterns, head, char_len, b = patterns, 1, #char1, char1[1] while head ~= char_len and b == char2[head] do		patterns = advance(patterns, b)		head = head + 1 b = char1[head] end local b1, b2 = char1[head], char2[head] add_range(patterns, b1 + 1, b2 - 1) do_boundary_byte(char1, patterns, head, char_len, b1, 128, 191, 1) do_boundary_byte(char2, patterns, head, char_len, b2, 191, 128, -1) end

local function make_patterns(ranges) local patterns = {} for i = 2, #ranges, 2 do		iterate(ranges[i - 1], ranges[i], patterns) end return patterns end

return make_patterns(require("Module:scripts/data").Hani.ranges)