2019-01-07 22:49:20 +00:00
|
|
|
# Needs UnicodeData.txt in the current directory.
|
|
|
|
#
|
|
|
|
# It can be obtained from unicode.org:
|
|
|
|
# - http://www.unicode.org/Public/<VERSION>/ucd/UnicodeData.txt
|
|
|
|
#
|
|
|
|
# If executed as a script, it will generate the contents of the file
|
2022-05-06 23:08:51 +00:00
|
|
|
# python3 scripts/generate_unicode_tolower.py header > `src/base/unicode/tolower.h`,
|
|
|
|
# python3 scripts/generate_unicode_tolower.py data > `src/base/unicode/tolower_data.h`.
|
2019-01-07 22:49:20 +00:00
|
|
|
|
2022-05-06 18:31:24 +00:00
|
|
|
import sys
|
2022-05-06 23:21:58 +00:00
|
|
|
import unicode
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
|
|
def generate_cases():
|
2020-12-02 14:22:26 +00:00
|
|
|
ud = unicode.data()
|
|
|
|
return [(unicode.unhex(u["Value"]), unicode.unhex(u["Simple_Lowercase_Mapping"])) for u in ud if u["Simple_Lowercase_Mapping"]]
|
2019-01-07 22:49:20 +00:00
|
|
|
|
2022-05-06 18:31:24 +00:00
|
|
|
def gen_header(cases):
|
2022-06-12 11:15:02 +00:00
|
|
|
print(f"""\
|
2023-02-28 19:41:07 +00:00
|
|
|
#include <cstdint>
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
|
|
struct UPPER_LOWER
|
|
|
|
{{
|
|
|
|
\tint32_t upper;
|
|
|
|
\tint32_t lower;
|
|
|
|
}};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{{
|
2022-06-12 11:15:02 +00:00
|
|
|
\tNUM_TOLOWER = {len(cases)},
|
2019-01-07 22:49:20 +00:00
|
|
|
}};
|
|
|
|
|
2022-06-12 11:15:02 +00:00
|
|
|
extern const struct UPPER_LOWER tolowermap[];""")
|
2022-05-06 18:31:24 +00:00
|
|
|
|
2022-05-06 23:08:51 +00:00
|
|
|
def gen_data(cases):
|
2022-05-06 18:31:24 +00:00
|
|
|
print("""\
|
|
|
|
#ifndef TOLOWER_DATA
|
|
|
|
#error "This file must only be included in `tolower.cpp`"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const struct UPPER_LOWER tolowermap[] = {""")
|
2020-12-02 14:22:26 +00:00
|
|
|
for upper_code, lower_code in cases:
|
2022-06-12 11:15:02 +00:00
|
|
|
print(f"\t{{{upper_code}, {lower_code}}},")
|
2020-12-02 14:22:26 +00:00
|
|
|
print("};")
|
2019-01-07 22:49:20 +00:00
|
|
|
|
2022-05-06 18:31:24 +00:00
|
|
|
def main():
|
|
|
|
cases = generate_cases()
|
|
|
|
|
|
|
|
header = "header" in sys.argv
|
2022-05-06 23:08:51 +00:00
|
|
|
data = "data" in sys.argv
|
2022-05-06 18:31:24 +00:00
|
|
|
|
|
|
|
if header:
|
|
|
|
gen_header(cases)
|
2022-05-06 23:08:51 +00:00
|
|
|
elif data:
|
|
|
|
gen_data(cases)
|
2022-05-06 18:31:24 +00:00
|
|
|
|
2019-01-07 22:49:20 +00:00
|
|
|
if __name__ == '__main__':
|
2020-12-02 14:22:26 +00:00
|
|
|
main()
|