From 728b948e7f621bb4a710ad09c5bdcd1e3d0afa77 Mon Sep 17 00:00:00 2001 From: salt-die Date: Mon, 13 Jan 2025 16:39:05 -0600 Subject: [PATCH] Refactor char_widths generation code. --- setup.py | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/setup.py b/setup.py index ada2de40..345038c4 100644 --- a/setup.py +++ b/setup.py @@ -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") @@ -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(