Determines the width of the line in column positions. Args: line: A string, which may be a Unicode string. Returns: The width of the line in column positions, accounting for Unicode combining characters and wide characters.
(line)
| 4773 | |
| 4774 | |
| 4775 | def GetLineWidth(line): |
| 4776 | """Determines the width of the line in column positions. |
| 4777 | |
| 4778 | Args: |
| 4779 | line: A string, which may be a Unicode string. |
| 4780 | |
| 4781 | Returns: |
| 4782 | The width of the line in column positions, accounting for Unicode |
| 4783 | combining characters and wide characters. |
| 4784 | """ |
| 4785 | if isinstance(line, unicode): |
| 4786 | width = 0 |
| 4787 | for uc in unicodedata.normalize('NFC', line): |
| 4788 | if unicodedata.east_asian_width(uc) in ('W', 'F'): |
| 4789 | width += 2 |
| 4790 | elif not unicodedata.combining(uc): |
| 4791 | # Issue 337 |
| 4792 | # https://mail.python.org/pipermail/python-list/2012-August/628809.html |
| 4793 | if (sys.version_info.major, sys.version_info.minor) <= (3, 2): |
| 4794 | # https://github.com/python/cpython/blob/2.7/Include/unicodeobject.h#L81 |
| 4795 | is_wide_build = sysconfig.get_config_var("Py_UNICODE_SIZE") >= 4 |
| 4796 | # https://github.com/python/cpython/blob/2.7/Objects/unicodeobject.c#L564 |
| 4797 | is_low_surrogate = 0xDC00 <= ord(uc) <= 0xDFFF |
| 4798 | if not is_wide_build and is_low_surrogate: |
| 4799 | width -= 1 |
| 4800 | |
| 4801 | width += 1 |
| 4802 | return width |
| 4803 | else: |
| 4804 | return len(line) |
| 4805 | |
| 4806 | |
| 4807 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
no test coverage detected