Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pathological.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package pathologize
// https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words

import (
"path/filepath"
"regexp"
"strings"
"unicode/utf8"
Expand All @@ -41,12 +40,14 @@ var (
)

func CleanPath(path string) string {
pathParts := strings.Split(path, string(filepath.Separator))
// Normalize backslashes to forward slashes so both separator styles work
// consistently on all platforms.
path = strings.ReplaceAll(path, "\\", "/")
pathParts := strings.Split(path, "/")
for i, part := range pathParts {
pathParts[i] = Clean(part)
}

return strings.Join(pathParts, string(filepath.Separator))
return strings.Join(pathParts, "/")
}

func IsClean(filename string) bool {
Expand Down
2 changes: 1 addition & 1 deletion pathological_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func Test_paths(t *testing.T) {
{"short", "foo", "foo"},
{"long", "foo.bar.baz", "foo.bar.baz"},
{"several dirs", "foo/bar/baz", "foo/bar/baz"},
{"several dirs2", "foo\\bar\\baz", "foobarbaz"},
{"several dirs2", "foo\\bar\\baz", "foo/bar/baz"},
{"dirs with spaces", "foo bar/baz", "foo bar/baz"},
{"dirs with bad chars", "foo*bar/baz", "foobar/baz"},
{"example", "foo/bar:baz*qux", "foo/barbazqux"},
Expand Down