Fix double newline in Println with trailing newlines#279
Open
veeceey wants to merge 1 commit intofatih:mainfrom
Open
Fix double newline in Println with trailing newlines#279veeceey wants to merge 1 commit intofatih:mainfrom
veeceey wants to merge 1 commit intofatih:mainfrom
Conversation
When using Println or SprintlnFunc with strings that already contain trailing newlines, the output would have double newlines - one from the input and one added by Println. This changes sprintln() to use TrimRight instead of TrimSuffix, which removes all trailing newlines from the formatted string before the Println functions add their own newline. This ensures consistent single-newline output regardless of whether the input has trailing newlines. Updated TestIssue218 to reflect the new normalized behavior. Added TestTrailingNewline to verify the fix. Fixes fatih#241
Author
Manual Test ResultsRan comprehensive tests to verify the fix works correctly: Test 1 and 3 show the fix working - no more double newlines. The output is now consistent regardless of whether the input has trailing newlines. All package tests pass: |
Author
|
gentle ping — still hoping to get this one in. let me know if you'd like any changes |
Author
|
any feedback on this? let me know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #241
Problem
When using
Println()orSprintlnFunc()with strings that already contain trailing newlines, the output would have double newlines:This happened because:
\nfmt.Sprintln()adds another\nTrimSuffixonly removes the one added bySprintln, leaving the originalPrintlnadds yet another\nviafmt.FprintlnSolution
Changed
sprintln()helper to useTrimRightinstead ofTrimSuffix. This removes ALL trailing newlines from the formatted string, ensuringPrintlnfunctions always add exactly one newline regardless of input.Changes
sprintln()to usestrings.TrimRight()to strip all trailing newlinesTestIssue218to reflect the new normalized behaviorTestTrailingNewlinewith comprehensive test casesTesting
All existing tests pass. Manual verification:
The behavior now matches user expectations -
Printlnalways produces exactly one trailing newline, making it more predictable when working with dynamic strings.