diff options
| author | 2025-08-21 16:41:50 +0200 | |
|---|---|---|
| committer | 2025-08-21 16:41:50 +0200 | |
| commit | a79f83cbded3fb1165b338ce316dc9f2217467ff (patch) | |
| tree | 4b4a2b7084aced51c0a691cee6352b4ecae90df8 /vendor/codeberg.org/gruf/go-mangler/v2/mangle.go | |
| parent | [chore] bump golangci-lint version in CI, disable var-naming package name che... (diff) | |
| download | gotosocial-a79f83cbded3fb1165b338ce316dc9f2217467ff.tar.xz | |
[chore] update dependencies (#4386)
- codeberg.org/gruf/go-bytesize v1.0.3 -> v1.0.4
- codeberg.org/gruf/go-kv/v2 v2.0.6 -> v2.0.7
- codeberg.org/gruf/go-mutexes v1.5.2 -> v1.5.3
- codeberg.org/gruf/go-structr v0.9.7 -> v0.9.8
- codeberg.org/gruf/go-ffmpreg v0.6.8 -> v0.6.9
- github.com/tomnomnom/linkheader HEAD@2018 -> HEAD@2025
all of the above codeberg.org/gruf updates are in preparation for Go1.25, except for bytesize, and also ffmpreg which is a rebuild with the latest version of ffmpeg (v5.1.7)
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4386
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mangler/v2/mangle.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-mangler/v2/mangle.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-mangler/v2/mangle.go b/vendor/codeberg.org/gruf/go-mangler/v2/mangle.go new file mode 100644 index 000000000..0c746fc95 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-mangler/v2/mangle.go @@ -0,0 +1,93 @@ +package mangler + +import ( + "fmt" + "unsafe" + + "codeberg.org/gruf/go-xunsafe" +) + +// Mangler is a function that will take an input value of known type, +// and append it in mangled serialized form to the given byte buffer. +type Mangler func(buf []byte, ptr unsafe.Pointer) []byte + +// Get will fetch the Mangler function for given runtime type information. +// The required argument is of type xunsafe.TypeIter{} as unsafe pointer +// access requires further contextual information like type nesting. +func Get(t xunsafe.TypeIter) Mangler { + t.Parent = nil // enforce type prefix + fn := loadOrStore(t) + if fn == nil { + panic(fmt.Sprintf("cannot mangle type: %s", t.Type)) + } + return fn +} + +// GetNoLoad is functionally similar to Get(), +// without caching the resulting Mangler. +func GetNoLoad(t xunsafe.TypeIter) Mangler { + t.Parent = nil // enforce type prefix + fn := loadOrGet(t) + if fn == nil { + panic(fmt.Sprintf("cannot mangle type: %s", t.Type)) + } + return fn +} + +// Append will append the mangled form of input value 'a' to buffer 'b'. +// +// See mangler.String() for more information on mangled output. +func Append(b []byte, a any) []byte { + t := xunsafe.TypeIterFrom(a) + p := xunsafe.UnpackEface(a) + return Get(t)(b, p) +} + +// AppendMulti appends all mangled forms of input value(s) 'a' to buffer 'b' +// separated by colon characters. When all type manglers are currently cached +// for all types in 'a', this will be faster than multiple calls to Append(). +// +// See mangler.String() for more information on mangled output. +func AppendMulti(b []byte, a ...any) []byte { + if p := manglers.load(); p != nil { + b4 := len(b) + for _, a := range a { + t := xunsafe.TypeIterFrom(a) + m := (*p)[t.TypeInfo] + if m == nil { + b = b[:b4] + goto slow + } + b = m(b, xunsafe.UnpackEface(a)) + b = append(b, '.') + } + return b + } +slow: + for _, a := range a { + b = Append(b, a) + b = append(b, '.') + } + return b +} + +// String will return the mangled format of input value 'a'. This +// mangled output will be unique for all default supported input types +// during a single runtime instance. Uniqueness cannot be guaranteed +// between separate runtime instances (whether running concurrently, or +// the same application running at different times). +// +// The exact formatting of the output data should not be relied upon, +// only that it is unique given the above constraints. Generally though, +// the mangled output is the binary formatted text of given input data. +// +// Uniqueness is guaranteed for similar input data of differing types +// (e.g. string("hello world") vs. []byte("hello world")) by prefixing +// mangled output with the input data's runtime type pointer. +// +// Default supported types include all concrete (i.e. non-interface{}) +// data types, and interfaces implementing Mangleable{}. +func String(a any) string { + b := Append(make([]byte, 0, 32), a) + return *(*string)(unsafe.Pointer(&b)) +} |
