Module:Sandbox/Erutuon: Difference between revisions

Content deleted Content added
m method for removing unnecessary fields (and metatable)
check length of private-use subtag; make "throw" method returnable
Line 362:
self.invalid = table.concat(self.input, "-", index)
self:remove_unnecessary_fields()
return self
end
function parsed_subtags_mt:remove_unnecessary_fields()
Line 404 ⟶ 405:
-- characters and hyphen-minus.
if not tag:find '^[A-Za-z0-9-]+$' then
return parsed_subtags:throw("invalid characters", 1)
return parsed_subtags
end
Line 464:
if #potential_subtags > matched_count then
-- Not all potential subtags were matched. TheCheck unmatchedfor tailprivate-use end of the tagsubtags.
-- (after the subtag at the index last_matched_subtag_i) is a
-- private-use subtag if it starts with "x". Otherwise, the tag is
-- invalid.
-- Remove the "x-"?
-- https://tools.ietf.org/html/bcp47#section-2.2.7
local private_use_tag_start_indices = {}
Line 477 ⟶ 473:
end
if not private_use_tag_start_indices[1] then
return parsed_subtags:throw("invalid subtag", last_matched_subtag_i + 1)
return parsed_subtags
end
for i, subtag_index in pairs(private_use_tag_start_indices) do
local next_subtag_index = private_use_tag_start_indices[i + 1] or #potential_subtags + 1
-- Private-use subtags consist of "x-" followed by a1 sequenceto of8 alphanumeric characters.
if next_subtag_index - subtag_index ~= 2 then
return parsed_subtags:throw("invalid subtag", subtag_index)
return parsed_subtags
end
-- Generate private_use table only if needed.
parsed_subtags.private_use = parsed_subtags.private_use or {}
local subtag = table.concat(potential_subtags, "-",
table.insert(parsed_subtags.private_use,
subtag_index,
table.concat(potential_subtags, "-",
and private_use_tag_start_indices[i + 1] - 1))
subtag_index,
and private_use_tag_start_indices[i + 1] - 1)
if not (3 <= #subtag and #subtag <= 10) then -- "u-x" - "u-xxxxxxxx"
and private_use_tag_start_indices[i + 1] - 1))
return parsed_subtags:throw("length of private-use subtag out of range",
private_use_tag_start_indices[i + 1])
end
table.insert(parsed_subtags.private_use, subtag)
end
end
if not (parsed_subtags.language or parsed_subtags.private_use) then
return parsed_subtags:throw("no language subtag", 1)
end