0.2.0-dev Download ZIP
116 lines ·3.1 KB Raw
| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Script used to dump case mappings from |
| 4 | # the Unicode Character Database to the `ucaps.h` file. |
| 5 | # NOTE: This script is deliberately not integrated into the build system; |
| 6 | # you should run it manually whenever you want to update the data. |
| 7 | import os |
| 8 | import sys |
| 9 | from typing import Final |
| 10 | from urllib.request import urlopen |
| 11 | |
| 12 | if __name__ == "__main__": |
| 13 | sys.path.insert(1, os.path.join(os.path.dirname(__file__), "../../")) |
| 14 | |
| 15 | from methods import generate_copyright_header |
| 16 | |
| 17 | URL: Final[str] = "https://www.unicode.org/Public/18.0.0/ucd/UnicodeData.txt" |
| 18 | |
| 19 | |
| 20 | lower_to_upper: list[tuple[str, str]] = [] |
| 21 | upper_to_lower: list[tuple[str, str]] = [] |
| 22 | |
| 23 | |
| 24 | def parse_unicode_data() -> None: |
| 25 | lines: list[str] = [line.decode("utf-8") for line in urlopen(URL)] |
| 26 | |
| 27 | for line in lines: |
| 28 | split_line: list[str] = line.split(";") |
| 29 | |
| 30 | code_value: str = split_line[0].strip() |
| 31 | uppercase_mapping: str = split_line[12].strip() |
| 32 | lowercase_mapping: str = split_line[13].strip() |
| 33 | |
| 34 | if uppercase_mapping: |
| 35 | lower_to_upper.append((f"0x{code_value}", f"0x{uppercase_mapping}")) |
| 36 | if lowercase_mapping: |
| 37 | upper_to_lower.append((f"0x{code_value}", f"0x{lowercase_mapping}")) |
| 38 | |
| 39 | |
| 40 | def make_cap_table(table_name: str, len_name: str, table: list[tuple[str, str]]) -> str: |
| 41 | result: str = f"static const int {table_name}[{len_name}][2] = {{\n" |
| 42 | |
| 43 | for first, second in table: |
| 44 | result += f"\t{{ {first}, {second} }},\n" |
| 45 | |
| 46 | result += "};\n\n" |
| 47 | |
| 48 | return result |
| 49 | |
| 50 | |
| 51 | def generate_ucaps_fetch() -> None: |
| 52 | parse_unicode_data() |
| 53 | |
| 54 | source: str = generate_copyright_header("ucaps.h") |
| 55 | |
| 56 | source += f""" |
| 57 | #pragma once |
| 58 | |
| 59 | // This file was generated using the `misc/scripts/ucaps_fetch.py` script. |
| 60 | |
| 61 | #define LTU_LEN {len(lower_to_upper)} |
| 62 | #define UTL_LEN {len(upper_to_lower)}\n\n""" |
| 63 | |
| 64 | source += make_cap_table("caps_table", "LTU_LEN", lower_to_upper) |
| 65 | source += make_cap_table("reverse_caps_table", "UTL_LEN", upper_to_lower) |
| 66 | |
| 67 | source += """inline int _find_upper(int p_char) { |
| 68 | \tint low = 0; |
| 69 | \tint high = LTU_LEN - 1; |
| 70 | \tint middle; |
| 71 | |
| 72 | \twhile (low <= high) { |
| 73 | \t\tmiddle = (low + high) / 2; |
| 74 | |
| 75 | \t\tif (p_char < caps_table[middle][0]) { |
| 76 | \t\t\thigh = middle - 1; // Search low end of array. |
| 77 | \t\t} else if (caps_table[middle][0] < p_char) { |
| 78 | \t\t\tlow = middle + 1; // Search high end of array. |
| 79 | \t\t} else { |
| 80 | \t\t\treturn caps_table[middle][1]; |
| 81 | \t\t} |
| 82 | \t} |
| 83 | |
| 84 | \treturn p_char; |
| 85 | } |
| 86 | |
| 87 | inline int _find_lower(int p_char) { |
| 88 | \tint low = 0; |
| 89 | \tint high = UTL_LEN - 1; |
| 90 | \tint middle; |
| 91 | |
| 92 | \twhile (low <= high) { |
| 93 | \t\tmiddle = (low + high) / 2; |
| 94 | |
| 95 | \t\tif (p_char < reverse_caps_table[middle][0]) { |
| 96 | \t\t\thigh = middle - 1; // Search low end of array. |
| 97 | \t\t} else if (reverse_caps_table[middle][0] < p_char) { |
| 98 | \t\t\tlow = middle + 1; // Search high end of array. |
| 99 | \t\t} else { |
| 100 | \t\t\treturn reverse_caps_table[middle][1]; |
| 101 | \t\t} |
| 102 | \t} |
| 103 | |
| 104 | \treturn p_char; |
| 105 | } |
| 106 | """ |
| 107 | |
| 108 | ucaps_path: str = os.path.join(os.path.dirname(__file__), "../../core/string/ucaps.h") |
| 109 | with open(ucaps_path, "w", newline="\n") as f: |
| 110 | f.write(source) |
| 111 | |
| 112 | print("`ucaps.h` generated successfully.") |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | generate_ucaps_fetch() |