Skip to content

Commit

Permalink
Fix readline method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Aug 6, 2024
1 parent 5fc08b7 commit bf746cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/multicsv/subtextio.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class allows for convenient and isolated operations within a given
--------------------
- `read(size: int = -1) -> str`: Reads a specified number of
characters from the buffer.
- `readline() -> str`: Reads and returns one line from the buffer.
- `readlines() -> List[str]`: Reads and returns all remaining
lines from the buffer.
- `readline(limit: int = - 1) -> str`: Reads and returns one line
from the buffer.
- `readlines(hint: int = - 1) -> List[str]`: Reads and returns all
remaining lines from the buffer.
- `write(s: str) -> int`: Writes a string to the buffer.
- `writelines(lines: List[str]) -> None`: Writes a list of lines
to the buffer.
Expand Down Expand Up @@ -122,7 +123,7 @@ def read(self, size: int = -1) -> str:
self.position += len(result)
return result

def readline(self) -> str:
def readline(self, limit: int = -1) -> str:
if self._closed:
raise IOError("Closed.")

Expand All @@ -131,12 +132,13 @@ def readline(self) -> str:

newline_pos = self._buffer.find('\n', self.position)
if newline_pos == -1 or newline_pos >= self.length:
result = self._buffer[self.position:]
self.position = self.length
else:
result = self._buffer[self.position:newline_pos + 1]
self.position = newline_pos + 1
newline_pos = self.length

if limit < 0 or limit > newline_pos - self.position:
limit = newline_pos - self.position + 1

result = self._buffer[self.position:self.position + limit]
self.position += len(result)
return result

def readlines(self) -> List[str]:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_subtextio.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,22 @@ def test_iterate(base_textio):
for line in sub_text:
expected_line = next(actual_lines)
assert line == expected_line

def test_readline_with_limit(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.readline(limit=3) == "Wor"
assert sub_text.readline(limit=4) == "ld,\n"
assert sub_text.readline(limit=4) == "this"
assert sub_text.readline(limit=4) == " is "
assert sub_text.readline(limit=4) == ""
assert sub_text.readline(limit=4) == ""

def test_readline_with_huge_limit(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.readline(limit=100) == "World,\n"
assert sub_text.readline(limit=100) == "this is "
assert sub_text.readline(limit=100) == ""

def test_readline_with_zero_limit(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.readline(limit=0) == ""

0 comments on commit bf746cf

Please sign in to comment.