-
Notifications
You must be signed in to change notification settings - Fork 9
/
print.go
249 lines (219 loc) · 4.15 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"io"
"unsafe"
)
var (
defaultLogWriter io.Writer = &serialDevice
defaultScreenWriter io.Writer = text_mode_writer{}
defaultErrorWriter io.Writer = text_mode_error_writer{}
)
// Copied and adapted code from go runtime
func printsp(w io.Writer) {
printstring(w, " ")
}
func printnl(w io.Writer) {
printstring(w, "\n")
}
func printbool(w io.Writer, v bool) {
if v {
printstring(w, "true")
} else {
printstring(w, "false")
}
}
func printfloat(w io.Writer, v float64) {
switch {
case v != v:
printstring(w, "NaN")
return
case v+v == v && v > 0:
printstring(w, "+Inf")
return
case v+v == v && v < 0:
printstring(w, "-Inf")
return
}
const n = 7 // digits printed
var buf [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := 5.0
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float64(s)
v *= 10
}
buf[1] = buf[2]
buf[2] = '.'
buf[n+2] = 'e'
buf[n+3] = '+'
if e < 0 {
e = -e
buf[n+3] = '-'
}
buf[n+4] = byte(e/100) + '0'
buf[n+5] = byte(e/10)%10 + '0'
buf[n+6] = byte(e%10) + '0'
writeWrapper(w, buf[:])
}
func printcomplex(w io.Writer, c complex128) {
kprint("(", real(c), imag(c), "i)")
}
func printuint(w io.Writer, v uint64) {
var buf [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = byte(v%10 + '0')
if v < 10 {
break
}
v /= 10
}
writeWrapper(w, buf[i:])
}
func printint(w io.Writer, v int64) {
if v < 0 {
printstring(w, "-")
v = -v
}
printuint(w, uint64(v))
}
var minhexdigits = 0 // protected by printlock
func printhex(w io.Writer, v uint64) {
const dig = "0123456789abcdef"
var buf [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = dig[v%16]
if v < 16 && len(buf)-i >= minhexdigits {
break
}
v /= 16
}
i--
buf[i] = 'x'
i--
buf[i] = '0'
writeWrapper(w, buf[i:])
}
func printpointer(w io.Writer, p unsafe.Pointer) {
printhex(w, uint64(uintptr(p)))
}
func printuintptr(w io.Writer, p uintptr) {
printhex(w, uint64(p))
}
func printstring(w io.Writer, s string) {
writeWrapper(w, bytes(s))
}
//func printslice(s []byte) {
// sp := (*slice)(unsafe.Pointer(&s))
// kprint("[", len(s), "/", cap(s), "]")
// printpointer(sp.array)
//}
func kFprint(w io.Writer, args ...interface{}) {
for _, v := range args {
switch t := v.(type) {
case bool:
printbool(w, t)
case string:
printstring(w, t)
case uintptr:
printuintptr(w, t)
case unsafe.Pointer:
printpointer(w, t)
case byte:
printuint(w, uint64(t))
case uint:
printuint(w, uint64(t))
case uint32:
printuint(w, uint64(t))
case uint64:
printuint(w, uint64(t))
case int:
printint(w, int64(t))
case int32:
printint(w, int64(t))
case int64:
printint(w, t)
case float64:
printfloat(w, t)
case complex128:
printcomplex(w, t)
default:
// TODO: Maybe print type if that is possible without memory allocation?
printstring(defaultErrorWriter, "<Unknown Type>")
}
}
}
func kprint(args ...interface{}) {
kFprint(defaultScreenWriter, args...)
kFprint(defaultLogWriter, args...)
}
func kprintln(args ...interface{}) {
kprint(args...)
kprint("\n")
}
func kerror(args ...interface{}) {
kFprint(defaultErrorWriter, args...)
kFprint(defaultLogWriter, args...)
}
func kerrorln(args ...interface{}) {
kerror(args...)
kerror("\n")
}
func kdebug(args ...interface{}) {
kFprint(defaultLogWriter, args...)
}
func kdebugln(args ...interface{}) {
kdebug(args...)
kdebug("\n")
}
func writeWrapper(w io.Writer, p []byte) {
if w == nil {
w = defaultScreenWriter
}
w.Write(*(*[]byte)(noEscape(unsafe.Pointer(&p))))
}
func bytes(s string) []byte {
stringData := unsafe.StringData(s)
return unsafe.Slice(stringData, len(s))
}
// noEscape hides a pointer from escape analysis. This function is copied over
// from runtime/stubs.go
//
//go:nosplit
func noEscape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)
}