fix column_to_char for tabs
This commit is contained in:
@@ -77,8 +77,12 @@ class Line:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def column_to_char(self, column_in_line: int) -> int:
|
def column_to_char(self, column_in_line: int) -> int:
|
||||||
|
while not column_in_line in self._column_to_char_cache and column_in_line > 0:
|
||||||
|
column_in_line = column_in_line - 1
|
||||||
|
|
||||||
if column_in_line in self._column_to_char_cache:
|
if column_in_line in self._column_to_char_cache:
|
||||||
return self._column_to_char_cache[column_in_line]
|
return self._column_to_char_cache[column_in_line]
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def char_to_column(self, char_in_line: int) -> int:
|
def char_to_column(self, char_in_line: int) -> int:
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ from line import Line
|
|||||||
class MyTestCase(unittest.TestCase):
|
class MyTestCase(unittest.TestCase):
|
||||||
def test_column_to_char(self):
|
def test_column_to_char(self):
|
||||||
byte_offset = 123
|
byte_offset = 123
|
||||||
text = "\tabc\td\tef\tg" # will be rendered as: ....abc.d...ef..g where . represents a whitespace column
|
# will be rendered as:
|
||||||
|
# ....abc.d...ef..g where . represents a whitespace column
|
||||||
|
# 00001234566678991 character index
|
||||||
|
# 01234567890123456 colum index
|
||||||
|
text = "\tabc\td\tef\tg"
|
||||||
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
|
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
|
||||||
|
|
||||||
self.assertEqual(0, line.column_to_char(0)) # the tab
|
self.assertEqual(0, line.column_to_char(0)) # the tab
|
||||||
@@ -99,8 +103,8 @@ class MyTestCase(unittest.TestCase):
|
|||||||
print(text)
|
print(text)
|
||||||
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
|
line = Line(byte_offset=byte_offset, byte_end=byte_offset + len(text.encode("utf8")), line=text)
|
||||||
self.assertEqual(0, line.byte_index_to_char_index(0)) # x
|
self.assertEqual(0, line.byte_index_to_char_index(0)) # x
|
||||||
self.assertEqual(0, line.byte_index_to_char_index(1)) # first byte of diacritical mark belonging to x
|
self.assertEqual(1, line.byte_index_to_char_index(1)) # first byte of diacritical mark belonging to x
|
||||||
self.assertEqual(0, line.byte_index_to_char_index(2)) # second byte of diacritical mark belonging to x
|
self.assertEqual(1, line.byte_index_to_char_index(2)) # second byte of diacritical mark belonging to x
|
||||||
|
|
||||||
def test_diacritical_marks(self):
|
def test_diacritical_marks(self):
|
||||||
text = "̈ẍỏôŏ̮👍🏿"
|
text = "̈ẍỏôŏ̮👍🏿"
|
||||||
|
|||||||
Reference in New Issue
Block a user