-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_encode.go
388 lines (325 loc) · 9.68 KB
/
text_encode.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package gg
import (
"encoding"
"fmt"
r "reflect"
"strconv"
"time"
u "unsafe"
)
/*
Shortcut for implementing string encoding of `Nullable` types.
Mostly for internal use.
*/
func StringNull[A any, B NullableValGetter[A]](val B) string {
if val.IsNull() {
return ``
}
return String(val.Get())
}
/*
Alias for `fmt.Sprint` defined as a generic function for compatibility with
higher-order functions like `Map`. Slightly more efficient than `fmt.Sprint`:
avoids spurious heap escape and copying.
The output of this function is intended only for debug purposes. For machine
consumption or user display, use `String`, which is more restrictive.
*/
func StringAny[A any](val A) string { return fmt.Sprint(AnyNoEscUnsafe(val)) }
// Stringifies an arbitrary value via `StringCatch`. Panics on errors.
func String[A any](val A) string { return Try1(StringCatch(val)) }
/*
Missing feature of the standard library. Converts an arbitrary value to a
string, allowing only INTENTIONALLY stringable values. Rules:
* Nil is considered "".
* A string is returned as-is.
* A byte slice is cast to a string.
* Any other primitive value (see constraint `Prim`) is encoded via `strconv`.
* Types that support `fmt.Stringer`, `AppenderTo` or `encoding.TextMarshaler`
are encoded by using the corresponding method.
* As a special case, `time.Time` is encoded in `time.RFC3339` to make encoding
and decoding automatically reversible, and generally for better
compatibility with machine parsing. This format is already used by
`time.Time.MarshalText`, `time.Time.MarshalJSON`, and the corresponding
unmarshaling methods. The different format used by `time.Time.String` tends
to be an inconvenience, one we rectify here.
* Any other type causes an error.
*/
func StringCatch[A any](val A) (string, error) {
box := AnyNoEscUnsafe(val)
// Must be handled before `fmt.Stringer`.
//
// Note that our `AppendTo` doesn't need this clause because it uses
// `time.Time.MarshalText`, which uses our preferred format. Yet
// another reason why we need this special case here.
inst, ok := box.(time.Time)
if ok {
return inst.Format(time.RFC3339), nil
}
stringer, _ := box.(fmt.Stringer)
if stringer != nil {
return stringer.String(), nil
}
appender, _ := box.(AppenderTo)
if appender != nil {
return ToString(appender.AppendTo(nil)), nil
}
marshaler, _ := box.(encoding.TextMarshaler)
if marshaler != nil {
val, err := marshaler.MarshalText()
return ToString(val), err
}
switch val := box.(type) {
case nil:
return ``, nil
case string:
return val, nil
case []byte:
return ToString(val), nil
case bool:
return strconv.FormatBool(val), nil
case int8:
return strconv.FormatInt(int64(val), 10), nil
case int16:
return strconv.FormatInt(int64(val), 10), nil
case int32:
return strconv.FormatInt(int64(val), 10), nil
case int64:
return strconv.FormatInt(int64(val), 10), nil
case int:
return strconv.FormatInt(int64(val), 10), nil
case uint8:
return strconv.FormatUint(uint64(val), 10), nil
case uint16:
return strconv.FormatUint(uint64(val), 10), nil
case uint32:
return strconv.FormatUint(uint64(val), 10), nil
case uint64:
return strconv.FormatUint(uint64(val), 10), nil
case uint:
return strconv.FormatUint(uint64(val), 10), nil
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 64), nil
case float64:
return strconv.FormatFloat(float64(val), 'f', -1, 64), nil
default:
return StringReflectCatch(r.ValueOf(box))
}
}
/*
Reflection-based component of `StringCatch`.
Mostly for internal use.
*/
func StringReflectCatch(val r.Value) (string, error) {
if !val.IsValid() {
return ``, nil
}
typ := val.Type()
switch typ.Kind() {
case r.String:
return val.String(), nil
case r.Bool:
return strconv.FormatBool(val.Bool()), nil
case r.Int8, r.Int16, r.Int32, r.Int64, r.Int:
return strconv.FormatInt(val.Int(), 10), nil
case r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uint:
return strconv.FormatUint(val.Uint(), 10), nil
case r.Float32, r.Float64:
return strconv.FormatFloat(val.Float(), 'f', -1, 64), nil
default:
return ValueToStringCatch(val)
}
}
/*
Shortcut for implementing string encoding of `Nullable` types.
Mostly for internal use.
*/
func AppendNull[A any, B NullableValGetter[A]](buf []byte, src B) []byte {
if src.IsNull() {
return buf
}
return AppendTo(buf, src.Get())
}
/*
Appends text representation of the input to the given buffer,
using `AppendCatch`. Panics on errors.
*/
func AppendTo[A ~[]byte, B any](buf A, src B) A {
return Try1(AppendCatch(buf, src))
}
/*
Same as `StringCatch`, but instead of returning a string, appends the text
representation of the input to the given buffer. See `StringCatch` for the
encoding rules.
*/
func AppendCatch[A ~[]byte, B any](buf A, src B) (A, error) {
box := AnyNoEscUnsafe(src)
switch val := box.(type) {
case nil:
return buf, nil
case string:
return append(buf, val...), nil
case []byte:
return append(buf, val...), nil
case bool:
return strconv.AppendBool(buf, val), nil
case int8:
return strconv.AppendInt(buf, int64(val), 10), nil
case int16:
return strconv.AppendInt(buf, int64(val), 10), nil
case int32:
return strconv.AppendInt(buf, int64(val), 10), nil
case int64:
return strconv.AppendInt(buf, int64(val), 10), nil
case int:
return strconv.AppendInt(buf, int64(val), 10), nil
case uint8:
return strconv.AppendUint(buf, uint64(val), 10), nil
case uint16:
return strconv.AppendUint(buf, uint64(val), 10), nil
case uint32:
return strconv.AppendUint(buf, uint64(val), 10), nil
case uint64:
return strconv.AppendUint(buf, uint64(val), 10), nil
case uint:
return strconv.AppendUint(buf, uint64(val), 10), nil
case float32:
return strconv.AppendFloat(buf, float64(val), 'f', -1, 64), nil
case float64:
return strconv.AppendFloat(buf, float64(val), 'f', -1, 64), nil
default:
appender, _ := val.(AppenderTo)
if appender != nil {
return appender.AppendTo(buf), nil
}
marshaler, _ := val.(encoding.TextMarshaler)
if marshaler != nil {
val, err := marshaler.MarshalText()
return append(buf, val...), err
}
stringer, _ := val.(fmt.Stringer)
if stringer != nil {
return append(buf, stringer.String()...), nil
}
return AppendReflectCatch(buf, r.ValueOf(box))
}
}
/*
Reflection-based component of `AppendCatch`.
Mostly for internal use.
*/
func AppendReflectCatch[A ~[]byte](buf A, val r.Value) (A, error) {
if !val.IsValid() {
return buf, nil
}
typ := val.Type()
switch typ.Kind() {
case r.String:
return append(buf, val.String()...), nil
case r.Bool:
return strconv.AppendBool(buf, val.Bool()), nil
case r.Int8, r.Int16, r.Int32, r.Int64, r.Int:
return strconv.AppendInt(buf, val.Int(), 10), nil
case r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uint:
return strconv.AppendUint(buf, val.Uint(), 10), nil
case r.Float32, r.Float64:
return strconv.AppendFloat(buf, val.Float(), 'f', -1, 64), nil
default:
str, err := ValueToStringCatch(val)
return append(buf, str...), err
}
}
/*
Shortcut for implementing `encoding.TextMarshaler` on arbitrary types. Mostly
for internal use. Uses `StringCatch` internally. The resulting bytes may be
backed by constant storage and must not be mutated.
*/
func Marshal(val any) ([]byte, error) {
str, err := StringCatch(val)
return ToBytes(str), err
}
/*
Shortcut for implementing `encoding.TextMarshaler` on `Nullable` types. Mostly
for internal use. Uses `StringCatch` internally. The resulting bytes may be
backed by constant storage and must not be mutated.
*/
func MarshalNullCatch[A any, B NullableValGetter[A]](val B) ([]byte, error) {
if val.IsNull() {
return nil, nil
}
return Marshal(val.Get())
}
/*
Shortcut for stringifying a type that implements `AppenderTo`.
Mostly for internal use.
*/
func AppenderString[A AppenderTo](val A) string {
return ToString(val.AppendTo(nil))
}
/*
Appends the `fmt.GoStringer` representation of the given input to the given
buffer. Also see the function `GoString`.
*/
func AppendGoString[A any](inout []byte, val A) []byte {
buf := Buf(inout)
box := AnyNoEscUnsafe(val)
impl, _ := box.(fmt.GoStringer)
if impl != nil {
buf.AppendString(impl.GoString())
return buf
}
fmt.Fprintf(NoEscUnsafe(&buf), `%#v`, box)
return buf
}
/*
Returns the `fmt.GoStringer` representation of the given input.
Equivalent to `fmt.Sprintf("%#v", val)` but marginally more efficient.
*/
func GoString[A any](val A) string {
box := AnyNoEscUnsafe(val)
impl, _ := box.(fmt.GoStringer)
if impl != nil {
return impl.GoString()
}
return fmt.Sprintf(`%#v`, box)
}
/*
Shortcut for `strconv.Quote(String(val))`.
Encodes an arbitrary value to a string and quotes it.
*/
func Quote[A any](val A) string { return strconv.Quote(String(val)) }
/*
Returns a representation of the bits in the given machine number, using the
traditional big-endian ordering, starting with the most significant bit, which
represents the sign in signed integers. The representation is always padded to
the full number of bits: 8 for uint8/int8, 16 for uint16/int16, and so on. For
unsigned numbers, this is equivalent to using `fmt.Sprintf` with the `%b` flag
and appropriate padding. For signed numbers, this is not equivalent.
*/
func NumBits[A Int](src A) string {
size := u.Sizeof(src)
var num uint64
switch size {
case 1:
num = uint64(CastUnsafe[uint8](src))
case 2:
num = uint64(CastUnsafe[uint16](src))
case 4:
num = uint64(CastUnsafe[uint32](src))
case 8:
num = uint64(CastUnsafe[uint64](src))
default:
panic(Errf(`unsupported sizeof %T: %v`, src, size))
}
buf0 := make(Buf, size*8)
buf1 := strconv.AppendUint(buf0[:0], num, 2)
len0 := len(buf0)
len1 := len(buf1)
off := len0 - len1
if off > 0 {
copy(buf0[off:], buf1)
for ind := range buf0[:off] {
buf0[ind] = '0'
}
}
return buf0.String()
}