-
Notifications
You must be signed in to change notification settings - Fork 30
/
string.go
261 lines (218 loc) · 5.9 KB
/
string.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
250
251
252
253
254
255
256
257
258
259
260
261
package mp4
import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
"github.com/abema/go-mp4/internal/util"
)
type stringifier struct {
buf *bytes.Buffer
src IImmutableBox
indent string
ctx Context
}
func Stringify(src IImmutableBox, ctx Context) (string, error) {
return StringifyWithIndent(src, "", ctx)
}
func StringifyWithIndent(src IImmutableBox, indent string, ctx Context) (string, error) {
boxDef := src.GetType().getBoxDef(ctx)
if boxDef == nil {
return "", ErrBoxInfoNotFound
}
v := reflect.ValueOf(src).Elem()
m := &stringifier{
buf: bytes.NewBuffer(nil),
src: src,
indent: indent,
ctx: ctx,
}
err := m.stringifyStruct(v, boxDef.fields, 0, true)
if err != nil {
return "", err
}
return m.buf.String(), nil
}
func (m *stringifier) stringify(v reflect.Value, fi *fieldInstance, depth int) error {
switch v.Type().Kind() {
case reflect.Ptr:
return m.stringifyPtr(v, fi, depth)
case reflect.Struct:
return m.stringifyStruct(v, fi.children, depth, fi.is(fieldExtend))
case reflect.Array:
return m.stringifyArray(v, fi, depth)
case reflect.Slice:
return m.stringifySlice(v, fi, depth)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return m.stringifyInt(v, fi, depth)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return m.stringifyUint(v, fi, depth)
case reflect.Bool:
return m.stringifyBool(v, depth)
case reflect.String:
return m.stringifyString(v, depth)
default:
return fmt.Errorf("unsupported type: %s", v.Type().Kind())
}
}
func (m *stringifier) stringifyPtr(v reflect.Value, fi *fieldInstance, depth int) error {
return m.stringify(v.Elem(), fi, depth)
}
func (m *stringifier) stringifyStruct(v reflect.Value, fs []*field, depth int, extended bool) error {
if !extended {
m.buf.WriteString("{")
if m.indent != "" {
m.buf.WriteString("\n")
}
depth++
}
for _, f := range fs {
fi := resolveFieldInstance(f, m.src, v, m.ctx)
if !isTargetField(m.src, fi, m.ctx) {
continue
}
if f.cnst != "" || f.is(fieldHidden) {
continue
}
if !f.is(fieldExtend) {
if m.indent != "" {
writeIndent(m.buf, m.indent, depth+1)
} else if m.buf.Len() != 0 && m.buf.Bytes()[m.buf.Len()-1] != '{' {
m.buf.WriteString(" ")
}
m.buf.WriteString(f.name)
m.buf.WriteString("=")
}
str, ok := fi.cfo.StringifyField(f.name, m.indent, depth+1, m.ctx)
if ok {
m.buf.WriteString(str)
if !f.is(fieldExtend) && m.indent != "" {
m.buf.WriteString("\n")
}
continue
}
if f.name == "Version" {
m.buf.WriteString(strconv.Itoa(int(m.src.GetVersion())))
} else if f.name == "Flags" {
fmt.Fprintf(m.buf, "0x%06x", m.src.GetFlags())
} else {
err := m.stringify(v.FieldByName(f.name), fi, depth)
if err != nil {
return err
}
}
if !f.is(fieldExtend) && m.indent != "" {
m.buf.WriteString("\n")
}
}
if !extended {
if m.indent != "" {
writeIndent(m.buf, m.indent, depth)
}
m.buf.WriteString("}")
}
return nil
}
func (m *stringifier) stringifyArray(v reflect.Value, fi *fieldInstance, depth int) error {
begin, sep, end := "[", ", ", "]"
if fi.is(fieldString) || fi.is(fieldISO639_2) {
begin, sep, end = "\"", "", "\""
} else if fi.is(fieldUUID) {
begin, sep, end = "", "", ""
}
m.buf.WriteString(begin)
m2 := *m
if fi.is(fieldString) {
m2.buf = bytes.NewBuffer(nil)
}
size := v.Type().Size()
for i := 0; i < int(size)/int(v.Type().Elem().Size()); i++ {
if i != 0 {
m2.buf.WriteString(sep)
}
if err := m2.stringify(v.Index(i), fi, depth+1); err != nil {
return err
}
if fi.is(fieldUUID) && (i == 3 || i == 5 || i == 7 || i == 9) {
m.buf.WriteString("-")
}
}
if fi.is(fieldString) {
m.buf.WriteString(util.EscapeUnprintables(m2.buf.String()))
}
m.buf.WriteString(end)
return nil
}
func (m *stringifier) stringifySlice(v reflect.Value, fi *fieldInstance, depth int) error {
begin, sep, end := "[", ", ", "]"
if fi.is(fieldString) || fi.is(fieldISO639_2) {
begin, sep, end = "\"", "", "\""
}
m.buf.WriteString(begin)
m2 := *m
if fi.is(fieldString) {
m2.buf = bytes.NewBuffer(nil)
}
for i := 0; i < v.Len(); i++ {
if fi.length != LengthUnlimited && uint(i) >= fi.length {
break
}
if i != 0 {
m2.buf.WriteString(sep)
}
if err := m2.stringify(v.Index(i), fi, depth+1); err != nil {
return err
}
}
if fi.is(fieldString) {
m.buf.WriteString(util.EscapeUnprintables(m2.buf.String()))
}
m.buf.WriteString(end)
return nil
}
func (m *stringifier) stringifyInt(v reflect.Value, fi *fieldInstance, depth int) error {
if fi.is(fieldHex) {
val := v.Int()
if val >= 0 {
m.buf.WriteString("0x")
m.buf.WriteString(strconv.FormatInt(val, 16))
} else {
m.buf.WriteString("-0x")
m.buf.WriteString(strconv.FormatInt(-val, 16))
}
} else {
m.buf.WriteString(strconv.FormatInt(v.Int(), 10))
}
return nil
}
func (m *stringifier) stringifyUint(v reflect.Value, fi *fieldInstance, depth int) error {
if fi.is(fieldISO639_2) {
m.buf.WriteString(string([]byte{byte(v.Uint() + 0x60)}))
} else if fi.is(fieldUUID) {
fmt.Fprintf(m.buf, "%02x", v.Uint())
} else if fi.is(fieldString) {
m.buf.WriteString(string([]byte{byte(v.Uint())}))
} else if fi.is(fieldHex) || (!fi.is(fieldDec) && v.Type().Kind() == reflect.Uint8) || v.Type().Kind() == reflect.Uintptr {
m.buf.WriteString("0x")
m.buf.WriteString(strconv.FormatUint(v.Uint(), 16))
} else {
m.buf.WriteString(strconv.FormatUint(v.Uint(), 10))
}
return nil
}
func (m *stringifier) stringifyBool(v reflect.Value, depth int) error {
m.buf.WriteString(strconv.FormatBool(v.Bool()))
return nil
}
func (m *stringifier) stringifyString(v reflect.Value, depth int) error {
m.buf.WriteString("\"")
m.buf.WriteString(util.EscapeUnprintables(v.String()))
m.buf.WriteString("\"")
return nil
}
func writeIndent(w io.Writer, indent string, depth int) {
for i := 0; i < depth; i++ {
io.WriteString(w, indent)
}
}