-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
83 lines (70 loc) · 2.78 KB
/
path.go
File metadata and controls
83 lines (70 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package goverhaul
import (
"path/filepath"
"strings"
)
// NormalizePath converts a path to use forward slashes consistently
// regardless of the operating system and cleans the path.
// It removes redundant separators, dot-segments, and normalizes separators to forward slashes.
// This ensures consistent path handling across different operating systems.
// Empty paths remain empty to maintain backward compatibility.
func NormalizePath(path string) string {
// Special case: empty path should remain empty
if path == "" {
return ""
}
// Clean the path to handle dot-segments and redundant separators
cleaned := filepath.Clean(path)
// Replace all backslashes with forward slashes
return strings.ReplaceAll(cleaned, "\\", "/")
}
// JoinPaths joins path elements and normalizes the result.
// It joins elements using the system's path separator, then normalizes to forward slashes,
// and cleans the path to remove redundant separators and dot-segments.
// This function works on all operating systems by normalizing paths to use forward slashes.
func JoinPaths(elem ...string) string {
return NormalizePath(filepath.Join(elem...))
}
// IsSubPath checks if childPath is a subdirectory of parentPath.
// Both paths are normalized before comparison.
// This function works on all operating systems by normalizing paths to use forward slashes.
func IsSubPath(parentPath, childPath string) bool {
normalizedParent := NormalizePath(parentPath)
normalizedChild := NormalizePath(childPath)
// Handle empty paths
if normalizedParent == "" || normalizedParent == "." {
return true // Empty parent means any path is a subpath
}
// Check for exact match first
if normalizedParent == normalizedChild {
return true
}
// Ensure paths end with slash for proper prefix matching
if !strings.HasSuffix(normalizedParent, "/") {
normalizedParent += "/"
}
return strings.HasPrefix(normalizedChild, normalizedParent)
}
// IsAbsPath checks if a path is absolute
// This function works on all operating systems by using the system's path separator
func IsAbsPath(path string) bool {
return filepath.IsAbs(path)
}
// AbsPath returns the absolute path for a given path
// If an error occurs, it returns the original path
// This function works on all operating systems by normalizing paths to use forward slashes
func AbsPath(path string) string {
absPath, err := filepath.Abs(path)
if err != nil {
return path
}
return NormalizePath(absPath)
}
// DirPath returns the directory portion of a path
// This function works on all operating systems by normalizing paths to use forward slashes
func DirPath(path string) string {
// Normalize the path first to ensure consistent handling
normalizedPath := NormalizePath(path)
// Use filepath.Dir and normalize the result
return NormalizePath(filepath.Dir(normalizedPath))
}