[New-bugs-announce] [issue17629] Expose string width to Python
report at bugs.python.org
Wed Apr 3 23:54:31 CEST 2013
New submission from Chris Angelico:
As of PEP 393, a string's width is recorded in its header - effectively, a marker that says whether the highest codepoint in the string is >0xFFFF, >0xFF, or <=0xFF. This is, on some occasions, useful to know; for instance, when testing string performance, it's handy to be able to very quickly throw something down that, without scanning the contents of all the strings used, can identify the width spread.
A similar facility is provided by Pike, which has a similar flexible string representation: http://pike.lysator.liu.se/generated/manual/modref/ex/7.2_3A_3A/String/width.html accessible to a script as String.width().
Since this is not something frequently needed, it would make sense to hide it away in the sys or inspect modules, or possibly in strings or as a method on the string itself.
Currently, the best way to do this is something like:
for ch in map(ord,s):
if n > 0xFFFF: return 4
if n > 0xFF: width=2
which necessitates a scan of the entire string, unless it has an astral character.
components: Library (Lib)
title: Expose string width to Python
versions: Python 3.4, Python 3.5
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce