Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/misc/scripts/ucaps_fetch.py
9896 views
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
8
import os
9
import sys
10
from typing import Final, List, Tuple
11
from urllib.request import urlopen
12
13
if __name__ == "__main__":
14
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "../../"))
15
16
from methods import generate_copyright_header
17
18
URL: Final[str] = "https://www.unicode.org/Public/16.0.0/ucd/UnicodeData.txt"
19
20
21
lower_to_upper: List[Tuple[str, str]] = []
22
upper_to_lower: List[Tuple[str, str]] = []
23
24
25
def parse_unicode_data() -> None:
26
lines: List[str] = [line.decode("utf-8") for line in urlopen(URL)]
27
28
for line in lines:
29
split_line: List[str] = line.split(";")
30
31
code_value: str = split_line[0].strip()
32
uppercase_mapping: str = split_line[12].strip()
33
lowercase_mapping: str = split_line[13].strip()
34
35
if uppercase_mapping:
36
lower_to_upper.append((f"0x{code_value}", f"0x{uppercase_mapping}"))
37
if lowercase_mapping:
38
upper_to_lower.append((f"0x{code_value}", f"0x{lowercase_mapping}"))
39
40
41
def make_cap_table(table_name: str, len_name: str, table: List[Tuple[str, str]]) -> str:
42
result: str = f"static const int {table_name}[{len_name}][2] = {{\n"
43
44
for first, second in table:
45
result += f"\t{{ {first}, {second} }},\n"
46
47
result += "};\n\n"
48
49
return result
50
51
52
def generate_ucaps_fetch() -> None:
53
parse_unicode_data()
54
55
source: str = generate_copyright_header("ucaps.h")
56
57
source += f"""
58
#pragma once
59
60
// This file was generated using the `misc/scripts/ucaps_fetch.py` script.
61
62
#define LTU_LEN {len(lower_to_upper)}
63
#define UTL_LEN {len(upper_to_lower)}\n\n"""
64
65
source += make_cap_table("caps_table", "LTU_LEN", lower_to_upper)
66
source += make_cap_table("reverse_caps_table", "UTL_LEN", upper_to_lower)
67
68
source += """static int _find_upper(int ch) {
69
\tint low = 0;
70
\tint high = LTU_LEN - 1;
71
\tint middle;
72
73
\twhile (low <= high) {
74
\t\tmiddle = (low + high) / 2;
75
76
\t\tif (ch < caps_table[middle][0]) {
77
\t\t\thigh = middle - 1; // Search low end of array.
78
\t\t} else if (caps_table[middle][0] < ch) {
79
\t\t\tlow = middle + 1; // Search high end of array.
80
\t\t} else {
81
\t\t\treturn caps_table[middle][1];
82
\t\t}
83
\t}
84
85
\treturn ch;
86
}
87
88
static int _find_lower(int ch) {
89
\tint low = 0;
90
\tint high = UTL_LEN - 1;
91
\tint middle;
92
93
\twhile (low <= high) {
94
\t\tmiddle = (low + high) / 2;
95
96
\t\tif (ch < reverse_caps_table[middle][0]) {
97
\t\t\thigh = middle - 1; // Search low end of array.
98
\t\t} else if (reverse_caps_table[middle][0] < ch) {
99
\t\t\tlow = middle + 1; // Search high end of array.
100
\t\t} else {
101
\t\t\treturn reverse_caps_table[middle][1];
102
\t\t}
103
\t}
104
105
\treturn ch;
106
}
107
"""
108
109
ucaps_path: str = os.path.join(os.path.dirname(__file__), "../../core/string/ucaps.h")
110
with open(ucaps_path, "w", newline="\n") as f:
111
f.write(source)
112
113
print("`ucaps.h` generated successfully.")
114
115
116
if __name__ == "__main__":
117
generate_ucaps_fetch()
118
119