Skip to content

Commit 6c3d0d2

Browse files
matttproudgopherbot
authored andcommitted
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>
1 parent 39fd61d commit 6c3d0d2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/path/filepath/path.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,20 @@ func unixAbs(path string) (string, error) {
173173
return Join(wd, path), nil
174174
}
175175

176-
// Rel returns a relative path that is lexically equivalent to targpath when
177-
// joined to basepath with an intervening separator. That is,
178-
// [Join](basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
179-
// On success, the returned path will always be relative to basepath,
180-
// even if basepath and targpath share no elements.
181-
// An error is returned if targpath can't be made relative to basepath or if
182-
// knowing the current working directory would be necessary to compute it.
183-
// Rel calls [Clean] on the result.
184-
func Rel(basepath, targpath string) (string, error) {
185-
baseVol := VolumeName(basepath)
186-
targVol := VolumeName(targpath)
187-
base := Clean(basepath)
188-
targ := Clean(targpath)
176+
// Rel returns a relative path that is lexically equivalent to targPath when
177+
// joined to basePath with an intervening separator. That is,
178+
// [Join](basePath, Rel(basePath, targPath)) is equivalent to targPath itself.
179+
//
180+
// The returned path will always be relative to basePath, even if basePath and
181+
// targPath share no elements. Rel calls [Clean] on the result.
182+
//
183+
// An error is returned if targPath can't be made relative to basePath
184+
// or if knowing the current working directory would be necessary to compute it.
185+
func Rel(basePath, targPath string) (string, error) {
186+
baseVol := VolumeName(basePath)
187+
targVol := VolumeName(targPath)
188+
base := Clean(basePath)
189+
targ := Clean(targPath)
189190
if sameWord(targ, base) {
190191
return ".", nil
191192
}
@@ -194,15 +195,15 @@ func Rel(basepath, targpath string) (string, error) {
194195
if base == "." {
195196
base = ""
196197
} else if base == "" && filepathlite.VolumeNameLen(baseVol) > 2 /* isUNC */ {
197-
// Treat any targetpath matching `\\host\share` basepath as absolute path.
198+
// Treat any targetpath matching `\\host\share` basePath as absolute path.
198199
base = string(Separator)
199200
}
200201

201202
// Can't use IsAbs - `\a` and `a` are both relative in Windows.
202203
baseSlashed := len(base) > 0 && base[0] == Separator
203204
targSlashed := len(targ) > 0 && targ[0] == Separator
204205
if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
205-
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
206+
return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
206207
}
207208
// Position base[b0:bi] and targ[t0:ti] at the first differing elements.
208209
bl := len(base)
@@ -228,7 +229,7 @@ func Rel(basepath, targpath string) (string, error) {
228229
t0 = ti
229230
}
230231
if base[b0:bi] == ".." {
231-
return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
232+
return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
232233
}
233234
if b0 != bl {
234235
// Base elements left. Must go up before going down.

0 commit comments

Comments
 (0)