mirror of
https://github.com/NotAShelf/watchdog.git
synced 2026-04-19 00:20:05 +00:00
internal/normalize: optimize path normalization via in-place processing & write index
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I3ec8b93833b0220b8ac5919cd0aee9616a6a6964
This commit is contained in:
parent
d1d450ec11
commit
6fed378bb6
1 changed files with 15 additions and 17 deletions
|
|
@ -48,38 +48,36 @@ func (n *PathNormalizer) Normalize(path string) string {
|
||||||
path = "/" + path
|
path = "/" + path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process segments in-place to minimize allocations
|
||||||
// Split into segments, first element is *always* empty for paths starting with '/'
|
// Split into segments, first element is *always* empty for paths starting with '/'
|
||||||
segments := strings.Split(path, "/")
|
segments := strings.Split(path, "/")
|
||||||
if len(segments) > 0 && segments[0] == "" {
|
|
||||||
segments = segments[1:]
|
// Process segments in a single pass: remove empty, resolve . and ..
|
||||||
|
writeIdx := 0
|
||||||
|
for i := 0; i < len(segments); i++ {
|
||||||
|
seg := segments[i]
|
||||||
|
|
||||||
|
// Skip empty segments (from double slashes or leading /)
|
||||||
|
if seg == "" {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove empty segments (from double slashes)
|
|
||||||
filtered := make([]string, 0, len(segments))
|
|
||||||
for _, seg := range segments {
|
|
||||||
if seg != "" {
|
|
||||||
filtered = append(filtered, seg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
segments = filtered
|
|
||||||
|
|
||||||
// Resolve . and .. segments
|
|
||||||
resolved := make([]string, 0, len(segments))
|
|
||||||
for _, seg := range segments {
|
|
||||||
if seg == "." {
|
if seg == "." {
|
||||||
// Skip current directory
|
// Skip current directory
|
||||||
continue
|
continue
|
||||||
} else if seg == ".." {
|
} else if seg == ".." {
|
||||||
// Go up one level if possible
|
// Go up one level if possible
|
||||||
if len(resolved) > 0 {
|
if writeIdx > 0 {
|
||||||
resolved = resolved[:len(resolved)-1]
|
writeIdx--
|
||||||
}
|
}
|
||||||
// If already at root, skip ..
|
// If already at root, skip ..
|
||||||
} else {
|
} else {
|
||||||
resolved = append(resolved, seg)
|
// Keep this segment
|
||||||
|
segments[writeIdx] = seg
|
||||||
|
writeIdx++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
segments = resolved
|
segments = segments[:writeIdx]
|
||||||
|
|
||||||
// Collapse numeric segments
|
// Collapse numeric segments
|
||||||
if n.cfg.CollapseNumericSegments {
|
if n.cfg.CollapseNumericSegments {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue