Skip to content

Commit

Permalink
Refactor char_widths generation code.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Jan 13, 2025
1 parent 748caeb commit 728b948
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
"""Build cython extensions and generated files."""

import sys
from itertools import groupby

from Cython.Build import cythonize
from setuptools import setup
from setuptools.command.build_py import build_py
from wcwidth import wcwidth

WIDTHS_FILE_HEADER = """\
\"""
_CHAR_WIDTHS_DOC = '''\
"""
Character widths as generated by wcwidth.
Each tuple in `CHAR_WIDTHS` represents an interval of ords with common width, i.e.,
`(1, 31, 0)` means ords 1 through 31 (inclusive) have width 0. Intervals with width 1
`(0, 31, 0)` means ords 0 through 31 (inclusive) have width 0. Intervals with width 1
are omitted so if an ord doesn't belong to any interval we can assume it has width 1.
\"""
"""
'''


def _create_char_widths():
"""Build ``_char_widths.py``."""
groups = []
start = end = 0
group_width = 1
for codepoint in range(sys.maxunicode + 1):
char_width = max(wcwidth(chr(codepoint)), 0)
if char_width != group_width:
if group_width != 1:
groups.append((start, end, group_width))
group_width = char_width
start = end = codepoint
else:
end = codepoint

def make_widths_table():
"""Build char widths file."""
widths = (
(codepoint, wcwidth(chr(codepoint))) for codepoint in range(sys.maxunicode + 1)
)
grouped = groupby(widths, key=lambda tup: tup[1])
with open("src/batgrl/_char_widths.py", "w") as file:
file.write(WIDTHS_FILE_HEADER)
file.write("\n")
file.write(_CHAR_WIDTHS_DOC)
file.write("CHAR_WIDTHS = (\n")

for width, group in grouped:
if width == 1:
continue
if width == -1:
width = 0
first_codepoint, _ = next(group)
last_codepoint = first_codepoint
for last_codepoint, _ in group:
pass

file.write(f" ({first_codepoint}, {last_codepoint}, {width}),\n")
for group in groups:
file.write(" ({}, {}, {}),\n".format(*group))
file.write(")\n")


Expand All @@ -50,7 +48,7 @@ class build_py_with_char_widths(build_py):
def run(self):
"""Generate width file on build."""
super().run()
make_widths_table()
_create_char_widths()


setup(
Expand Down

0 comments on commit 728b948

Please sign in to comment.