summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mangler
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2025-03-09 17:47:56 +0100
committerLibravatar Terin Stock <terinjokes@gmail.com>2025-03-10 01:59:49 +0100
commit3ac1ee16f377d31a0fb80c8dae28b6239ac4229e (patch)
treef61faa581feaaeaba2542b9f2b8234a590684413 /vendor/codeberg.org/gruf/go-mangler
parent[chore] update URLs to forked source (diff)
downloadgotosocial-3ac1ee16f377d31a0fb80c8dae28b6239ac4229e.tar.xz
[chore] remove vendor
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mangler')
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/LICENSE9
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/README.md31
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/helpers.go200
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/load.go223
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/mangle.go130
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/manglers.go113
6 files changed, 0 insertions, 706 deletions
diff --git a/vendor/codeberg.org/gruf/go-mangler/LICENSE b/vendor/codeberg.org/gruf/go-mangler/LICENSE
deleted file mode 100644
index dffbdf0c9..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/LICENSE
+++ /dev/null
@@ -1,9 +0,0 @@
-MIT License
-
-Copyright (c) 2023 gruf
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/codeberg.org/gruf/go-mangler/README.md b/vendor/codeberg.org/gruf/go-mangler/README.md
deleted file mode 100644
index 636c2494c..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/README.md
+++ /dev/null
@@ -1,31 +0,0 @@
-# go-mangler
-
-[Documentation](https://pkg.go.dev/codeberg.org/gruf/go-mangler).
-
-To put it simply is a bit of an odd library. It aims to provide incredibly fast, unique string outputs for all default supported input data types during a given runtime instance. See `mangler.String()`for supported types.
-
-It is useful, for example, for use as part of larger abstractions involving hashmaps. That was my particular usecase anyways...
-
-This package does make liberal use of the "unsafe" package.
-
-Benchmarks are below. Please note the more important thing to notice here is the relative difference in benchmark scores, the actual `ns/op`,`B/op`,`allocs/op` accounts for running through ~80 possible test cases, including some not-ideal situations.
-
-The choice of libraries in the benchmark are just a selection of libraries that could be used in a similar manner to this one, i.e. serializing in some manner.
-
-```
-go test -run=none -benchmem -gcflags=all='-l=4' -bench=.*
-goos: linux
-goarch: amd64
-pkg: codeberg.org/gruf/go-mangler
-cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
-BenchmarkMangle-8 1278526 966.0 ns/op 0 B/op 0 allocs/op
-BenchmarkMangleKnown-8 3443587 345.9 ns/op 0 B/op 0 allocs/op
-BenchmarkJSON-8 228962 4717 ns/op 1849 B/op 99 allocs/op
-BenchmarkLoosy-8 307194 3447 ns/op 776 B/op 65 allocs/op
-BenchmarkFmt-8 150254 7405 ns/op 1377 B/op 143 allocs/op
-BenchmarkFxmackerCbor-8 364411 3037 ns/op 1224 B/op 105 allocs/op
-BenchmarkMitchellhHashStructure-8 102272 11268 ns/op 8996 B/op 1000 allocs/op
-BenchmarkCnfStructhash-8 6789 168703 ns/op 288301 B/op 5779 allocs/op
-PASS
-ok codeberg.org/gruf/go-mangler 11.715s
-```
diff --git a/vendor/codeberg.org/gruf/go-mangler/helpers.go b/vendor/codeberg.org/gruf/go-mangler/helpers.go
deleted file mode 100644
index 4e37e1344..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/helpers.go
+++ /dev/null
@@ -1,200 +0,0 @@
-package mangler
-
-import (
- "reflect"
- "unsafe"
-)
-
-func append_uint16(b []byte, u uint16) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- )
-}
-
-func append_uint32(b []byte, u uint32) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- byte(u>>16),
- byte(u>>24),
- )
-}
-
-func append_uint64(b []byte, u uint64) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- byte(u>>16),
- byte(u>>24),
- byte(u>>32),
- byte(u>>40),
- byte(u>>48),
- byte(u>>56),
- )
-}
-
-type typecontext struct {
- ntype reflect.Type
- rtype reflect.Type
-}
-
-func deref_ptr_mangler(ctx typecontext, mangle Mangler, n uint) Mangler {
- if mangle == nil || n == 0 {
- panic("bad input")
- }
-
- // Non-nested value types,
- // i.e. just direct ptrs to
- // primitives require one
- // less dereference to ptr.
- if ctx.ntype == nil {
- n--
- }
-
- return func(buf []byte, ptr unsafe.Pointer) []byte {
-
- // Deref n number times.
- for i := n; i > 0; i-- {
-
- if ptr == nil {
- // Check for nil values
- buf = append(buf, '0')
- return buf
- }
-
- // Further deref ptr
- buf = append(buf, '1')
- ptr = *(*unsafe.Pointer)(ptr)
- }
-
- if ptr == nil {
- // Final nil val check
- buf = append(buf, '0')
- return buf
- }
-
- // Mangle fully deref'd
- buf = append(buf, '1')
- buf = mangle(buf, ptr)
- return buf
- }
-}
-
-func iter_slice_mangler(ctx typecontext, mangle Mangler) Mangler {
- if ctx.rtype == nil || mangle == nil {
- panic("bad input")
- }
-
- // memory size of elem.
- esz := ctx.rtype.Size()
-
- return func(buf []byte, ptr unsafe.Pointer) []byte {
- // Get data as slice hdr.
- hdr := (*slice_header)(ptr)
-
- for i := 0; i < hdr.len; i++ {
- // Mangle data at slice index.
- eptr := array_at(hdr.data, esz, i)
- buf = mangle(buf, eptr)
- buf = append(buf, ',')
- }
-
- if hdr.len > 0 {
- // Drop final comma.
- buf = buf[:len(buf)-1]
- }
-
- return buf
- }
-}
-
-func iter_array_mangler(ctx typecontext, mangle Mangler) Mangler {
- if ctx.rtype == nil || mangle == nil {
- panic("bad input")
- }
-
- // no. array elements.
- n := ctx.ntype.Len()
-
- // memory size of elem.
- esz := ctx.rtype.Size()
-
- return func(buf []byte, ptr unsafe.Pointer) []byte {
- for i := 0; i < n; i++ {
- // Mangle data at array index.
- offset := esz * uintptr(i)
- eptr := add(ptr, offset)
- buf = mangle(buf, eptr)
- buf = append(buf, ',')
- }
-
- if n > 0 {
- // Drop final comma.
- buf = buf[:len(buf)-1]
- }
-
- return buf
- }
-}
-
-func iter_struct_mangler(ctx typecontext, manglers []Mangler) Mangler {
- if ctx.rtype == nil || len(manglers) != ctx.rtype.NumField() {
- panic("bad input")
- }
-
- type field struct {
- mangle Mangler
- offset uintptr
- }
-
- // Bundle together the fields and manglers.
- fields := make([]field, ctx.rtype.NumField())
- for i := range fields {
- rfield := ctx.rtype.FieldByIndex([]int{i})
- fields[i].offset = rfield.Offset
- fields[i].mangle = manglers[i]
- if fields[i].mangle == nil {
- panic("bad input")
- }
- }
-
- return func(buf []byte, ptr unsafe.Pointer) []byte {
- for i := range fields {
- // Get struct field ptr via offset.
- fptr := add(ptr, fields[i].offset)
-
- // Mangle the struct field data.
- buf = fields[i].mangle(buf, fptr)
- buf = append(buf, ',')
- }
-
- if len(fields) > 0 {
- // Drop final comma.
- buf = buf[:len(buf)-1]
- }
-
- return buf
- }
-}
-
-// array_at returns ptr to index in array at ptr, given element size.
-func array_at(ptr unsafe.Pointer, esz uintptr, i int) unsafe.Pointer {
- return unsafe.Pointer(uintptr(ptr) + esz*uintptr(i))
-}
-
-// add returns the ptr addition of starting ptr and a delta.
-func add(ptr unsafe.Pointer, delta uintptr) unsafe.Pointer {
- return unsafe.Pointer(uintptr(ptr) + delta)
-}
-
-type slice_header struct {
- data unsafe.Pointer
- len int
- cap int
-}
-
-func eface_data(a any) unsafe.Pointer {
- type eface struct{ _, data unsafe.Pointer }
- return (*eface)(unsafe.Pointer(&a)).data
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/load.go b/vendor/codeberg.org/gruf/go-mangler/load.go
deleted file mode 100644
index bc79d381e..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/load.go
+++ /dev/null
@@ -1,223 +0,0 @@
-package mangler
-
-import (
- "reflect"
-)
-
-// loadMangler is the top-most Mangler load function. It guarantees that a Mangler
-// function will be returned for given value interface{} and reflected type. Else panics.
-func loadMangler(t reflect.Type) Mangler {
- ctx := typecontext{rtype: t}
-
- // Load mangler fn
- mng := load(ctx)
- if mng != nil {
- return mng
- }
-
- // No mangler function could be determined
- panic("cannot mangle type: " + t.String())
-}
-
-// load will load a Mangler or reflect Mangler for given type and iface 'a'.
-// Note: allocates new interface value if nil provided, i.e. if coming via reflection.
-func load(ctx typecontext) Mangler {
- if ctx.rtype == nil {
- // There is no reflect type to search by
- panic("cannot mangle nil interface{} type")
- }
-
- // Search by reflection.
- mng := loadReflect(ctx)
- if mng != nil {
- return mng
- }
-
- return nil
-}
-
-// loadReflect will load a Mangler (or rMangler) function for the given reflected type info.
-// NOTE: this is used as the top level load function for nested reflective searches.
-func loadReflect(ctx typecontext) Mangler {
- switch ctx.rtype.Kind() {
- case reflect.Pointer:
- return loadReflectPtr(ctx)
-
- case reflect.String:
- return mangle_string
-
- case reflect.Struct:
- return loadReflectStruct(ctx)
-
- case reflect.Array:
- return loadReflectArray(ctx)
-
- case reflect.Slice:
- return loadReflectSlice(ctx)
-
- case reflect.Bool:
- return mangle_bool
-
- case reflect.Int,
- reflect.Uint,
- reflect.Uintptr:
- return mangle_int
-
- case reflect.Int8, reflect.Uint8:
- return mangle_8bit
-
- case reflect.Int16, reflect.Uint16:
- return mangle_16bit
-
- case reflect.Int32, reflect.Uint32:
- return mangle_32bit
-
- case reflect.Int64, reflect.Uint64:
- return mangle_64bit
-
- case reflect.Float32:
- return mangle_32bit
-
- case reflect.Float64:
- return mangle_64bit
-
- case reflect.Complex64:
- return mangle_64bit
-
- case reflect.Complex128:
- return mangle_128bit
-
- default:
- return nil
- }
-}
-
-// loadReflectPtr loads a Mangler (or rMangler) function for a ptr's element type.
-// This also handles further dereferencing of any further ptr indrections (e.g. ***int).
-func loadReflectPtr(ctx typecontext) Mangler {
- var n uint
-
- // Iteratively dereference ptrs
- for ctx.rtype.Kind() == reflect.Pointer {
- ctx.rtype = ctx.rtype.Elem()
- n++
- }
-
- // Search for elemn type mangler.
- if mng := load(ctx); mng != nil {
- return deref_ptr_mangler(ctx, mng, n)
- }
-
- return nil
-}
-
-// loadReflectKnownSlice loads a Mangler function for a
-// known slice-of-element type (in this case, primtives).
-func loadReflectKnownSlice(ctx typecontext) Mangler {
- switch ctx.rtype.Kind() {
- case reflect.String:
- return mangle_string_slice
-
- case reflect.Bool:
- return mangle_bool_slice
-
- case reflect.Int,
- reflect.Uint,
- reflect.Uintptr:
- return mangle_int_slice
-
- case reflect.Int8, reflect.Uint8:
- return mangle_8bit_slice
-
- case reflect.Int16, reflect.Uint16:
- return mangle_16bit_slice
-
- case reflect.Int32, reflect.Uint32:
- return mangle_32bit_slice
-
- case reflect.Int64, reflect.Uint64:
- return mangle_64bit_slice
-
- case reflect.Float32:
- return mangle_32bit_slice
-
- case reflect.Float64:
- return mangle_64bit_slice
-
- case reflect.Complex64:
- return mangle_64bit_slice
-
- case reflect.Complex128:
- return mangle_128bit_slice
-
- default:
- return nil
- }
-}
-
-// loadReflectSlice ...
-func loadReflectSlice(ctx typecontext) Mangler {
- // Set nesting type.
- ctx.ntype = ctx.rtype
-
- // Get nested element type.
- ctx.rtype = ctx.rtype.Elem()
-
- // Preferably look for known slice mangler func
- if mng := loadReflectKnownSlice(ctx); mng != nil {
- return mng
- }
-
- // Use nested mangler iteration.
- if mng := load(ctx); mng != nil {
- return iter_slice_mangler(ctx, mng)
- }
-
- return nil
-}
-
-// loadReflectArray ...
-func loadReflectArray(ctx typecontext) Mangler {
- // Set nesting type.
- ctx.ntype = ctx.rtype
-
- // Get nested element type.
- ctx.rtype = ctx.rtype.Elem()
-
- // Use manglers for nested iteration.
- if mng := load(ctx); mng != nil {
- return iter_array_mangler(ctx, mng)
- }
-
- return nil
-}
-
-// loadReflectStruct ...
-func loadReflectStruct(ctx typecontext) Mangler {
- var mngs []Mangler
-
- // Set nesting type.
- ctx.ntype = ctx.rtype
-
- // Gather manglers for all fields.
- for i := 0; i < ctx.ntype.NumField(); i++ {
-
- // Field typectx.
- ctx := typecontext{
- ntype: ctx.ntype,
- rtype: ctx.ntype.Field(i).Type,
- }
-
- // Load mangler.
- mng := load(ctx)
- if mng == nil {
- return nil
- }
-
- // Append next to map.
- mngs = append(mngs, mng)
- }
-
- // Use manglers for nested iteration.
- return iter_struct_mangler(ctx, mngs)
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/mangle.go b/vendor/codeberg.org/gruf/go-mangler/mangle.go
deleted file mode 100644
index 576286682..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/mangle.go
+++ /dev/null
@@ -1,130 +0,0 @@
-package mangler
-
-import (
- "reflect"
- "sync"
- "unsafe"
-)
-
-// manglers is a map of runtime
-// type ptrs => Mangler functions.
-var manglers sync.Map
-
-// Mangler is a function that will take an input interface value of known
-// type, and append it in mangled serialized form to the given byte buffer.
-// While the value type is an interface, the Mangler functions are accessed
-// by the value's runtime type pointer, allowing the input value type to be known.
-type Mangler func(buf []byte, ptr unsafe.Pointer) []byte
-
-// Get will fetch the Mangler function for given runtime type.
-// Note that the returned mangler will be a no-op in the case
-// that an incorrect type is passed as the value argument.
-func Get(t reflect.Type) Mangler {
- var mng Mangler
-
- // Get raw runtime type ptr
- uptr := uintptr(eface_data(t))
-
- // Look for a cached mangler
- v, ok := manglers.Load(uptr)
-
- if !ok {
- // Load mangler function
- mng = loadMangler(t)
- } else {
- // cast cached value
- mng = v.(Mangler)
- }
-
- return func(buf []byte, ptr unsafe.Pointer) []byte {
- // First write the type ptr (this adds
- // a unique prefix for each runtime type).
- buf = append_uint64(buf, uint64(uptr))
-
- // Finally, mangle value
- return mng(buf, ptr)
- }
-}
-
-// Register will register the given Mangler function for use with vars of given runtime type. This allows
-// registering performant manglers for existing types not implementing Mangled (e.g. std library types).
-// NOTE: panics if there already exists a Mangler function for given type. Register on init().
-func Register(t reflect.Type, m Mangler) {
- if t == nil {
- // Nil interface{} types cannot be searched by, do not accept
- panic("cannot register mangler for nil interface{} type")
- }
-
- // Get raw runtime type ptr
- uptr := uintptr(eface_data(t))
-
- // Ensure this is a unique encoder
- if _, ok := manglers.Load(uptr); ok {
- panic("already registered mangler for type: " + t.String())
- }
-
- // Cache this encoder func
- manglers.Store(uptr, m)
-}
-
-// 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 {
- var mng Mangler
-
- // Get reflect type of 'a'
- t := reflect.TypeOf(a)
-
- // Get raw runtime type ptr
- uptr := uintptr(eface_data(t))
-
- // Look for a cached mangler
- v, ok := manglers.Load(uptr)
-
- if !ok {
- // Load into cache
- mng = loadMangler(t)
- manglers.Store(uptr, mng)
- } else {
- // cast cached value
- mng = v.(Mangler)
- }
-
- // First write the type ptr (this adds
- // a unique prefix for each runtime type).
- b = append_uint64(b, uint64(uptr))
-
- // Finally, mangle value
- ptr := eface_data(a)
- return mng(b, ptr)
-}
-
-// 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:
-// - string
-// - bool
-// - int,int8,int16,int32,int64
-// - uint,uint8,uint16,uint32,uint64,uintptr
-// - float32,float64
-// - complex64,complex128
-// - arbitrary structs
-// - all type aliases of above
-// - all pointers to the above
-// - all slices / arrays of the above
-func String(a any) string {
- b := Append(make([]byte, 0, 32), a)
- return *(*string)(unsafe.Pointer(&b))
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/manglers.go b/vendor/codeberg.org/gruf/go-mangler/manglers.go
deleted file mode 100644
index 79bfe3c00..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/manglers.go
+++ /dev/null
@@ -1,113 +0,0 @@
-package mangler
-
-import (
- "unsafe"
- _ "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, ptr unsafe.Pointer) []byte {
- return append(buf, *(*string)(ptr)...)
-}
-
-func mangle_string_slice(buf []byte, ptr unsafe.Pointer) []byte {
- s := *(*[]string)(ptr)
- 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, ptr unsafe.Pointer) []byte {
- if *(*bool)(ptr) {
- return append(buf, '1')
- }
- return append(buf, '0')
-}
-
-func mangle_bool_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, b := range *(*[]bool)(ptr) {
- if b {
- buf = append(buf, '1')
- } else {
- buf = append(buf, '0')
- }
- }
- return buf
-}
-
-func mangle_8bit(buf []byte, ptr unsafe.Pointer) []byte {
- return append(buf, *(*uint8)(ptr))
-}
-
-func mangle_8bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
- return append(buf, *(*[]uint8)(ptr)...)
-}
-
-func mangle_16bit(buf []byte, ptr unsafe.Pointer) []byte {
- return append_uint16(buf, *(*uint16)(ptr))
-}
-
-func mangle_16bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, u := range *(*[]uint16)(ptr) {
- buf = append_uint16(buf, u)
- }
- return buf
-}
-
-func mangle_32bit(buf []byte, ptr unsafe.Pointer) []byte {
- return append_uint32(buf, *(*uint32)(ptr))
-}
-
-func mangle_32bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, u := range *(*[]uint32)(ptr) {
- buf = append_uint32(buf, u)
- }
- return buf
-}
-
-func mangle_64bit(buf []byte, ptr unsafe.Pointer) []byte {
- return append_uint64(buf, *(*uint64)(ptr))
-}
-
-func mangle_64bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, u := range *(*[]uint64)(ptr) {
- buf = append_uint64(buf, u)
- }
- return buf
-}
-
-func mangle_int(buf []byte, ptr unsafe.Pointer) []byte {
- return append_uint64(buf, uint64(*(*uint)(ptr)))
-}
-
-func mangle_int_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, u := range *(*[]uint)(ptr) {
- buf = append_uint64(buf, uint64(u))
- }
- return buf
-}
-
-func mangle_128bit(buf []byte, ptr unsafe.Pointer) []byte {
- u2 := *(*[2]uint64)(ptr)
- buf = append_uint64(buf, u2[0])
- buf = append_uint64(buf, u2[1])
- return buf
-}
-
-func mangle_128bit_slice(buf []byte, ptr unsafe.Pointer) []byte {
- for _, u2 := range *(*[][2]uint64)(ptr) {
- buf = append_uint64(buf, u2[0])
- buf = append_uint64(buf, u2[1])
- }
- return buf
-}