This is faster by O(n) to O(n²) compared to the previous algorithm that would re-compute the column for each call.
107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
from typing import Dict
|
|
|
|
import constants
|
|
|
|
|
|
class Line:
|
|
def __init__(self, byte_offset: int, byte_end: int, line: str):
|
|
self._byte_offset = byte_offset
|
|
self._byte_end = byte_end
|
|
self._line = line
|
|
self._char_to_column_cache = self._cache_char_to_column()
|
|
|
|
def byte_offset(self) -> int:
|
|
return self._byte_offset
|
|
|
|
def byte_end(self) -> int:
|
|
return self._byte_end
|
|
|
|
def line(self) -> str:
|
|
return self._line
|
|
|
|
def length_in_charaters(self) -> int:
|
|
return len(self._line)
|
|
|
|
def length_in_columns(self) -> int:
|
|
return self.char_to_column(len(self._line))
|
|
|
|
def char_index_to_byte(self, char_in_line: int) -> int:
|
|
return len(self.prefix(char_in_line).encode("utf8"))
|
|
|
|
def byte_index_to_char_index(self, byte_index: int) -> int:
|
|
prefix_bytes = self._line.encode("utf8")[:byte_index]
|
|
prefix_chars = prefix_bytes.decode("utf8", errors="ignore")
|
|
return len(prefix_chars)
|
|
|
|
def line_tabs_replaced(self):
|
|
line = self._line;
|
|
i = 0
|
|
offset = 0
|
|
result = ""
|
|
length = len(line)
|
|
while True:
|
|
tab_index = line.find("\t", offset)
|
|
if tab_index < 0:
|
|
break
|
|
result = result + line[offset:tab_index]
|
|
result = result + " " * (constants.tab_width - len(result) % constants.tab_width)
|
|
offset = tab_index + 1
|
|
|
|
result = result + line[offset:]
|
|
|
|
return result
|
|
|
|
def column_to_char(self, column_in_line: int) -> int:
|
|
i = 0
|
|
result = 0
|
|
while i < column_in_line:
|
|
char = self._line[result]
|
|
if char == "\t":
|
|
i = i + constants.tab_width - i % constants.tab_width # jump the additional columns to complete the tab
|
|
if i > column_in_line:
|
|
break;
|
|
else:
|
|
i = i + 1
|
|
result = result + 1
|
|
|
|
return result
|
|
|
|
def char_to_column(self, char_in_line: int) -> int:
|
|
if not char_in_line in self._char_to_column_cache:
|
|
# print("%d in %s" % (char_in_line, self._char_to_column_cache))
|
|
return -1
|
|
return self._char_to_column_cache[char_in_line]
|
|
|
|
def _cache_char_to_column(self) -> Dict[int, int]:
|
|
char_to_column_cache = {}
|
|
result = 0
|
|
i = 0
|
|
char_to_column_cache[0] = 0
|
|
while i < len(self._line):
|
|
if i < len(self._line) and self._line[i] == "\t":
|
|
result = result + constants.tab_width - result % constants.tab_width
|
|
else:
|
|
result = result + 1
|
|
i = i + 1
|
|
char_to_column_cache[i] = result
|
|
return char_to_column_cache
|
|
|
|
def includes_byte(self, byte: int) -> bool:
|
|
return self._byte_offset <= byte <= self._byte_end
|
|
|
|
def intersects(self, start_byte: int, end_byte: int):
|
|
result = start_byte < self._byte_end and end_byte > self._byte_offset
|
|
# print("%d,%d in %d,%d" % (start_byte, end_byte, self._byte_offset, self._byte_end))
|
|
return result
|
|
|
|
def prefix(self, index: int) -> str:
|
|
return self._line[0:index]
|
|
|
|
def substr(self, offset: int, length: int) -> str:
|
|
return self._line[offset:offset+length]
|
|
|
|
def suffix(self, index: int) -> str:
|
|
return self._line[index:]
|
|
|
|
def __str__(self):
|
|
return "%s (%d->%d)" % (self._line, self._byte_offset, self._byte_end) |