Skip to content

Commit

Permalink
fix: only split lines at Python's universal newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jan 3, 2025
1 parent 7f68ae0 commit e2cff51
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/darkgraylib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def detect_newline(string: str) -> str:
return "\n"


def normalize_newlines(string: str) -> str:
"""Normalize newlines in a string to LF"""
return io.IncrementalNewlineDecoder(None, True).decode(string)


def splitlines(string: str) -> list[str]:
"""Split a string into lines at universal newlines."""
if not string:
return []
return (
normalize_newlines(string) # Normalize newlines to LF
.rstrip("\n") # Remove trailing newline
.split("\n") # Split into lines
)


class TextDocument:
"""Store & handle a multi-line text document, either as a string or list of lines"""

Expand Down Expand Up @@ -65,7 +81,7 @@ def encoded_string(self) -> bytes:
def lines(self) -> TextLines:
"""Return the document as a list of lines converting and caching if necessary"""
if self._lines is None:
self._lines = tuple((self._string or "").splitlines())
self._lines = tuple(splitlines(self._string or ""))
return self._lines

@property
Expand Down

0 comments on commit e2cff51

Please sign in to comment.