No description
Find a file
Andrew Hamilton 6ac7d0e3c7
Some checks failed
CI / build (macos-latest, 3.11) (push) Has been cancelled
CI / build (macos-latest, 3.12) (push) Has been cancelled
CI / build (macos-latest, 3.13) (push) Has been cancelled
CI / build (macos-latest, 3.14) (push) Has been cancelled
CI / build (ubuntu-latest, 3.11) (push) Has been cancelled
CI / build (ubuntu-latest, 3.12) (push) Has been cancelled
CI / build (ubuntu-latest, 3.13) (push) Has been cancelled
CI / build (ubuntu-latest, 3.14) (push) Has been cancelled
CI / build (windows-latest, 3.11) (push) Has been cancelled
CI / build (windows-latest, 3.12) (push) Has been cancelled
CI / build (windows-latest, 3.13) (push) Has been cancelled
CI / build (windows-latest, 3.14) (push) Has been cancelled
CI / build-limited-api (macos-latest) (push) Has been cancelled
CI / build-limited-api (ubuntu-latest) (push) Has been cancelled
CI / build-limited-api (windows-latest) (push) Has been cancelled
Linters / black (push) Has been cancelled
Linters / codespell (push) Has been cancelled
Declare freethreading compatibility (Py_mod_gil_not_used)
2026-08-03 09:31:49 +10:00
.github CI: fix syntax 2025-11-01 17:58:59 +01:00
cwcwidth Declare freethreading compatibility (Py_mod_gil_not_used) 2026-08-03 09:31:49 +10:00
docs Fix typo 2025-02-09 21:51:27 +01:00
tests Skip test on macos 2025-10-28 09:10:15 +01:00
.clang-format Support up to Unicode 15.1 in fallback implementation (fixes #17) 2024-01-14 10:20:10 +01:00
.gitignore Ignore more artefacts 2021-01-23 15:02:21 +01:00
CHANGELOG.md Bump version to 0.1.12 2025-11-01 18:23:42 +01:00
LICENSE Bump copyright years to -2025 2025-02-09 21:49:31 +01:00
MANIFEST.in Include _impl.c again 2021-02-07 23:38:10 +01:00
pyproject.toml Supported builds with limited API 2025-11-01 17:55:11 +01:00
README.md Supported builds with limited API 2025-11-01 17:55:11 +01:00
setup.cfg Bump version to 0.1.12 2025-11-01 18:23:42 +01:00
setup.py Supported builds with limited API 2025-11-01 17:55:11 +01:00

Python bindings for wc(s)width

cwcwidth provides Python bindings for wcwidth and wcswidth functions defined in POSIX.1-2001 and POSIX.1-2008 based on Cython. These functions compute the printable length of a unicode character/string on a terminal. The module provides the same functions as wcwidth and its behavior is compatible.

On systems not conforming to POSIX.1-2001 and POSIX.1-2008, Markus Kuhn's implementation is used to provide the functionality.

Dependencies

  • Cython >= 3.1 (optional, only for building). If Cython is not available, the C files are not regenerated from their source.

Quick installation guide

cwcwidth can be installed via pip:

pip install cwcwidth

or by running:

python3 setup.py install

In case the extension should be built with the limited Python API, set the environment variable USE_LIMITED_API to any value.

Usage

>>> import cwcwidth
>>> cwcwidth.wcwidth("a")
1
>>> cwcwidth.wcswidth("コ")
2
>>> cwcwidth.wcswidth("コンニチハ, セカイ!")
19
>>> cwcwidth.wcswidth("コンニチハ, セカイ!", 5)
10

Comparison with wcwidth

>>> import wcwidth, cwcwidth, timeit
>>> timeit.timeit(lambda: wcwidth.wcswidth("コンニチハ, セカイ!"))
19.14463168097427
>>> timeit.timeit(lambda: cwcwidth.wcswidth("コンニチハ, セカイ!"))
0.16294104099506512

License

The code is licensed under the MIT license.