Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Index Conversion from Text to TextRef #129

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions python/pycrdt/_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def __contains__(self, item: str) -> bool:
return item in str(self)

def __len__(self) -> int:
with self.doc.transaction() as txn:
return self.integrated.len(txn._txn)
return len(self.__str__())

def __str__(self) -> str:
with self.doc.transaction() as txn:
Expand All @@ -61,6 +60,9 @@ def __iadd__(self, value: str) -> Text:
self.integrated.insert(txn._txn, len(self), value)
return self

def _integrated_len(self, index: int | None = None):
return len(str(self)[:index].encode(encoding="utf-8"))

def _check_slice(self, key: slice) -> tuple[int, int]:
if key.step is not None:
raise RuntimeError("Step not supported")
Expand All @@ -69,13 +71,13 @@ def _check_slice(self, key: slice) -> tuple[int, int]:
elif key.start < 0:
raise RuntimeError("Negative start not supported")
else:
start = key.start
start = self._integrated_len(key.start)
if key.stop is None:
stop = len(self)
stop = self._integrated_len()
elif key.stop < 0:
raise RuntimeError("Negative stop not supported")
else:
stop = key.stop
stop = self._integrated_len(key.stop)
return start, stop

def __delitem__(self, key: int | slice) -> None:
Expand Down
Loading