path/filepath: reword documentation for Rel

The existing func Rel's API documentation was presented in a rather
dense way without a lot of organization that oriented around topical
flow, so the documentation has been cleaned up to present the
function's behavior more clearly and concisely.

Fixes #75893

Change-Id: I6c8f6ef508250397be9d0127a15508e7335f18c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/712440
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Bypass: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Matt T. Proud 2025-10-16 19:14:45 +02:00 committed by Gopher Robot
parent 39fd61ddb0
commit 6c3d0d259f

View file

@ -173,19 +173,20 @@ func unixAbs(path string) (string, error) {
return Join(wd, path), nil return Join(wd, path), nil
} }
// Rel returns a relative path that is lexically equivalent to targpath when // Rel returns a relative path that is lexically equivalent to targPath when
// joined to basepath with an intervening separator. That is, // joined to basePath with an intervening separator. That is,
// [Join](basepath, Rel(basepath, targpath)) is equivalent to targpath itself. // [Join](basePath, Rel(basePath, targPath)) is equivalent to targPath itself.
// On success, the returned path will always be relative to basepath, //
// even if basepath and targpath share no elements. // The returned path will always be relative to basePath, even if basePath and
// An error is returned if targpath can't be made relative to basepath or if // targPath share no elements. Rel calls [Clean] on the result.
// knowing the current working directory would be necessary to compute it. //
// Rel calls [Clean] on the result. // An error is returned if targPath can't be made relative to basePath
func Rel(basepath, targpath string) (string, error) { // or if knowing the current working directory would be necessary to compute it.
baseVol := VolumeName(basepath) func Rel(basePath, targPath string) (string, error) {
targVol := VolumeName(targpath) baseVol := VolumeName(basePath)
base := Clean(basepath) targVol := VolumeName(targPath)
targ := Clean(targpath) base := Clean(basePath)
targ := Clean(targPath)
if sameWord(targ, base) { if sameWord(targ, base) {
return ".", nil return ".", nil
} }
@ -194,7 +195,7 @@ func Rel(basepath, targpath string) (string, error) {
if base == "." { if base == "." {
base = "" base = ""
} else if base == "" && filepathlite.VolumeNameLen(baseVol) > 2 /* isUNC */ { } else if base == "" && filepathlite.VolumeNameLen(baseVol) > 2 /* isUNC */ {
// Treat any targetpath matching `\\host\share` basepath as absolute path. // Treat any targetpath matching `\\host\share` basePath as absolute path.
base = string(Separator) base = string(Separator)
} }
@ -202,7 +203,7 @@ func Rel(basepath, targpath string) (string, error) {
baseSlashed := len(base) > 0 && base[0] == Separator baseSlashed := len(base) > 0 && base[0] == Separator
targSlashed := len(targ) > 0 && targ[0] == Separator targSlashed := len(targ) > 0 && targ[0] == Separator
if baseSlashed != targSlashed || !sameWord(baseVol, targVol) { if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath) return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
} }
// Position base[b0:bi] and targ[t0:ti] at the first differing elements. // Position base[b0:bi] and targ[t0:ti] at the first differing elements.
bl := len(base) bl := len(base)
@ -228,7 +229,7 @@ func Rel(basepath, targpath string) (string, error) {
t0 = ti t0 = ti
} }
if base[b0:bi] == ".." { if base[b0:bi] == ".." {
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath) return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
} }
if b0 != bl { if b0 != bl {
// Base elements left. Must go up before going down. // Base elements left. Must go up before going down.