Skip to content

fix: avoid spurious deleted line when diffing empty string#33

Closed
Yanhu007 wants to merge 1 commit intokylelemons:masterfrom
Yanhu007:fix/diff-empty-string
Closed

fix: avoid spurious deleted line when diffing empty string#33
Yanhu007 wants to merge 1 commit intokylelemons:masterfrom
Yanhu007:fix/diff-empty-string

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #22

Problem

Diff("", "hello") produces:

-
+hello

The spurious - line appears because strings.Split("", "\n") returns [""] (a slice containing one empty string), not [].

Fix

Add a splitLines helper that returns nil for empty input, so the diff engine treats an empty string as having zero lines instead of one empty line.

Testing

All existing tests pass. Manually verified:

Diff("", "hello\nworld") → "+hello\n+world"  // no spurious - line
Diff("hello\nworld", "") → "-hello\n-world"  // clean
Diff("", "") → ""                             // empty

strings.Split("", "\n") returns [""] (one empty string), not [].
This causes Diff("", content) to always show a removed blank line.

Add a splitLines helper that returns nil for empty input.

Fixes kylelemons#22
@Yanhu007
Copy link
Copy Markdown
Author

Duplicate of #34, closing this one.

@Yanhu007 Yanhu007 closed this Apr 15, 2026
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.

diff.Diff() with an empty value produces one blank line

1 participant