1
0
mirror of https://github.com/golang/go synced 2024-11-20 10:14:43 -07:00

godoc: fix ToAbsolute mapping

The implementation of splitFirst was broken;
splitFirst("foo/") must be the same as splitFirst("foo").
As a result, ToAbsolute could be simplified, and as a side
effect this fixes a long-standing bug.

Thanks to Luca Greco <luca.greco@alcacoop.it> for doing
the investigation.

Fixes #1157.

R=rsc
CC=golang-dev
https://golang.org/cl/5278050
This commit is contained in:
Robert Griesemer 2011-10-18 10:28:30 -07:00
parent 9d99d52fcb
commit 0e4d1c3e2c

View File

@ -139,13 +139,18 @@ func (m *Mapping) Fprint(w io.Writer) {
} }
} }
// splitFirst splits a path at the first path separator and returns
// the path's head (the top-most directory specified by the path) and
// its tail (the rest of the path). If there is no path separator,
// splitFirst returns path as head, and the the empty string as tail.
// Specifically, splitFirst("foo") == splitFirst("foo/").
//
func splitFirst(path string) (head, tail string) { func splitFirst(path string) (head, tail string) {
i := strings.Index(path, string(filepath.Separator)) if i := strings.Index(path, string(filepath.Separator)); i > 0 {
if i > 0 {
// 0 < i < len(path) // 0 < i < len(path)
return path[0:i], path[i+1:] return path[0:i], path[i+1:]
} }
return "", path return path, ""
} }
// ToAbsolute maps a slash-separated relative path to an absolute filesystem // ToAbsolute maps a slash-separated relative path to an absolute filesystem
@ -156,20 +161,14 @@ func (m *Mapping) ToAbsolute(spath string) string {
fpath := filepath.FromSlash(spath) fpath := filepath.FromSlash(spath)
prefix, tail := splitFirst(fpath) prefix, tail := splitFirst(fpath)
for _, e := range m.list { for _, e := range m.list {
switch { if e.prefix == prefix {
case e.prefix == prefix: // found potential mapping
// use tail abspath := filepath.Join(e.path, tail)
case e.prefix == "": if _, err := fs.Stat(abspath); err == nil {
tail = fpath return abspath
default: }
continue // no match
}
abspath := filepath.Join(e.path, tail)
if _, err := fs.Stat(abspath); err == nil {
return abspath
} }
} }
return "" // no match return "" // no match
} }