Build a translate mapping that replaces halfwidth and fullwidth forms with their standard-width forms.
()
| 231 | |
| 232 | |
| 233 | def _build_width_map() -> dict[int, str]: |
| 234 | """ |
| 235 | Build a translate mapping that replaces halfwidth and fullwidth forms |
| 236 | with their standard-width forms. |
| 237 | """ |
| 238 | # Though it's not listed as a fullwidth character, we'll want to convert |
| 239 | # U+3000 IDEOGRAPHIC SPACE to U+20 SPACE on the same principle, so start |
| 240 | # with that in the dictionary. |
| 241 | width_map = {0x3000: " "} |
| 242 | for i in range(0xFF01, 0xFFF0): |
| 243 | char = chr(i) |
| 244 | alternate = unicodedata.normalize("NFKC", char) |
| 245 | if alternate != char: |
| 246 | width_map[i] = alternate |
| 247 | return width_map |
| 248 | |
| 249 | |
| 250 | WIDTH_MAP = _build_width_map() |