Compare canonical paths by token spelling

This commit is contained in:
2026-10-09 03:23:08 +00:00
parent edc6660654
commit 9ac65ccc08
5 changed files with 151 additions and 16 deletions

View File

@@ -128,6 +128,45 @@ func clonePathSegments(segments []pathSegment, extra int) []pathSegment {
return cloned
}
func pathTokenSpellingEqual(left, right pathSegment) bool {
switch {
case left.kind == pathSegmentIndex && right.kind == pathSegmentIndex:
return left.index == right.index
case left.kind == pathSegmentIndex:
return pathIndexSpellingEqual(left.index, right.key)
case right.kind == pathSegmentIndex:
return pathIndexSpellingEqual(right.index, left.key)
default:
return left.key == right.key
}
}
func pathIndexSpellingEqual(index int, token string) bool {
var scratch [20]byte
spelling := strconv.AppendInt(scratch[:0], int64(index), 10)
if len(spelling) != len(token) {
return false
}
for i, character := range spelling {
if character != token[i] {
return false
}
}
return true
}
func pathSpellingEqual(left, right Path) bool {
if len(left.segments) != len(right.segments) {
return false
}
for i, segment := range left.segments {
if !pathTokenSpellingEqual(segment, right.segments[i]) {
return false
}
}
return true
}
func escapePathToken(token string) string {
token = strings.ReplaceAll(token, "~", "~0")
return strings.ReplaceAll(token, "/", "~1")