Skip to content

Commit 9012f89

Browse files
authored
On UCS-2 builds py27 handle low surrogate chars GH342
On UCS-2 builds of Python 2.7-3.2, handle low surrogate characters.
2 parents 5508c2f + b280a73 commit 9012f89

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

cpplint/cpplint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import string
5252
import sys
5353
import unicodedata
54+
import sysconfig
5455

5556
try:
5657
xrange # Python 2
@@ -4291,6 +4292,16 @@ def GetLineWidth(line):
42914292
if unicodedata.east_asian_width(uc) in ('W', 'F'):
42924293
width += 2
42934294
elif not unicodedata.combining(uc):
4295+
# Issue 337
4296+
# https://mail.python.org/pipermail/python-list/2012-August/628809.html
4297+
if (sys.version_info.major, sys.version_info.minor) <= (3, 2):
4298+
# https://github.com/python/cpython/blob/2.7/Include/unicodeobject.h#L81
4299+
is_wide_build = sysconfig.get_config_var("Py_UNICODE_SIZE") >= 4
4300+
# https://github.com/python/cpython/blob/2.7/Objects/unicodeobject.c#L564
4301+
is_low_surrogate = 0xDC00 <= ord(uc) <= 0xDFFF
4302+
if not is_wide_build and is_low_surrogate:
4303+
width -= 1
4304+
42944305
width += 1
42954306
return width
42964307
else:

cpplint/cpplint_unittest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ def testGetLineWidth(self):
321321
self.assertEquals(0, cpplint.GetLineWidth(''))
322322
self.assertEquals(10, cpplint.GetLineWidth(u'x' * 10))
323323
self.assertEquals(16, cpplint.GetLineWidth(u'都|道|府|県|支庁'))
324+
self.assertEquals(5 + 13 + 9, cpplint.GetLineWidth(
325+
u'd𝐱/dt' + u'f : t ⨯ 𝐱 → ℝ' + u't ⨯ 𝐱 → ℝ'))
324326

325327
def testGetTextInside(self):
326328
self.assertEquals('', cpplint._GetTextInside('fun()', r'fun\('))

0 commit comments

Comments
 (0)