fix column_to_char for tabs

This commit is contained in:
2022-05-07 15:23:35 +02:00
parent 7d9a2d0648
commit b525a0978d
2 changed files with 11 additions and 3 deletions

View File

@@ -77,8 +77,12 @@ class Line:
return result
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:
return self._column_to_char_cache[column_in_line]
return 0
def char_to_column(self, char_in_line: int) -> int:

View File

@@ -11,7 +11,11 @@ from line import Line
class MyTestCase(unittest.TestCase):
def test_column_to_char(self):
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)
self.assertEqual(0, line.column_to_char(0)) # the tab
@@ -99,8 +103,8 @@ class MyTestCase(unittest.TestCase):
print(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(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(1)) # first 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):
text = "̈ẍỏôŏ̮👍🏿"