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
|
package format
import "codeberg.org/gruf/go-xunsafe"
// iterSliceType returns a FormatFunc capable of iterating
// and formatting the given slice type currently in TypeIter{}.
// note this will fetch a sub-FormatFunc for the slice element
// type, and also handle special cases of []byte, []rune slices.
func (fmt *Formatter) iterSliceType(t xunsafe.TypeIter) FormatFunc {
// Get nested element type.
elem := t.Type.Elem()
esz := elem.Size()
// Get nested elem TypeIter{} with flags.
flags := xunsafe.ReflectSliceElemFlags(elem)
et := t.Child(elem, flags)
// Get elem format func.
fn := fmt.loadOrGet(et)
if fn == nil {
panic("unreachable")
}
if !needs_typestr(t) {
return func(s *State) {
ptr := s.P
// Get data as unsafe slice header.
hdr := (*xunsafe.Unsafeheader_Slice)(ptr)
if hdr == nil || hdr.Data == nil {
// Append nil.
appendNil(s)
return
}
// Prepend array brace.
s.B = append(s.B, '[')
if hdr.Len > 0 {
for i := 0; i < hdr.Len; i++ {
// Format at array index.
offset := esz * uintptr(i)
s.P = add(hdr.Data, offset)
fn(s)
// Append separator.
s.B = append(s.B, ',')
}
// Drop final comma.
s.B = s.B[:len(s.B)-1]
}
// Append array brace.
s.B = append(s.B, ']')
}
}
// Slice type string with ptrs / refs.
typestrPtrs := typestr_with_ptrs(t)
typestrRefs := typestr_with_refs(t)
return func(s *State) {
ptr := s.P
// Get data as unsafe slice header.
hdr := (*xunsafe.Unsafeheader_Slice)(ptr)
if hdr == nil || hdr.Data == nil {
// Append nil value with type.
appendNilType(s, typestrPtrs)
return
}
// Open / close braces.
var open, close uint8
open, close = '[', ']'
// Include type info.
if s.A.WithType() {
s.B = append(s.B, typestrRefs...)
open, close = '{', '}'
}
// Prepend array brace.
s.B = append(s.B, open)
if hdr.Len > 0 {
for i := 0; i < hdr.Len; i++ {
// Format at array index.
offset := esz * uintptr(i)
s.P = add(hdr.Data, offset)
fn(s)
// Append separator.
s.B = append(s.B, ',')
}
// Drop final comma.
s.B = s.B[:len(s.B)-1]
}
// Append array brace.
s.B = append(s.B, close)
}
}
func wrapByteSlice(t xunsafe.TypeIter, fn FormatFunc) FormatFunc {
if !needs_typestr(t) {
return func(s *State) {
if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() {
appendString(s, *(*string)(s.P))
} else {
fn(s)
}
}
}
typestr := typestr_with_ptrs(t)
return func(s *State) {
if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() {
if s.A.WithType() {
s.B = append(s.B, "("+typestr+")("...)
appendString(s, *(*string)(s.P))
s.B = append(s.B, ")"...)
} else {
appendString(s, *(*string)(s.P))
}
} else {
fn(s)
}
}
}
func wrapRuneSlice(t xunsafe.TypeIter, fn FormatFunc) FormatFunc {
if !needs_typestr(t) {
return func(s *State) {
if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() {
appendString(s, string(*(*[]rune)(s.P)))
} else {
fn(s)
}
}
}
typestr := typestr_with_ptrs(t)
return func(s *State) {
if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() {
if s.A.WithType() {
s.B = append(s.B, "("+typestr+")("...)
appendString(s, string(*(*[]rune)(s.P)))
s.B = append(s.B, ")"...)
} else {
appendString(s, string(*(*[]rune)(s.P)))
}
} else {
fn(s)
}
}
}
|