Repository: godotengine/godot
File: misc/scripts/char_range_fetch.py (Python)

Code before:
        range_tuple: tuple[int, int] = (int(range_start, 16), int(range_end, 16))

        if char_property == "XID_Start":
            xid_start.append(range_tuple)
        elif char_property == "XID_Continue":
            xid_continue.append(range_tuple)
        elif char_property == "Uppercase":
            uppercase_letter.append(range_tuple)
        elif char_property == "Lowercase":
            lowercase_letter.append(range_tuple)
        elif char_property == "Alphabetic":
            unicode_letter.append(range_tuple)

    # Underscore technically isn't in XID_Start, but for our purposes it's included.
    xid_start.append((0x005F, 0x005F))
    xid_start.sort(key=lambda x: x[0])

    merge_ranges(xid_start)
    merge_ranges(xid_continue)
    merge_ranges(uppercase_letter)
    merge_ranges(lowercase_letter)
    merge_ranges(unicode_letter)


def make_array(array_name: str, range_list: list[tuple[int, int]]) -> str:
    result: str = f"\n\nCharRange {array_name}[{len(range_list)}] = {{\n"

    for start, end in range_list:
        result += f"\t{{ 0x{start:x}, 0x{end:x} }},\n"

    result += "};"

    return result


def generate_char_range_inc() -> None:
    parse_unicode_data()

    source: str = generate_copyright_header("char_range.cpp")

    source += f"""
// This file was generated using the `misc/scripts/char_range_fetch.py` script.

#include "core/string/char_utils.h"

// Unicode Derived Core Properties
// Source: {URL}\
"""

    source += make_array("xid_start", xid_start)
    source += make_array("xid_continue", xid_continue)

Review comment (suggestion):
```suggestion
    result: str = f"\n\nconst int {array_name}_size = {len(range_list)};\n"
    result += f"const CharRange {array_name}[{array_name}_size] = {{\n"
```

Code after:
        range_tuple: tuple[int, int] = (int(range_start, 16), int(range_end, 16))

        if char_property == "XID_Start":
            xid_start.append(range_tuple)
        elif char_property == "XID_Continue":
            xid_continue.append(range_tuple)
        elif char_property == "Uppercase":
            uppercase_letter.append(range_tuple)
        elif char_property == "Lowercase":
            lowercase_letter.append(range_tuple)
        elif char_property == "Alphabetic":
            unicode_letter.append(range_tuple)

    # Underscore technically isn't in XID_Start, but for our purposes it's included.
    xid_start.append((0x005F, 0x005F))
    xid_start.sort(key=lambda x: x[0])

    merge_ranges(xid_start)
    merge_ranges(xid_continue)
    merge_ranges(uppercase_letter)
    merge_ranges(lowercase_letter)
    merge_ranges(unicode_letter)


def make_array(array_name: str, range_list: list[tuple[int, int]]) -> str:
    result: str = f"\n\nconst int {array_name}_size = {len(range_list)};\n"
    result += f"const CharRange {array_name}[{array_name}_size] = {{\n"

    for start, end in range_list:
        result += f"\t{{ 0x{start:x}, 0x{end:x} }},\n"

    result += "};"

    return result


def generate_char_range_inc() -> None:
    parse_unicode_data()

    source: str = generate_copyright_header("char_range.cpp")

    source += f"""
// This file was generated using the `misc/scripts/char_range_fetch.py` script.

#include "core/string/char_utils.h"

// Unicode Derived Core Properties
// Source: {URL}\
"""

    source += make_array("xid_start", xid_start)