Calculate the extra amount of width space the given source code segment might take if it were to be displayed on a fixed width output device. Supports wide unicode characters and emojis.
(line, offset)
| 631 | _WIDE_CHAR_SPECIFIERS = "WF" |
| 632 | |
| 633 | def _display_width(line, offset): |
| 634 | """Calculate the extra amount of width space the given source |
| 635 | code segment might take if it were to be displayed on a fixed |
| 636 | width output device. Supports wide unicode characters and emojis.""" |
| 637 | |
| 638 | # Fast track for ASCII-only strings |
| 639 | if line.isascii(): |
| 640 | return offset |
| 641 | |
| 642 | import unicodedata |
| 643 | |
| 644 | return sum( |
| 645 | 2 if unicodedata.east_asian_width(char) in _WIDE_CHAR_SPECIFIERS else 1 |
| 646 | for char in line[:offset] |
| 647 | ) |
| 648 | |
| 649 | |
| 650 |
no test coverage detected