-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
idna: Process the unicode UTS46-mappings into something usable in C++
- Loading branch information
1 parent
dcff638
commit 61136fe
Showing
4 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# SPDX-FileCopyrightText: 2023 Robin Lindén <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
import sys | ||
import textwrap | ||
import typing | ||
|
||
|
||
def expand_code_point_ranges(ranges: [str]) -> [str]: | ||
expanded = [] | ||
for r in ranges: | ||
if ".." in r: | ||
start, end = r.split("..") | ||
expanded.extend(c for c in range(int(start, 16), int(end, 16) + 1)) | ||
else: | ||
expanded.append(int(r, 16)) | ||
return expanded | ||
|
||
|
||
class IDNA: | ||
def __init__(self): | ||
self.disallowed = set() | ||
self.disallowed_std3_valid = set() | ||
self.disallowed_std3_mapped = dict() | ||
self.ignored = set() | ||
self.mapped = dict() | ||
self.deviation = dict() | ||
self.valid = set() | ||
|
||
# Valid in UTS46, but not in IDNA2008. | ||
self.valid_nv8 = set() | ||
self.valid_xv8 = set() | ||
|
||
# https://www.unicode.org/reports/tr46/#Table_Data_File_Fields | ||
@staticmethod | ||
def from_table(table_rows: [str]) -> typing.Self: | ||
idna = IDNA() | ||
for row in table_rows: | ||
# Drop the trailing comment about what code point this is. | ||
row = row.split("#")[0].strip() | ||
|
||
cols = [col.strip() for col in row.split(";")] | ||
# Some rows are blank or just a comment. | ||
if len(cols) <= 1: | ||
continue | ||
|
||
action = cols[1] | ||
if action == "disallowed": | ||
assert len(cols) == 2 | ||
idna.disallowed.add(cols[0]) | ||
elif action == "disallowed_STD3_valid": | ||
assert len(cols) == 2 | ||
idna.disallowed_std3_valid.add(cols[0]) | ||
elif action == "disallowed_STD3_mapped": | ||
assert len(cols) == 3 | ||
idna.disallowed_std3_mapped[cols[0]] = cols[2] | ||
elif action == "ignored": | ||
assert len(cols) == 2 | ||
idna.ignored.add(cols[0]) | ||
elif action == "mapped": | ||
assert len(cols) == 3 | ||
idna.mapped[cols[0]] = cols[2] | ||
elif action == "deviation": | ||
assert len(cols) == 3 | ||
idna.deviation[cols[0]] = cols[2] | ||
elif action == "valid" and len(cols) == 2: | ||
idna.valid.add(cols[0]) | ||
elif action == "valid" and len(cols) == 4 and cols[3] == "NV8": | ||
idna.valid_nv8.add(cols[0]) | ||
elif action == "valid" and len(cols) == 4 and cols[3] == "XV8": | ||
idna.valid_xv8.add(cols[0]) | ||
else: | ||
raise Exception(f"Unable to parse data: {cols}") | ||
|
||
idna.disallowed = expand_code_point_ranges(idna.disallowed) | ||
idna.ignored = expand_code_point_ranges(idna.ignored) | ||
return idna | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 2: | ||
print( | ||
f"Usage: {sys.argv[0]} <IdnaMappingTable.txt>", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) | ||
|
||
with open(sys.argv[1]) as table: | ||
idna = IDNA.from_table(table.readlines()) | ||
|
||
print( | ||
textwrap.dedent( | ||
f"""\ | ||
// SPDX-FileCopyrightText: 2023 Robin Lindén <[email protected]> | ||
// | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// This file is generated. Do not touch it. | ||
#ifndef IDNA_IDNA_DATA_H_ | ||
#define IDNA_IDNA_DATA_H_ | ||
// clang-format off | ||
#include <array> | ||
namespace idna::uts46 {{ | ||
constexpr std::array<char32_t, {len(idna.disallowed)}> disallowed = {{ | ||
{", ".join(hex(c) for c in idna.disallowed)} | ||
}}; | ||
constexpr std::array<char32_t, {len(idna.ignored)}> ignored = {{ | ||
{", ".join(hex(c) for c in idna.ignored)} | ||
}}; | ||
}} // namespace idna::uts46 | ||
// clang-format on | ||
#endif | ||
""" | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters