Selfie
Loading...
Searching...
No Matches
LineReader.py
Go to the documentation of this file.
1import io
2from typing import Optional
3
4
5def _to_unix(s: str) -> str:
6 if s.find("\r\n") == -1:
7 return s
8 else:
9 return s.replace("\r\n", "\n")
10
11
13 def __init__(self, content: bytes):
14 self.__buffer = io.BytesIO(content)
16 self.__line_count = 0 # Initialize line count
17
18 @classmethod
19 def for_binary(cls, content: bytes):
20 return cls(content)
21
22 @classmethod
23 def for_string(cls, content: str):
24 return cls(content.encode("utf-8"))
25
26 def __detect_newline_type(self) -> bool:
27 first_line = self.__buffer.readline()
28 self.__buffer.seek(0) # Reset buffer for actual reading
29 return b"\r\n" not in first_line
30
31 def unix_newlines(self) -> bool:
32 return self.__uses_unix_newlines
33
34 def read_line(self) -> Optional[str]:
35 line_bytes = self.__buffer.readline()
36 if line_bytes == b"":
37 return None
38 else:
39 self.__line_count += 1 # Increment line count for each line read
40 line = line_bytes.decode("utf-8")
41 return line.rstrip("\r\n" if not self.__uses_unix_newlines else "\n")
42
43 # Method to get the current line number
44 def get_line_number(self) -> int:
45 return self.__line_count
__init__(self, bytes content)
Definition LineReader.py:13
for_binary(cls, bytes content)
Definition LineReader.py:19
Optional[str] read_line(self)
Definition LineReader.py:34