go/types: write TypeParam id's using subscript digits (fun)

Change-Id: I0df6e534774b2616f67c29a1525ddb40dbed9b05
This commit is contained in:
Robert Griesemer 2019-11-27 12:55:40 -08:00
parent 8c7ab48154
commit 3e2c9743cb
1 changed files with 19 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import (
"bytes"
"fmt"
"strings"
"unicode/utf8"
)
// A Qualifier controls how named package-level objects are printed in
@ -262,9 +263,9 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
case *TypeParam:
var s string
if t.obj != nil {
s = fmt.Sprintf("%s.%d", t.obj.name, t.id)
s = fmt.Sprintf("%s%s", t.obj.name, subscript(t.id))
} else {
s = fmt.Sprintf("TypeParam.%d[%d]", t.id, t.index)
s = fmt.Sprintf("TypeParam%d[%d]", subscript(t.id), t.index)
}
buf.WriteString(s)
@ -370,3 +371,19 @@ func embeddedFieldName(typ Type) string {
}
return "" // not a (pointer to) a defined type
}
// subscript returns the decimal (utf8) representation of x using subscript digits.
func subscript(x uint64) []byte {
const w = len("₀") // all digits 0...9 have the same utf8 width
var buf [32 * w]byte
i := len(buf)
for {
i -= w
utf8.EncodeRune(buf[i:], '₀'+rune(x%10)) // '₀' == U+2080
x /= 10
if x == 0 {
break
}
}
return buf[i:]
}