mirror of
https://codeberg.org/Freedium-cfd/web.git
synced 2026-03-11 09:04:37 +00:00
refactor(mutable_string): remove _is_dirty flag and related methods
This commit is contained in:
parent
047719a040
commit
e1d986fedf
2 changed files with 2 additions and 21 deletions
|
|
@ -2,30 +2,21 @@ from typing import List, Union
|
|||
|
||||
|
||||
class MutableString:
|
||||
__slots__ = ("_string", "_string_list", "_is_dirty")
|
||||
__slots__ = ("_string", "_string_list")
|
||||
|
||||
def __init__(self, string: Union[str, "MutableString"]) -> None:
|
||||
self._string: str = str(string) if isinstance(string, MutableString) else string
|
||||
self._string_list: List[str] = list(self._string)
|
||||
self._is_dirty: bool = False
|
||||
|
||||
@property
|
||||
def string(self) -> str:
|
||||
if self._is_dirty:
|
||||
self._string = "".join(self._string_list)
|
||||
self._is_dirty = False
|
||||
|
||||
return self._string
|
||||
|
||||
def _mark_dirty(self) -> None:
|
||||
self._is_dirty = True
|
||||
return "".join(self._string_list)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._string_list)
|
||||
|
||||
def pop(self, key: int) -> "MutableString":
|
||||
self._string_list.pop(key)
|
||||
self._mark_dirty()
|
||||
return self
|
||||
|
||||
def encode(self, encoding: str) -> bytes:
|
||||
|
|
@ -35,12 +26,10 @@ class MutableString:
|
|||
if key < 0 or key > len(self._string_list):
|
||||
raise IndexError("string index out of range")
|
||||
self._string_list.insert(key, value)
|
||||
self._mark_dirty()
|
||||
return self
|
||||
|
||||
def __setitem__(self, key: Union[int, slice], value: str) -> None:
|
||||
self._string_list[key] = value
|
||||
self._mark_dirty()
|
||||
|
||||
def __getitem__(self, key: int) -> str:
|
||||
return self._string_list[key]
|
||||
|
|
@ -55,4 +44,3 @@ class MutableString:
|
|||
if start < 0 or start >= len(self._string_list):
|
||||
raise IndexError("string index out of range")
|
||||
del self._string_list[start : start + length]
|
||||
self._mark_dirty()
|
||||
|
|
|
|||
|
|
@ -17,13 +17,6 @@ def test_init():
|
|||
assert str(ms3) == ""
|
||||
|
||||
|
||||
def test_string_property():
|
||||
ms = MutableString("test")
|
||||
ms._string_list[0] = "b"
|
||||
ms._mark_dirty()
|
||||
assert ms.string == "best"
|
||||
|
||||
|
||||
def test_len():
|
||||
ms = MutableString("hello")
|
||||
assert len(ms) == 5
|
||||
|
|
|
|||
Loading…
Reference in a new issue