| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# # Copyright © 2025 Pierre Le Marre <[email protected]> |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
""" |
| 7 |
Utils to parse the Unicode database files |
| 8 |
""" |
| 9 |
|
| 10 |
from collections.abc import Callable, Iterator |
| 11 |
from dataclasses import dataclass |
| 12 |
from pathlib import Path |
| 13 |
import sys |
| 14 |
from typing import ClassVar, Self |
| 15 |
|
| 16 |
|
| 17 |
def parse_code_point(raw: str) -> int | None: |
| 18 |
return None if not raw else int(raw, 16) |
| 19 |
|
| 20 |
|
| 21 |
@dataclass |
| 22 |
class CodePointRange: |
| 23 |
start: int |
| 24 |
end: int |
| 25 |
|
| 26 |
def __iter__(self) -> Iterator[int]: |
| 27 |
yield from range(self.start, self.end + 1) |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def parse(cls, raw: str) -> Self: |
| 31 |
start, *end = raw.strip().split("..") |
| 32 |
return cls( |
| 33 |
start=int(start, 16), end=int(start, 16) if not end else int(end[0], 16) |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
@dataclass |
| 38 |
class PropertyEntry: |
| 39 |
code_point: int |
| 40 |
property: str |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def parse_file( |
| 44 |
cls, path: Path, filter: Callable[[str], bool] | None = None |
| 45 |
) -> Iterator[Self]: |
| 46 |
with path.open("rt", encoding="utf-8") as fd: |
| 47 |
for line in fd: |
| 48 |
# Remove comment |
| 49 |
line, *_ = line.split("#") |
| 50 |
line = line.strip() |
| 51 |
# Skip empty lines |
| 52 |
if not line: |
| 53 |
continue |
| 54 |
raw_range, property, *_ = line.split(";") |
| 55 |
range = CodePointRange.parse(raw_range) |
| 56 |
property = property.strip() |
| 57 |
if filter and not filter(property): |
| 58 |
continue |
| 59 |
for code_point in range: |
| 60 |
yield cls(code_point=code_point, property=property) |
| 61 |
|
| 62 |
|
| 63 |
@dataclass |
| 64 |
class UnicodeDataEntry: |
| 65 |
code_point: int |
| 66 |
general_category: str |
| 67 |
lower_case: int | None |
| 68 |
upper_case: int | None |
| 69 |
title_case: int | None |
| 70 |
|
| 71 |
@classmethod |
| 72 |
def parse_file(cls, path: Path) -> Iterator[Self]: |
| 73 |
with path.open("rt", encoding="utf-8") as fd: |
| 74 |
for line in fd: |
| 75 |
line = line.strip() |
| 76 |
if not line or line.startswith("#"): |
| 77 |
continue |
| 78 |
( |
| 79 |
cp, |
| 80 |
_name, |
| 81 |
general_category, |
| 82 |
_cc, |
| 83 |
_bc, |
| 84 |
_d, |
| 85 |
_decimal, |
| 86 |
_digit, |
| 87 |
_numeric, |
| 88 |
_mirrored, |
| 89 |
_, |
| 90 |
_, |
| 91 |
upper_case, |
| 92 |
lower_case, |
| 93 |
title_case, |
| 94 |
*_, |
| 95 |
) = line.split(";") |
| 96 |
code_point = int(cp, 16) |
| 97 |
yield cls( |
| 98 |
code_point=code_point, |
| 99 |
general_category=general_category, |
| 100 |
lower_case=parse_code_point(lower_case), |
| 101 |
upper_case=parse_code_point(upper_case), |
| 102 |
title_case=parse_code_point(title_case), |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
@dataclass |
| 107 |
class DB: |
| 108 |
lower_case: set[int] |
| 109 |
upper_case: set[int] |
| 110 |
title_case: set[int] |
| 111 |
lower_case_mappings: dict[int, int] |
| 112 |
upper_case_mappings: dict[int, int] |
| 113 |
title_case_mappings: dict[int, int] |
| 114 |
|
| 115 |
case_properties: ClassVar[frozenset[str]] = frozenset(("Lowercase", "Uppercase")) |
| 116 |
|
| 117 |
@classmethod |
| 118 |
def filter_case_properties(cls, property: str) -> bool: |
| 119 |
return property in cls.case_properties |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def parse_ucd(cls, path: Path) -> Self: |
| 123 |
lower_case: set[int] = set() |
| 124 |
upper_case: set[int] = set() |
| 125 |
title_case: set[int] = set() |
| 126 |
|
| 127 |
lower_case_mappings: dict[int, int] = {} |
| 128 |
upper_case_mappings: dict[int, int] = {} |
| 129 |
title_case_mappings: dict[int, int] = {} |
| 130 |
|
| 131 |
for entry in UnicodeDataEntry.parse_file(path / "UnicodeData.txt"): |
| 132 |
if entry.general_category == "Lt": |
| 133 |
title_case.add(entry.code_point) |
| 134 |
if entry.lower_case is not None: |
| 135 |
lower_case_mappings[entry.code_point] = entry.lower_case |
| 136 |
if entry.upper_case is not None: |
| 137 |
upper_case_mappings[entry.code_point] = entry.upper_case |
| 138 |
if entry.title_case is not None: |
| 139 |
title_case_mappings[entry.code_point] = entry.title_case |
| 140 |
|
| 141 |
for entry in PropertyEntry.parse_file( |
| 142 |
path / "DerivedCoreProperties.txt", filter=cls.filter_case_properties |
| 143 |
): |
| 144 |
match entry.property: |
| 145 |
case "Lowercase": |
| 146 |
lower_case.add(entry.code_point) |
| 147 |
case "Uppercase": |
| 148 |
upper_case.add(entry.code_point) |
| 149 |
case _: |
| 150 |
raise ValueError(entry) |
| 151 |
|
| 152 |
return cls( |
| 153 |
lower_case=lower_case, |
| 154 |
upper_case=upper_case, |
| 155 |
title_case=title_case, |
| 156 |
lower_case_mappings=lower_case_mappings, |
| 157 |
upper_case_mappings=upper_case_mappings, |
| 158 |
title_case_mappings=title_case_mappings, |
| 159 |
) |
| 160 |
|
| 161 |
def isULowercase(self, cp: int) -> bool: |
| 162 |
return cp in self.lower_case |
| 163 |
|
| 164 |
def isUUppercase(self, cp: int) -> bool: |
| 165 |
return cp in self.upper_case |
| 166 |
|
| 167 |
def istitle(self, cp: int) -> bool: |
| 168 |
return cp in self.title_case |
| 169 |
|
| 170 |
def tolower(self, cp_or_char: int | str) -> int | str: |
| 171 |
cp = cp_or_char if isinstance(cp_or_char, int) else ord(cp_or_char) |
| 172 |
mapping = self.lower_case_mappings.get(cp, cp) |
| 173 |
return mapping if isinstance(cp_or_char, int) else chr(mapping) |
| 174 |
|
| 175 |
def toupper(self, cp_or_char: int | str) -> int | str: |
| 176 |
cp = cp_or_char if isinstance(cp_or_char, int) else ord(cp_or_char) |
| 177 |
mapping = self.upper_case_mappings.get(cp, cp) |
| 178 |
return mapping if isinstance(cp_or_char, int) else chr(mapping) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__": |
| 182 |
# Test |
| 183 |
import icu |
| 184 |
|
| 185 |
c = icu.Locale.createFromName("C") |
| 186 |
icu.Locale.setDefault(c) |
| 187 |
|
| 188 |
path = Path(sys.argv[1]) |
| 189 |
db = DB.parse_ucd(path) |
| 190 |
|
| 191 |
for cp in range(0, 0x10FFFF + 1): |
| 192 |
assert db.isULowercase(cp) == icu.DB.isULowercase(cp), ( |
| 193 |
cp, |
| 194 |
db.isULowercase(cp), |
| 195 |
icu.DB.isULowercase(cp), |
| 196 |
) |
| 197 |
assert db.isUUppercase(cp) == icu.DB.isUUppercase(cp), cp |
| 198 |
assert db.istitle(cp) == icu.DB.istitle(cp), cp |
| 199 |
assert db.tolower(cp) == icu.DB.tolower(cp) |
| 200 |
assert db.toupper(cp) == icu.DB.toupper(cp) |