godoc: Strip "/src/pkg/" prefix from identifier keys, so that it's keyed identically to import count.
Intern the documentation strings.

LGTM=sameer
R=sameer, bradfitz
CC=golang-codereviews
https://golang.org/cl/107250043
diff --git a/godoc/godoc.go b/godoc/godoc.go
index 5b9f95f..dd574dc 100644
--- a/godoc/godoc.go
+++ b/godoc/godoc.go
@@ -303,11 +303,12 @@
 }
 
 func pkgLinkFunc(path string) string {
-	relpath := path[1:]
 	// because of the irregular mapping under goroot
 	// we need to correct certain relative paths
-	relpath = strings.TrimPrefix(relpath, "src/pkg/")
-	return "pkg/" + relpath // remove trailing '/' for relative URL
+	path = strings.TrimPrefix(path, "/")
+	path = strings.TrimPrefix(path, "src/")
+	path = strings.TrimPrefix(path, "pkg/")
+	return "pkg/" + path
 }
 
 func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string {
@@ -368,6 +369,7 @@
 
 func srcLinkFunc(s string) string {
 	s = pathpkg.Clean("/" + s)
+	// TODO(bgarcia): Once the /src/pkg -> /src transition occurs, update this function.
 	if !strings.HasPrefix(s, "/src/pkg/") {
 		s = "/src/pkg" + s
 	}
@@ -388,8 +390,7 @@
 }
 
 func docLinkFunc(s string, ident string) string {
-	s = strings.TrimPrefix(s, "/src")
-	return pathpkg.Clean("/"+s) + "/#" + ident
+	return pathpkg.Clean("/pkg/"+s) + "/#" + ident
 }
 
 func (p *Presentation) example_textFunc(info *PageInfo, funcName, indent string) string {
diff --git a/godoc/godoc_test.go b/godoc/godoc_test.go
index 76ce946..37e1d71 100644
--- a/godoc/godoc_test.go
+++ b/godoc/godoc_test.go
@@ -14,7 +14,9 @@
 		want string
 	}{
 		{"/src/pkg/fmt", "pkg/fmt"},
+		{"src/pkg/fmt", "pkg/fmt"},
 		{"/fmt", "pkg/fmt"},
+		{"fmt", "pkg/fmt"},
 	} {
 		if got := pkgLinkFunc(tc.path); got != tc.want {
 			t.Errorf("pkgLinkFunc(%v) = %v; want %v", tc.path, got, tc.want)
@@ -84,8 +86,8 @@
 		ident string
 		want  string
 	}{
-		{"/src/pkg/fmt", "Sprintf", "/pkg/fmt/#Sprintf"},
-		{"/src/pkg/fmt", "EOF", "/pkg/fmt/#EOF"},
+		{"fmt", "Sprintf", "/pkg/fmt/#Sprintf"},
+		{"fmt", "EOF", "/pkg/fmt/#EOF"},
 	} {
 		if got := docLinkFunc(tc.src, tc.ident); got != tc.want {
 			t.Errorf("docLinkFunc(%v, %v) = %v; want %v", tc.src, tc.ident, got, tc.want)
diff --git a/godoc/index.go b/godoc/index.go
index 417c386..f14f5c1 100644
--- a/godoc/index.go
+++ b/godoc/index.go
@@ -694,11 +694,11 @@
 }
 
 func (x *Indexer) indexDocs(dirname string, filename string, astFile *ast.File) {
-	pkgName := astFile.Name.Name
+	pkgName := x.intern(astFile.Name.Name)
 	if pkgName == "main" {
 		return
 	}
-	dirname = pathpkg.Clean(dirname)
+	pkgPath := x.intern(strings.TrimPrefix(strings.TrimPrefix(dirname, "/src/"), "pkg/"))
 	astPkg := ast.Package{
 		Name: pkgName,
 		Files: map[string]*ast.File{
@@ -711,8 +711,9 @@
 		if x.idents[sk] == nil {
 			x.idents[sk] = make(map[string][]Ident)
 		}
+		name = x.intern(name)
 		x.idents[sk][name] = append(x.idents[sk][name], Ident{
-			Path:    dirname,
+			Path:    pkgPath,
 			Package: pkgName,
 			Name:    name,
 			Doc:     doc.Synopsis(docstr),
@@ -722,7 +723,7 @@
 	if x.idents[PackageClause] != nil {
 		pkgs := x.idents[PackageClause][docPkg.Name]
 		for i, p := range pkgs {
-			if p.Path == dirname {
+			if p.Path == pkgPath {
 				foundPkg = true
 				if docPkg.Doc != "" {
 					p.Doc = doc.Synopsis(docPkg.Doc)
@@ -760,7 +761,7 @@
 			// Change the name of methods to be "<typename>.<methodname>".
 			// They will still be indexed as <methodname>.
 			idents := x.idents[MethodDecl][f.Name]
-			idents[len(idents)-1].Name = t.Name + "." + f.Name
+			idents[len(idents)-1].Name = x.intern(t.Name + "." + f.Name)
 		}
 	}
 	for _, v := range docPkg.Vars {
@@ -797,7 +798,7 @@
 	if _, ok := x.packagePath[ppKey]; !ok {
 		x.packagePath[ppKey] = make(map[string]bool)
 	}
-	pkgPath := x.intern(strings.TrimPrefix(dirname, "/src/pkg/"))
+	pkgPath := x.intern(strings.TrimPrefix(strings.TrimPrefix(dirname, "/src/"), "pkg/"))
 	x.packagePath[ppKey][pkgPath] = true
 
 	// Merge in exported symbols found walking this file into
diff --git a/godoc/index_test.go b/godoc/index_test.go
index 9a71a47..9341784 100644
--- a/godoc/index_test.go
+++ b/godoc/index_test.go
@@ -231,23 +231,23 @@
 		want = map[SpotKind]map[string][]Ident{
 			PackageClause: map[string][]Ident{
 				"bar": []Ident{
-					{"/src/pkg/bar", "bar", "bar", "Package bar is another example to test races."},
-					{"/src/pkg/other/bar", "bar", "bar", "Package bar is another bar package."},
+					{"bar", "bar", "bar", "Package bar is another example to test races."},
+					{"other/bar", "bar", "bar", "Package bar is another bar package."},
 				},
-				"foo": []Ident{{"/src/pkg/foo", "foo", "foo", "Package foo is an example."}},
+				"foo": []Ident{{"foo", "foo", "foo", "Package foo is an example."}},
 			},
 			ConstDecl: map[string][]Ident{
-				"Pi": []Ident{{"/src/pkg/foo", "foo", "Pi", ""}},
+				"Pi": []Ident{{"foo", "foo", "Pi", ""}},
 			},
 			VarDecl: map[string][]Ident{
-				"Foos": []Ident{{"/src/pkg/foo", "foo", "Foos", ""}},
+				"Foos": []Ident{{"foo", "foo", "Foos", ""}},
 			},
 			TypeDecl: map[string][]Ident{
-				"Foo": []Ident{{"/src/pkg/foo", "foo", "Foo", "Foo is stuff."}},
+				"Foo": []Ident{{"foo", "foo", "Foo", "Foo is stuff."}},
 			},
 			FuncDecl: map[string][]Ident{
-				"New": []Ident{{"/src/pkg/foo", "foo", "New", ""}},
-				"X":   []Ident{{"/src/pkg/other/bar", "bar", "X", ""}},
+				"New": []Ident{{"foo", "foo", "New", ""}},
+				"X":   []Ident{{"other/bar", "bar", "X", ""}},
 			},
 		}
 	}