MCPcopy Index your code
hub / github.com/maruel/panicparse / Init

Method Init

stack/stack.go:52–106  ·  view source on GitHub ↗

Init parses the raw function call line from a goroutine stack trace. Go stack traces print a mangled function call, this wrapper unmangle the string before printing and adds other filtering methods. The main caveat is that for calls in package main, the package import URL is left out.

(raw string)

Source from the content-addressed store, hash-verified

50// The main caveat is that for calls in package main, the package import URL is
51// left out.
52func (f *Func) Init(raw string) error {
53 // Format can be:
54 // - gopkg.in/yaml%2ev2.(*Struct).Method (handling dots is tricky)
55 // - main.func·001 (go statements)
56 // - foo (C code)
57 //
58 // The function is optimized to reduce its memory usage.
59 endPkg := 0
60 if lastSlash := strings.LastIndexByte(raw, '/'); lastSlash != -1 {
61 // Cut the path elements.
62 r := strings.IndexByte(raw[lastSlash+1:], '.')
63 if r == -1 {
64 return errors.New("bad function reference: expected to have at least one dot")
65 }
66 endPkg = lastSlash + r + 1
67 } else {
68 // It's fine if there's no dot, it happens in C code in go1.4 and lower.
69 endPkg = strings.IndexByte(raw, '.')
70 }
71 // Only the path part is escaped.
72 var err error
73 if f.Complete, err = url.QueryUnescape(raw); err != nil {
74 return fmt.Errorf("bad function reference: %w", err)
75 }
76 // Update the index in the unescaped string.
77 endPkg += len(f.Complete) - len(raw)
78 if endPkg != -1 {
79 f.ImportPath = f.Complete[:endPkg]
80 }
81 f.Name = f.Complete[endPkg+1:]
82 if idx := strings.LastIndexByte(f.Name, ' '); idx > -1 {
83 cut := f.Name[:idx]
84 // TODO(go1.20): switch to strings.CutSuffix
85 const inGoroutineSuffix = " in goroutine"
86 if strings.HasSuffix(cut, inGoroutineSuffix) {
87 f.Name = strings.TrimSuffix(cut, inGoroutineSuffix)
88 }
89 }
90 f.DirName = f.ImportPath
91 if i := strings.LastIndexByte(f.DirName, '/'); i != -1 {
92 f.DirName = f.DirName[i+1:]
93 }
94 if f.ImportPath == "main" {
95 f.IsPkgMain = true
96 // Consider main.main to be exported.
97 if f.Name == "main" {
98 f.IsExported = true
99 }
100 } else {
101 parts := strings.Split(f.Name, ".")
102 r, _ := utf8.DecodeRuneInString(parts[len(parts)-1])
103 f.IsExported = unicode.ToUpper(r) == r
104 }
105 return nil
106}
107
108// String returns Complete.
109func (f *Func) String() string {

Callers 4

newFuncFunction · 0.95
newFuncFunction · 0.95
scanMethod · 0.80
parseFuncFunction · 0.80

Calls

no outgoing calls

Tested by 2

newFuncFunction · 0.76
newFuncFunction · 0.76