-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
modules.go
94 lines (89 loc) · 1.74 KB
/
modules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cxgo
import "bytes"
type SrcFunc struct {
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
OffsetStart int `json:"offset_start,omitempty"`
OffsetEnd int `json:"offset_end,omitempty"`
Func string `json:"func"`
Src string `json:"src,omitempty"`
Proto string `json:"proto,omitempty"`
}
func SourceFunc(fname string, src []byte, fd *CFuncDecl) SrcFunc {
fsrc := ""
psrc := ""
var (
istart int
iend int
line int
)
if rng := fd.Range; rng != nil && rng.Start > 0 {
line = rng.StartLine
start := rng.Start
end := rng.End
if e, f := findEnd(src[start:]); e > 0 {
e += start
f += start
end = e
psrc = string(bytes.TrimSpace(src[start:f])) + ";\n"
}
if s := findCommentStart(src[:start]); s > 0 {
start = s
}
if end > 0 {
istart = start
iend = end
fsrc = string(src[start:end])
}
}
return SrcFunc{
File: fname,
Func: fd.Name.Name,
Src: fsrc,
Proto: psrc,
Line: line,
OffsetStart: istart,
OffsetEnd: iend,
}
}
func findCommentStart(src []byte) int {
if len(src) == 0 || src[len(src)-1] != '\n' {
return -1
}
src = src[:len(src)-1]
i := bytes.LastIndex(src, []byte("//"))
if i < 0 || bytes.ContainsAny(src[i:], "\n\r") {
return -1
}
return i
}
func findEnd(src []byte) (int, int) {
started := false
level := 0
first := -1
i := 0
for ; (!started || level > 0) && i < len(src); i++ {
switch src[i] {
case '{':
if !started {
started = true
first = i
}
level++
case '}':
level--
if level == 0 {
break
}
}
}
if i >= len(src) {
return -1, -1
}
for j := 0; j < 2; j++ {
if i+1 < len(src) && src[i+1] == '\n' {
i++
}
}
return i, first
}