Skip to content

Commit

Permalink
🔥 merge align_to into apply
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 19, 2024
1 parent 087997f commit df72877
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/tarina/_string_c.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class String:
left_index: int
offset: int
next_index: int
len: int
text: str

def __init__(self, text: str): ...
Expand All @@ -65,12 +66,10 @@ class String:

def val(self) -> str: ...

def apply(self) -> None: ...
def apply(self, left: int | None = None): ...

def rest(self) -> str: ...

def align_to(self, index: int) -> None: ...

@property
def complete(self) -> bool: ...

Expand Down
21 changes: 10 additions & 11 deletions src/tarina/_string_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ cdef class String:
cdef public Py_ssize_t left_index
cdef public Py_ssize_t next_index
cdef public Py_ssize_t offset
cdef Py_ssize_t _len
cdef public Py_ssize_t len
cdef public str text

def __init__(self, str text):
self.text = text
self._len = PyUnicode_GET_LENGTH(text)
self.len = PyUnicode_GET_LENGTH(text)
self.left_index = 0
self.offset = 0
self.next_index = 0
Expand All @@ -219,24 +219,23 @@ cdef class String:
def val(self):
return PyUnicode_Substring(self.text, self.left_index, self.next_index - self.offset)

def apply(self):
self.left_index = self.next_index
def apply(self, int left = None):
if left is None:
self.left_index = self.next_index
else:
self.next_index = self.left_index = left
self.offset = 0

def rest(self):
return PyUnicode_Substring(self.text, self.left_index, self._len)

def align_to(self, int index):
self.next_index = self.left_index = index
self.offset = 0
return PyUnicode_Substring(self.text, self.left_index, self.len)

@property
def complete(self):
return self.left_index == self._len
return self.left_index == self.len

@property
def will_complete(self):
return self.next_index == self._len
return self.next_index == self.len

def __repr__(self):
return f"String({self.text!r}[{self.left_index}:{self.next_index - self.offset}])"
Expand Down
19 changes: 9 additions & 10 deletions src/tarina/_string_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ class String:
left_index: int
offset: int
next_index: int
_len: int
len: int
text: str

def __init__(self, text: str):
self.text = text
self._len = len(text)
self.len = len(text)
self.left_index = 0
self.offset = 0
self.next_index = 0
Expand All @@ -212,24 +212,23 @@ def step(self, separator: str, crlf: bool = True):
def val(self):
return self.text[self.left_index:self.next_index - self.offset]

def apply(self):
self.left_index = self.next_index
def apply(self, left: int | None = None):
if left is None:
self.left_index = self.next_index
else:
self.next_index = self.left_index = left
self.offset = 0

def rest(self):
return self.text[self.left_index:]

def align_to(self, index: int):
self.next_index = self.left_index = index
self.offset = 0

@property
def complete(self):
return self.left_index == self._len
return self.left_index == self.len

@property
def will_complete(self):
return self.next_index == self._len
return self.next_index == self.len

def __repr__(self):
return f"String({self.text!r}[{self.left_index}:{self.next_index - self.offset}])"
Expand Down

0 comments on commit df72877

Please sign in to comment.