summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mangler/manglers.go
blob: b9ba817056f1405632a53caed0112869a44a316c (plain)
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
package mangler

import (
	"math/bits"
	_ "unsafe"
)

// Notes:
//   the use of unsafe conversion from the direct interface values to
//   the chosen types in each of the below functions allows us to convert
//   not only those types directly, but anything type-aliased to those
//   types. e.g. `time.Duration` directly as int64.

func mangle_string(buf []byte, a any) []byte {
	return append(buf, *(*string)(iface_value(a))...)
}

func mangle_string_ptr(buf []byte, a any) []byte {
	if ptr := (*string)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		return append(buf, *ptr...)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_string_slice(buf []byte, a any) []byte {
	s := *(*[]string)(iface_value(a))
	for _, s := range s {
		buf = append(buf, s...)
		buf = append(buf, ',')
	}
	if len(s) > 0 {
		buf = buf[:len(buf)-1]
	}
	return buf
}

func mangle_bool(buf []byte, a any) []byte {
	if *(*bool)(iface_value(a)) {
		return append(buf, '1')
	}
	return append(buf, '0')
}

func mangle_bool_ptr(buf []byte, a any) []byte {
	if ptr := (*bool)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		if *ptr {
			return append(buf, '1')
		}
		return append(buf, '0')
	}
	buf = append(buf, '0')
	return buf
}

func mangle_bool_slice(buf []byte, a any) []byte {
	for _, b := range *(*[]bool)(iface_value(a)) {
		if b {
			buf = append(buf, '1')
		} else {
			buf = append(buf, '0')
		}
	}
	return buf
}

func mangle_8bit(buf []byte, a any) []byte {
	return append(buf, *(*uint8)(iface_value(a)))
}

func mangle_8bit_ptr(buf []byte, a any) []byte {
	if ptr := (*uint8)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		return append(buf, *ptr)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_8bit_slice(buf []byte, a any) []byte {
	return append(buf, *(*[]uint8)(iface_value(a))...)
}

func mangle_16bit(buf []byte, a any) []byte {
	return append_uint16(buf, *(*uint16)(iface_value(a)))
}

func mangle_16bit_ptr(buf []byte, a any) []byte {
	if ptr := (*uint16)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		return append_uint16(buf, *ptr)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_16bit_slice(buf []byte, a any) []byte {
	for _, u := range *(*[]uint16)(iface_value(a)) {
		buf = append_uint16(buf, u)
	}
	return buf
}

func mangle_32bit(buf []byte, a any) []byte {
	return append_uint32(buf, *(*uint32)(iface_value(a)))
}

func mangle_32bit_ptr(buf []byte, a any) []byte {
	if ptr := (*uint32)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		return append_uint32(buf, *ptr)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_32bit_slice(buf []byte, a any) []byte {
	for _, u := range *(*[]uint32)(iface_value(a)) {
		buf = append_uint32(buf, u)
	}
	return buf
}

func mangle_64bit(buf []byte, a any) []byte {
	return append_uint64(buf, *(*uint64)(iface_value(a)))
}

func mangle_64bit_ptr(buf []byte, a any) []byte {
	if ptr := (*uint64)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		return append_uint64(buf, *ptr)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_64bit_slice(buf []byte, a any) []byte {
	for _, u := range *(*[]uint64)(iface_value(a)) {
		buf = append_uint64(buf, u)
	}
	return buf
}

// mangle_platform_int contains the correct iface mangler on runtime for platform int size.
var mangle_platform_int = func() Mangler {
	switch bits.UintSize {
	case 32:
		return mangle_32bit
	case 64:
		return mangle_64bit
	default:
		panic("unexpected platform int size")
	}
}()

// mangle_platform_int_ptr contains the correct iface mangler on runtime for platform int size.
var mangle_platform_int_ptr = func() Mangler {
	switch bits.UintSize {
	case 32:
		return mangle_32bit_ptr
	case 64:
		return mangle_64bit_ptr
	default:
		panic("unexpected platform int size")
	}
}()

// mangle_platform_int_slice contains the correct iface mangler on runtime for platform int size.
var mangle_platform_int_slice = func() Mangler {
	switch bits.UintSize {
	case 32:
		return mangle_32bit_slice
	case 64:
		return mangle_64bit_slice
	default:
		panic("unexpected platform int size")
	}
}()

// uint128 provides an easily mangleable data type for 128bit data types to be cast into.
type uint128 [2]uint64

func mangle_128bit(buf []byte, a any) []byte {
	u2 := *(*uint128)(iface_value(a))
	buf = append_uint64(buf, u2[0])
	buf = append_uint64(buf, u2[1])
	return buf
}

func mangle_128bit_ptr(buf []byte, a any) []byte {
	if ptr := (*uint128)(iface_value(a)); ptr != nil {
		buf = append(buf, '1')
		buf = append_uint64(buf, (*ptr)[0])
		buf = append_uint64(buf, (*ptr)[1])
	}
	buf = append(buf, '0')
	return buf
}

func mangle_128bit_slice(buf []byte, a any) []byte {
	for _, u2 := range *(*[]uint128)(iface_value(a)) {
		buf = append_uint64(buf, u2[0])
		buf = append_uint64(buf, u2[1])
	}
	return buf
}

func mangle_mangled(buf []byte, a any) []byte {
	if v := a.(Mangled); v != nil {
		buf = append(buf, '1')
		return v.Mangle(buf)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_binary(buf []byte, a any) []byte {
	if v := a.(binarymarshaler); v != nil {
		b, err := v.MarshalBinary()
		if err != nil {
			panic("mangle_binary: " + err.Error())
		}
		buf = append(buf, '1')
		return append(buf, b...)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_stringer(buf []byte, a any) []byte {
	if v := a.(stringer); v != nil {
		buf = append(buf, '1')
		return append(buf, v.String()...)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_text(buf []byte, a any) []byte {
	if v := a.(textmarshaler); v != nil {
		b, err := v.MarshalText()
		if err != nil {
			panic("mangle_text: " + err.Error())
		}
		buf = append(buf, '1')
		return append(buf, b...)
	}
	buf = append(buf, '0')
	return buf
}

func mangle_json(buf []byte, a any) []byte {
	if v := a.(jsonmarshaler); v != nil {
		b, err := v.MarshalJSON()
		if err != nil {
			panic("mangle_json: " + err.Error())
		}
		buf = append(buf, '1')
		return append(buf, b...)
	}
	buf = append(buf, '0')
	return buf
}