This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.go
216 lines (194 loc) · 4.33 KB
/
print.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package xfmt
import (
"fmt"
"reflect"
"strings"
)
const varFormat = "%v"
var status = true
func Enable() {
status = true
}
func Disable() {
status = false
}
type value struct {
Arg interface{}
Flag string
}
type pointer struct {
Format string
Ptr uintptr
Addr reflect.Value
}
// Print 打印
func Print(args ...interface{}) {
fmt.Print(Sprintf(format(args...), args...))
}
// Println 打印并换行
func Println(args ...interface{}) {
fmt.Println(Sprintf(format(args...), args...))
}
// Printf 打印并格式化
func Printf(format string, args ...interface{}) {
fmt.Print(Sprintf(format, args...))
}
// Sprint 打印并返回
func Sprint(args ...interface{}) string {
return Sprintf(format(args...), args...)
}
// Sprintln 打印换行并返回
func Sprintln(args ...interface{}) string {
return Sprintf(format(args...)+"\n", args...)
}
// Sprintf 格式化并返回
func Sprintf(format string, args ...interface{}) string {
str := fmt.Sprintf(format, args...) // 放在第一行可以起到效验的作用
// 停用
if !status {
return str
}
var pointers []pointer
for _, v := range values(format, args...) {
pointers = append(pointers, extract(reflect.ValueOf(v.Arg), v.Flag)...)
}
// 去重
var unique []pointer
for _, p := range pointers {
find := false
for _, u := range unique {
if p.Ptr == u.Ptr {
find = true
}
}
if !find {
unique = append(unique, p)
}
}
return replace(str, unique)
}
// 生成格式
func format(args ...interface{}) string {
var formats []string
for i := 0; i < len(args); i++ {
formats = append(formats, varFormat)
}
return strings.Join(formats, " ")
}
// 获取全部指针对象的反射
func values(format string, args ...interface{}) []value {
var result []value
for _, v := range filter(format, args...) {
val := reflect.ValueOf(v.Arg)
switch val.Kind() {
case reflect.Struct:
result = append(result, v)
break
case reflect.Ptr:
if !val.Elem().CanAddr() {
continue
}
result = append(result, v)
break
case reflect.Map:
iter := val.MapRange()
for iter.Next() {
result = append(result, value{iter.Value().Interface(), v.Flag})
}
break
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
result = append(result, value{val.Index(i).Interface(), v.Flag})
}
break
}
}
return result
}
// 过滤无需解析的参数
func filter(format string, args ...interface{}) []value {
fb := []byte(format)
next := len(fb) - 1
loc := -1
var result []value
for k, v := range fb {
if v == '%' {
loc += 1
if k+1 <= next {
switch fb[k+1] {
case 'v':
result = append(result, value{
Arg: args[loc],
Flag: "%v",
})
break
case '+':
if k+2 <= next && fb[k+2] == 'v' {
result = append(result, value{
Arg: args[loc],
Flag: "%+v",
})
}
break
case '#':
if k+2 <= next && fb[k+2] == 'v' {
result = append(result, value{
Arg: args[loc],
Flag: "%#v",
})
}
break
}
}
}
}
return result
}
// 提取指针信息
func extract(val reflect.Value, format string) []pointer {
var pointers []pointer
switch val.Kind() {
case reflect.Ptr:
elem := val.Elem()
if !elem.CanAddr() {
return pointers
}
pointers = append(pointers, pointer{
Format: format,
Ptr: elem.Addr().Pointer(),
Addr: elem.Addr(),
})
for _, v := range values(format, elem.Interface()) {
pointers = append(pointers, extract(reflect.ValueOf(v.Arg), v.Flag)...)
}
break
case reflect.Struct:
for i := 0; i < val.NumField(); i++ {
if !val.Field(i).CanInterface() {
continue
}
tag := val.Type().Field(i).Tag.Get("xfmt")
if tag == "-" || tag == "_" {
continue
}
for _, v := range values(format, val.Field(i).Interface()) {
pointers = append(pointers, extract(reflect.ValueOf(v.Arg), v.Flag)...)
}
}
break
case reflect.Map, reflect.Slice, reflect.Array:
for _, v := range values(format, val.Interface()) {
pointers = append(pointers, extract(reflect.ValueOf(v.Arg), v.Flag)...)
}
break
}
return pointers
}
// 替换指针为机构体
func replace(str string, pointers []pointer) string {
for _, ptr := range pointers {
ptrString := fmt.Sprintf("0x%x", ptr.Ptr)
str = strings.Replace(str, ptrString, fmt.Sprintf("%s:"+ptr.Format, ptrString, ptr.Addr), 1)
}
return str
}