-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsafe_fmt.go
82 lines (60 loc) · 1.23 KB
/
unsafe_fmt.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
package errors
import (
"fmt"
"io"
"strconv"
"unsafe"
)
func subPrintArg(s fmt.State, arg interface{}, verb rune) {
i := *(*iface)(unsafe.Pointer(&s))
if i.typ != ppType {
var buf [64]byte
i := 0
buf[i] = '%'
i++
for _, f := range "-+# 0" {
if s.Flag(int(f)) {
buf[i] = byte(f)
i++
}
}
if w, ok := s.Width(); ok {
q := strconv.AppendInt(buf[:i], int64(w), 10)
i = len(q)
}
if p, ok := s.Precision(); ok {
buf[i] = '.'
i++
q := strconv.AppendInt(buf[:i], int64(p), 10)
i = len(q)
}
buf[i] = byte(verb)
i++
fmt.Fprintf(s, bytesToString(buf[:i]), arg)
return
}
printArg(i.word, arg, verb)
}
type (
iface struct {
typ, word unsafe.Pointer
}
formatter struct{}
)
var ppType unsafe.Pointer
func init() {
fmt.Fprintf(io.Discard, "%v", formatter{})
}
//go:linkname printArg fmt.(*pp).printArg
func printArg(p unsafe.Pointer, arg interface{}, verb rune)
//go:linkname newPrinter fmt.newPrinter
func newPrinter() unsafe.Pointer
//go:linkname ppFree fmt.(*pp).free
func ppFree(unsafe.Pointer)
func (formatter) Format(s fmt.State, _ rune) {
i := *(*iface)(unsafe.Pointer(&s))
ppType = i.typ
}
func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}