Skip to content

Fix CRLF being counted as two line breaks with preserve_blank_lines - #104

Open
afonsojanu wants to merge 1 commit into
ppannuto:mainfrom
afonsojanu:fix-crlf-preserve-blank-lines
Open

afonsojanu wants to merge 1 commit into
ppannuto:mainfrom
afonsojanu:fix-crlf-preserve-blank-lines

Conversation

@afonsojanu

Copy link
Copy Markdown

Closes #103.

preserve_blank_lines=True splits the input on the character class [\r\n], which matches each of \r and \n separately. A Windows-style \r\n line ending is therefore split into two pieces with an empty string between them, and that empty string turns into an extra blank line once the pieces are joined back together with \n.

>>> titlecase("Line1\r\n\r\nLine2", preserve_blank_lines=True)
'Line1\n\n\n\nLine2'   # expected 'Line1\n\nLine2'

This changes the split to match a full line-ending sequence (\r\n, \r, or \n) instead of any single character in that class, so CRLF is treated as one line break like it should be. The preserve_blank_lines=False branch already used [\r\n]+ and doesn't have this problem, so it's untouched.

Added a test case covering CRLF and lone-CR input under preserve_blank_lines=True, since the existing blank-line tests only exercised \n.

When preserve_blank_lines=True, lines were split on the character
class [\r\n], which treats a Windows CRLF as two separate line
endings instead of one. Every \r\n in the input therefore produced
an extra blank line in the output (a single line break could turn
into up to four newlines).

Split on the line-ending sequence \r\n|\r|\n instead, so CRLF, lone
CR, and LF are all treated as one line break each. The
preserve_blank_lines=False path already used [\r\n]+ and was not
affected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

preserve_blank_lines=True counts each CRLF as two line breaks

1 participant