-
Notifications
You must be signed in to change notification settings - Fork 32
/
ptrconvert.go
executable file
·140 lines (114 loc) · 3.24 KB
/
ptrconvert.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
package jingo
// ptrconvert.go declares a number of primitive form -> buffer conversion
// functions based on an unsafe.Pointer input. We're using the implementation
// from the standard library here, which don't perform badly but are a
// candidate for a more high performance implementation to be introduced.
import (
"reflect"
"strconv"
"time"
"unsafe"
)
var typeconv = map[reflect.Kind]func(unsafe.Pointer, *Buffer){
reflect.Bool: ptrBoolToBuf,
reflect.Int: ptrIntToBuf,
reflect.Int8: ptrInt8ToBuf,
reflect.Int16: ptrInt16ToBuf,
reflect.Int32: ptrInt32ToBuf,
reflect.Int64: ptrInt64ToBuf,
reflect.Uint: ptrUintToBuf,
reflect.Uint8: ptrUint8ToBuf,
reflect.Uint16: ptrUint16ToBuf,
reflect.Uint32: ptrUint32ToBuf,
reflect.Uint64: ptrUint64ToBuf,
reflect.Float32: ptrFloat32ToBuf,
reflect.Float64: ptrFloat64ToBuf,
reflect.String: ptrStringToBuf,
}
var btrue, bfalse = []byte("true"), []byte("false")
func ptrBoolToBuf(v unsafe.Pointer, b *Buffer) {
r := *(*bool)(v)
if r {
b.Write(btrue)
} else {
b.Write(bfalse)
}
}
func ptrIntToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendInt(b.Bytes, int64(*(*int)(v)), 10)
}
func ptrInt8ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendInt(b.Bytes, int64(*(*int8)(v)), 10)
}
func ptrInt16ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendInt(b.Bytes, int64(*(*int16)(v)), 10)
}
func ptrInt32ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendInt(b.Bytes, int64(*(*int32)(v)), 10)
}
func ptrInt64ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendInt(b.Bytes, *(*int64)(v), 10)
}
func ptrUintToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendUint(b.Bytes, uint64(*(*uint)(v)), 10)
}
func ptrUint8ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendUint(b.Bytes, uint64(*(*uint8)(v)), 10)
}
func ptrUint16ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendUint(b.Bytes, uint64(*(*uint16)(v)), 10)
}
func ptrUint32ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendUint(b.Bytes, uint64(*(*uint32)(v)), 10)
}
func ptrUint64ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendUint(b.Bytes, *(*uint64)(v), 10)
}
func ptrFloat32ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendFloat(b.Bytes, float64(*(*float32)(v)), 'f', -1, 32)
}
func ptrFloat64ToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = strconv.AppendFloat(b.Bytes, *(*float64)(v), 'f', -1, 64)
}
func ptrStringToBuf(v unsafe.Pointer, b *Buffer) {
b.WriteString(*(*string)(v))
}
func ptrTimeToBuf(v unsafe.Pointer, b *Buffer) {
b.Bytes = (*time.Time)(v).AppendFormat(b.Bytes, time.RFC3339Nano)
}
func ptrEscapeStringToBuf(v unsafe.Pointer, w *Buffer) {
bs := *(*string)(v)
pos := 0
for i := 0; i < len(bs); i++ {
switch bs[i] {
case '\\', '"':
if pos < i {
w.WriteString(bs[pos:i])
}
pos = i + 1
w.WriteByte('\\')
w.WriteByte(bs[i])
case '\n':
if pos < i {
w.WriteString(bs[pos:i])
}
pos = i + 1
w.WriteString(`\n`)
case '\r':
if pos < i {
w.WriteString(bs[pos:i])
}
pos = i + 1
w.WriteString(`\r`)
case '\t':
if pos < i {
w.WriteString(bs[pos:i])
}
pos = i + 1
w.WriteString(`\t`)
}
}
if pos < len(bs) {
w.WriteString(bs[pos:])
}
}