Skip to content

Commit

Permalink
added default value to iohelper.get_newline
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Nov 20, 2024
1 parent 3daade4 commit 7b51978
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cat_win/src/service/helper/iohelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def yield_file(src_file: Path, binary: bool = False,
file.close()

@staticmethod
def get_newline(file: Path) -> str:
def get_newline(file: Path, default: str = '\n') -> str:
"""
determines the line ending of a given file.
Expand All @@ -202,10 +202,10 @@ def get_newline(file: Path) -> str:
try:
with open(file, 'rb') as _f:
_l = _f.readline()
_l += b'\n' * bool(not _l[-1:] or _l[-1:] not in b'\r\n')
return '\r\n' if _l[-2:] == b'\r\n' else _l[-1:].decode()
_l+= default.encode() * (not _l[-1:] or _l[-1:] not in b'\r\n')
return _l[-1-(_l[-2:] == b'\r\n'):].decode()
except OSError:
return '\n'
return default


@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions cat_win/tests/src/service/helper/test_iohelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def test_get_newline(self):
self.assertEqual(IoHelper.get_newline(test_file_path), '\r\n')
self.assertEqual(IoHelper.get_newline(test_file_path_empty), '\n')
self.assertEqual(IoHelper.get_newline(test_file_path_oneline), '\n')
self.assertEqual(IoHelper.get_newline(test_file_path, 'x'), '\r\n')
self.assertEqual(IoHelper.get_newline(test_file_path_empty, 'x'), 'x')
self.assertEqual(IoHelper.get_newline(test_file_path_oneline, 'x'), 'x')

def test_get_stdin_content_oneline(self):
stdin_mock.set_content('hello\nworld')
Expand Down

0 comments on commit 7b51978

Please sign in to comment.