Skip to content

Commit 9a0bdb7

Browse files
grokifyclaude
andcommitted
feat(fs): add NormalizeLineEndings for CRLF to LF conversion
Useful for normalizing text files that may have Windows line endings, especially when files are checked out with CRLF via git autocrlf. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c00a57b commit 9a0bdb7

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

fs/fs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,10 @@ func PathHasPrefix(path, prefix string) bool {
238238
}
239239
return strings.HasPrefix(strings.ToLower(p)+"/", strings.ToLower(pfx))
240240
}
241+
242+
// NormalizeLineEndings converts Windows line endings (CRLF) to Unix (LF).
243+
// This is useful when reading text files that may have been created on Windows
244+
// or when files are checked out with CRLF line endings on Windows via git.
245+
func NormalizeLineEndings(s string) string {
246+
return strings.ReplaceAll(s, "\r\n", "\n")
247+
}

fs/fs_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,30 @@ func TestBirthtimeNonExistent(t *testing.T) {
470470
t.Error("Birthtime() on non-existent file should return error")
471471
}
472472
}
473+
474+
func TestNormalizeLineEndings(t *testing.T) {
475+
tests := []struct {
476+
name string
477+
input string
478+
want string
479+
}{
480+
{"empty", "", ""},
481+
{"no line endings", "hello world", "hello world"},
482+
{"unix line endings", "line1\nline2\nline3", "line1\nline2\nline3"},
483+
{"windows line endings", "line1\r\nline2\r\nline3", "line1\nline2\nline3"},
484+
{"mixed line endings", "line1\r\nline2\nline3\r\n", "line1\nline2\nline3\n"},
485+
{"only CRLF", "\r\n", "\n"},
486+
{"multiple CRLF", "\r\n\r\n\r\n", "\n\n\n"},
487+
{"trailing CRLF", "hello\r\n", "hello\n"},
488+
{"CR only preserved", "line1\rline2", "line1\rline2"},
489+
}
490+
491+
for _, tt := range tests {
492+
t.Run(tt.name, func(t *testing.T) {
493+
got := fs.NormalizeLineEndings(tt.input)
494+
if got != tt.want {
495+
t.Errorf("NormalizeLineEndings(%q) = %q, want %q", tt.input, got, tt.want)
496+
}
497+
})
498+
}
499+
}

0 commit comments

Comments
 (0)