Skip to content

Commit 41dd04a

Browse files
committed
copying the actual code of the stringer from big go
1 parent 1d4f0e7 commit 41dd04a

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

src/runtime/debug/debug.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Package debug is a dummy package that is not yet implemented.
22
package debug
33

4-
import "runtime"
4+
import (
5+
"fmt"
6+
"runtime"
7+
"strconv"
8+
"strings"
9+
)
510

611
// SetMaxStack sets the maximum amount of memory that can be used by a single
712
// goroutine stack.
@@ -62,9 +67,59 @@ func SetGCPercent(n int) int {
6267
return n
6368
}
6469

65-
// String implements Stringer for BuildInfo.
66-
//
67-
// Not implemented.
70+
// Start of stolen from big go. TODO: import/reuse without copy pasta.
71+
72+
// quoteKey reports whether key is required to be quoted.
73+
func quoteKey(key string) bool {
74+
return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`")
75+
}
76+
77+
// quoteValue reports whether value is required to be quoted.
78+
func quoteValue(value string) bool {
79+
return strings.ContainsAny(value, " \t\r\n\"`")
80+
}
81+
6882
func (bi *BuildInfo) String() string {
69-
return "<build info placeholder>\n"
83+
buf := new(strings.Builder)
84+
if bi.GoVersion != "" {
85+
fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion)
86+
}
87+
if bi.Path != "" {
88+
fmt.Fprintf(buf, "path\t%s\n", bi.Path)
89+
}
90+
var formatMod func(string, Module)
91+
formatMod = func(word string, m Module) {
92+
buf.WriteString(word)
93+
buf.WriteByte('\t')
94+
buf.WriteString(m.Path)
95+
buf.WriteByte('\t')
96+
buf.WriteString(m.Version)
97+
if m.Replace == nil {
98+
buf.WriteByte('\t')
99+
buf.WriteString(m.Sum)
100+
} else {
101+
buf.WriteByte('\n')
102+
formatMod("=>", *m.Replace)
103+
}
104+
buf.WriteByte('\n')
105+
}
106+
if bi.Main != (Module{}) {
107+
formatMod("mod", bi.Main)
108+
}
109+
for _, dep := range bi.Deps {
110+
formatMod("dep", *dep)
111+
}
112+
for _, s := range bi.Settings {
113+
key := s.Key
114+
if quoteKey(key) {
115+
key = strconv.Quote(key)
116+
}
117+
value := s.Value
118+
if quoteValue(value) {
119+
value = strconv.Quote(value)
120+
}
121+
fmt.Fprintf(buf, "build\t%s=%s\n", key, value)
122+
}
123+
124+
return buf.String()
70125
}

0 commit comments

Comments
 (0)