diff --git a/pathological.go b/pathological.go index e987caf..36938a7 100644 --- a/pathological.go +++ b/pathological.go @@ -22,7 +22,6 @@ package pathologize // https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words import ( - "path/filepath" "regexp" "strings" "unicode/utf8" @@ -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 { diff --git a/pathological_test.go b/pathological_test.go index 2692216..baa67f5 100644 --- a/pathological_test.go +++ b/pathological_test.go @@ -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"},