diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-kv/v2')
19 files changed, 0 insertions, 2768 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/LICENSE b/vendor/codeberg.org/gruf/go-kv/v2/LICENSE deleted file mode 100644 index d6f08d0ab..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) 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-kv/v2/README.md b/vendor/codeberg.org/gruf/go-kv/v2/README.md deleted file mode 100644 index 2815f1c83..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# go-kv - -This library provides a key-value field structure `kv.Field{}` that plays well with the `"fmt"` package. It gives an easy means of appending key-value fields to log entries, in a more performant manner that also happens to look nice! (it's not far removed from using a `map[string]interface{}`). - -The formatting for these key-value fields is handled by the `"fmt"` package by default. If you set the `kvformat` build tag then it will use a custom formatting library found under `format/`. diff --git a/vendor/codeberg.org/gruf/go-kv/v2/field.go b/vendor/codeberg.org/gruf/go-kv/v2/field.go deleted file mode 100644 index 58399e4a9..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/field.go +++ /dev/null @@ -1,103 +0,0 @@ -package kv - -import ( - "codeberg.org/gruf/go-byteutil" -) - -// bufsize is the default buffer size per field to alloc -// when calling .AppendFormat() from within .String(). -const bufsize = 64 - -// Fields is a typedef for a []Field slice to provide -// slightly more performant string formatting for multiples. -type Fields []Field - -// Get will return the field with given 'key'. -func (f Fields) Get(key string) (*Field, bool) { - for i := 0; i < len(f); i++ { - if f[i].K == key { - return &f[i], true - } - } - return nil, false -} - -// Set will set an existing field with 'key' to 'value', or append new. -func (f *Fields) Set(key string, value interface{}) { - for i := 0; i < len(*f); i++ { - // Update existing value - if (*f)[i].K == key { - (*f)[i].V = value - return - } - } - - // Append new field - *f = append(*f, Field{ - K: key, - V: value, - }) -} - -// AppendFormat appends a string representation of receiving Field(s) to 'b'. -func (f Fields) AppendFormat(buf *byteutil.Buffer, vbose bool) { - for i := 0; i < len(f); i++ { - f[i].AppendFormat(buf, vbose) - buf.WriteByte(' ') - } - if len(f) > 0 { - buf.Truncate(1) - } -} - -// String returns a string representation of receiving Field(s). -func (f Fields) String() string { - b := make([]byte, 0, bufsize*len(f)) - buf := byteutil.Buffer{B: b} - f.AppendFormat(&buf, false) - return buf.String() -} - -// GoString performs .String() but with type prefix. -func (f Fields) GoString() string { - b := make([]byte, 0, bufsize*len(f)) - buf := byteutil.Buffer{B: b} - f.AppendFormat(&buf, true) - return "kv.Fields{" + buf.String() + "}" -} - -// Field represents an individual key-value field. -type Field struct { - K string // Field key - V interface{} // Field value -} - -// Key returns the formatted key string of this Field. -func (f Field) Key() string { - buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} - AppendQuoteString(&buf, f.K) - return buf.String() -} - -// String will return a string representation of this Field -// of the form `key=value` where `value` is formatted using -// fmt package's `%+v` directive. If the .X = true (verbose), -// then it uses '%#v'. Both key and value are escaped and -// quoted if necessary to fit on single line. -// -// If the `kvformat` build tag is provided, the formatting -// will be performed by the `kv/format` package. -func (f Field) String() string { - b := make([]byte, 0, bufsize) - buf := byteutil.Buffer{B: b} - f.AppendFormat(&buf, false) - return buf.String() -} - -// GoString performs .String() but with verbose always enabled. -func (f Field) GoString() string { - b := make([]byte, 0, bufsize) - buf := byteutil.Buffer{B: b} - f.AppendFormat(&buf, true) - return buf.String() -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go b/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go deleted file mode 100644 index 5d6e77f4b..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go +++ /dev/null @@ -1,63 +0,0 @@ -//go:build !kvformat -// +build !kvformat - -package kv - -import ( - "fmt" - "sync" - - "codeberg.org/gruf/go-byteutil" -) - -// bufPool is a memory pool of byte buffers. -var bufPool = sync.Pool{ - New: func() interface{} { - return &byteutil.Buffer{B: make([]byte, 0, 512)} - }, -} - -// AppendFormat will append formatted format of Field to 'buf'. See .String() for details. -func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { - var fmtstr string - if vbose /* verbose */ { - fmtstr = `%#v` - } else /* regular */ { - fmtstr = `%+v` - } - AppendQuoteString(buf, f.K) - buf.WriteByte('=') - appendValuef(buf, fmtstr, f.V) -} - -// Value returns the formatted value string of this Field. -func (f Field) Value(vbose bool) string { - var fmtstr string - if vbose /* verbose */ { - fmtstr = `%#v` - } else /* regular */ { - fmtstr = `%+v` - } - buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} - appendValuef(&buf, fmtstr, f.V) - return buf.String() -} - -// appendValuef appends a quoted value string (formatted by fmt.Appendf) to 'buf'. -func appendValuef(buf *byteutil.Buffer, format string, args ...interface{}) { - // Write format string to a byte buffer - fmtbuf := bufPool.Get().(*byteutil.Buffer) - fmtbuf.B = fmt.Appendf(fmtbuf.B, format, args...) - - // Append quoted value to dst buffer - AppendQuoteValue(buf, fmtbuf.String()) - - // Drop overly large capacity buffers - if fmtbuf.Cap() > int(^uint16(0)) { - return - } - - // Replace buffer in pool - fmtbuf.Reset() - bufPool.Put(fmtbuf) -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/field_format.go b/vendor/codeberg.org/gruf/go-kv/v2/field_format.go deleted file mode 100644 index f5db0f123..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/field_format.go +++ /dev/null @@ -1,44 +0,0 @@ -//go:build kvformat -// +build kvformat - -package kv - -import ( - "codeberg.org/gruf/go-byteutil" - "codeberg.org/gruf/go-kv/v2/format" -) - -var argsDefault = format.DefaultArgs() - -var argsVerbose = func() format.Args { - args := format.DefaultArgs() - args.SetWithType() - args.SetNoMethod() - return args -}() - -// AppendFormat will append formatted format of Field to 'buf'. See .String() for details. -func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { - var args format.Args - if vbose { - args = argsVerbose - } else { - args = argsDefault - } - AppendQuoteString(buf, f.K) - buf.WriteByte('=') - buf.B = format.Global.Append(buf.B, f.V, args) -} - -// Value returns the formatted value string of this Field. -func (f Field) Value(vbose bool) string { - var args format.Args - if vbose { - args = argsVerbose - } else { - args = argsDefault - } - buf := make([]byte, 0, bufsize/2) - buf = format.Global.Append(buf, f.V, args) - return byteutil.B2S(buf) -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/README.md b/vendor/codeberg.org/gruf/go-kv/v2/format/README.md deleted file mode 100644 index 5d11cc6b9..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# format - -a low-level string formatting library that takes arbitrary input types as interfaces, and arguments as a struct. this does not contain any printf-like argument parsing, only log-friendly serialization of arbitrary input arguments. (noting that our output is noticably more log-friendly for struct / map types than stdlib "fmt"). - -benchmarks: -```shell -goos: linux -goarch: amd64 -pkg: codeberg.org/gruf/go-kv/v2/format -cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics - -# go-kv/v2/format (i.e. latest) -BenchmarkFormatV2Append -BenchmarkFormatV2Append-16 590422 1977 ns/op 488 B/op 23 allocs/op -BenchmarkFormatV2AppendVerbose -BenchmarkFormatV2AppendVerbose-16 375628 2981 ns/op 1704 B/op 45 allocs/op - -# go-kv/format (i.e. v1) -BenchmarkFormatAppend -BenchmarkFormatAppend-16 208357 5883 ns/op 2624 B/op 169 allocs/op -BenchmarkFormatAppendVerbose -BenchmarkFormatAppendVerbose-16 35916 33563 ns/op 3734 B/op 208 allocs/op - -# fmt (i.e. stdlib) -BenchmarkFmtAppend -BenchmarkFmtAppend-16 147722 8418 ns/op 4747 B/op 191 allocs/op -BenchmarkFmtAppendVerbose -BenchmarkFmtAppendVerbose-16 167112 7238 ns/op 4401 B/op 178 allocs/op - -PASS -ok codeberg.org/gruf/go-kv/v2/format -``` diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/abi.go b/vendor/codeberg.org/gruf/go-kv/v2/format/abi.go deleted file mode 100644 index 16b9c8cb9..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/abi.go +++ /dev/null @@ -1,25 +0,0 @@ -//go:build go1.24 && !go1.26 - -package format - -import ( - "reflect" - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// 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) -} - -// typeof is short-hand for reflect.TypeFor[T](). -func typeof[T any]() reflect.Type { - return reflect.TypeFor[T]() -} - -const ( - // custom xunsafe.Reflect_flag to indicate key types. - flagKeyType xunsafe.Reflect_flag = 1 << 10 -) diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/args.go b/vendor/codeberg.org/gruf/go-kv/v2/format/args.go deleted file mode 100644 index c85a04d26..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/args.go +++ /dev/null @@ -1,247 +0,0 @@ -package format - -const ( - // TypeMask when set in argument flags - // indicates that type information of - // the passed value, and all nested types, - // should be included in formatted output. - TypeMask = uint64(1) << 0 - - // LogfmtMask when set in argument flags - // indicates that strings should be escaped - // and quoted only where necessary. i.e. if - // it contains any unsafe ASCII chars or double - // quotes it will be quoted and escaped, if it - // contains any spaces it will be quoted, and - // all else will be printed as-is. This proves - // particularly well readable in key-value types. - LogfmtMask = uint64(1) << 1 - - // NumberMask when set in argument flags - // indicates that where possible value - // types should be formatted as numbers, - // i.e. byte or rune types. - NumberMask = uint64(1) << 2 - - // TextMask when set in argument flags - // indicates that where possible value - // types should be formatted as text, - // i.e. []byte or []rune types. - TextMask = uint64(1) << 3 - - // QuotedTextMask when set in argument flags - // indicates that text should always be quoted. - QuotedTextMask = uint64(1) << 4 - - // QuotedAsciiMask when set in argument flags - // indicates that text should always be quoted, - // and escaped as ASCII characters where needed. - QuotedAsciiMask = uint64(1) << 5 - - // NoMethodMask when set in argument flags - // indicates that where a type supports a - // known method, (e.g. Error() or String()), - // this should not be used for formatting - // instead treating as a method-less type. - // e.g. printing the entire struct value of - // a &url.URL{} without calling String(). - NoMethodMask = uint64(1) << 6 -) - -var ( - // default set of Args. - defaultArgs = Args{ - Flags: LogfmtMask | TextMask, - Int: IntArgs{Base: 10}, - Uint: IntArgs{Base: 10}, - Float: FloatArgs{Fmt: 'g', Prec: -1}, - Complex: ComplexArgs{ - Real: FloatArgs{Fmt: 'g', Prec: -1}, - Imag: FloatArgs{Fmt: 'g', Prec: -1}, - }, - } - - // zeroArgs used for checking - // zero value Arg{} fields. - zeroArgs Args -) - -// DefaultArgs returns default -// set of formatter arguments. -func DefaultArgs() Args { - return defaultArgs -} - -// Args contains arguments -// for a call to a FormatFunc. -type Args struct { - - // Boolean argument - // flags as bit-field. - Flags uint64 - - // Integer - // arguments. - // i.e. for: - // - int - // - int8 - // - int16 - // - int32 (treated as rune char, number with NumberMask) - // - int64 - Int IntArgs - - // Unsigned - // integer - // arguments. - // i.e. for: - // - uint - // - uint8 (treated as byte char, number with NumberMask) - // - uint16 - // - uint32 - // - uint64 - Uint IntArgs - - // Float - // arguments. - // i.e. for: - // - float32 - // - float64 - Float FloatArgs - - // Complex - // arguments. - // i.e. for: - // - complex64 - // - complex128 - Complex ComplexArgs -} - -// IntArgs provides a set of -// arguments for customizing -// integer number serialization. -type IntArgs struct { - Base int - Pad int -} - -// FloatArgs provides a set of -// arguments for customizing -// float number serialization. -type FloatArgs struct { - Fmt byte - Prec int -} - -// ComplexArgs provides a set of -// arguments for customizing complex -// number serialization, as real and -// imaginary float number parts. -type ComplexArgs struct { - Real FloatArgs - Imag FloatArgs -} - -// WithType returns if TypeMask is set. -func (a *Args) WithType() bool { - return a.Flags&TypeMask != 0 -} - -// Logfmt returns if LogfmtMask is set. -func (a *Args) Logfmt() bool { - return a.Flags&LogfmtMask != 0 -} - -// AsNumber returns if NumberMask is set. -func (a *Args) AsNumber() bool { - return a.Flags&NumberMask != 0 -} - -// AsText returns if TextMask is set. -func (a *Args) AsText() bool { - return a.Flags&TextMask != 0 -} - -// AsQuotedText returns if QuotedTextMask is set. -func (a *Args) AsQuotedText() bool { - return a.Flags&QuotedTextMask != 0 -} - -// AsQuotedASCII returns if QuotedAsciiMask is set. -func (a *Args) AsQuotedASCII() bool { - return a.Flags&QuotedAsciiMask != 0 -} - -// NoMethod returns if NoMethodMask is set. -func (a *Args) NoMethod() bool { - return a.Flags&NoMethodMask != 0 -} - -// SetWithType sets the TypeMask bit. -func (a *Args) SetWithType() { - a.Flags = a.Flags | TypeMask -} - -// SetLogfmt sets the LogfmtMask bit. -func (a *Args) SetLogfmt() { - a.Flags = a.Flags | LogfmtMask -} - -// SetAsNumber sets the NumberMask bit. -func (a *Args) SetAsNumber() { - a.Flags = a.Flags | NumberMask -} - -// SetAsText sets the TextMask bit. -func (a *Args) SetAsText() { - a.Flags = a.Flags | TextMask -} - -// SetAsQuotedText sets the QuotedTextMask bit. -func (a *Args) SetAsQuotedText() { - a.Flags = a.Flags | QuotedTextMask -} - -// SetAsQuotedASCII sets the QuotedAsciiMask bit. -func (a *Args) SetAsQuotedASCII() { - a.Flags = a.Flags | QuotedAsciiMask -} - -// SetNoMethod sets the NoMethodMask bit. -func (a *Args) SetNoMethod() { - a.Flags = a.Flags | NoMethodMask -} - -// UnsetWithType unsets the TypeMask bit. -func (a *Args) UnsetWithType() { - a.Flags = a.Flags & ^TypeMask -} - -// UnsetLogfmt unsets the LogfmtMask bit. -func (a *Args) UnsetLogfmt() { - a.Flags = a.Flags & ^LogfmtMask -} - -// UnsetAsNumber unsets the NumberMask bit. -func (a *Args) UnsetAsNumber() { - a.Flags = a.Flags & ^NumberMask -} - -// UnsetAsText unsets the TextMask bit. -func (a *Args) UnsetAsText() { - a.Flags = a.Flags & ^TextMask -} - -// UnsetAsQuotedText unsets the QuotedTextMask bit. -func (a *Args) UnsetAsQuotedText() { - a.Flags = a.Flags & ^QuotedTextMask -} - -// UnsetAsQuotedASCII unsets the QuotedAsciiMask bit. -func (a *Args) UnsetAsQuotedASCII() { - a.Flags = a.Flags & ^QuotedAsciiMask -} - -// UnsetNoMethod unsets the NoMethodMask bit. -func (a *Args) UnsetNoMethod() { - a.Flags = a.Flags & ^NoMethodMask -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/array.go b/vendor/codeberg.org/gruf/go-kv/v2/format/array.go deleted file mode 100644 index 1ece16378..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/array.go +++ /dev/null @@ -1,235 +0,0 @@ -package format - -import ( - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// iterArrayType returns a FormatFunc capable of iterating -// and formatting the given array type currently in TypeIter{}. -// note this will fetch a sub-FormatFunc for the array element -// type, and also handle special cases of [n]byte, [n]rune arrays. -func (fmt *Formatter) iterArrayType(t xunsafe.TypeIter) FormatFunc { - - // Array element type. - elem := t.Type.Elem() - - // Get nested elem TypeIter with appropriate flags. - flags := xunsafe.ReflectArrayElemFlags(t.Flag, elem) - et := t.Child(elem, flags) - - // Get elem format func. - fn := fmt.loadOrGet(et) - if fn == nil { - panic("unreachable") - } - - // Handle possible sizes. - switch t.Type.Len() { - case 0: - return emptyArrayType(t) - case 1: - return iterSingleArrayType(t, fn) - default: - return iterMultiArrayType(t, fn) - } -} - -func emptyArrayType(t xunsafe.TypeIter) FormatFunc { - if !needs_typestr(t) { - // Simply append empty. - return func(s *State) { - s.B = append(s.B, "[]"...) - } - } - - // Array type string with refs. - typestr := typestr_with_refs(t) - - // Append empty with type. - return func(s *State) { - if s.A.WithType() { - s.B = append(s.B, typestr...) - s.B = append(s.B, "{}"...) - } else { - s.B = append(s.B, "[]"...) - } - } -} - -func iterSingleArrayType(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - if !needs_typestr(t) { - return func(s *State) { - // Wrap 'fn' in braces. - s.B = append(s.B, '[') - fn(s) - s.B = append(s.B, ']') - } - } - - // Array type string with refs. - typestr := typestr_with_refs(t) - - // Wrap in type+braces. - return func(s *State) { - - // Open / close braces. - var open, close uint8 - open, close = '[', ']' - - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestr...) - open, close = '{', '}' - } - - // Wrap 'fn' in braces. - s.B = append(s.B, open) - fn(s) - s.B = append(s.B, close) - } -} - -func iterMultiArrayType(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - // Array element in-memory size. - esz := t.Type.Elem().Size() - - // Number of elements. - n := t.Type.Len() - - if !needs_typestr(t) { - // Wrap elems in braces. - return func(s *State) { - ptr := s.P - - // Prepend array brace. - s.B = append(s.B, '[') - - for i := 0; i < n; i++ { - // Format at array index. - offset := esz * uintptr(i) - s.P = add(ptr, offset) - fn(s) - - // Append separator. - s.B = append(s.B, ',') - } - - // Drop final space. - s.B = s.B[:len(s.B)-1] - - // Prepend array brace. - s.B = append(s.B, ']') - } - } - - // Array type string with refs. - typestr := typestr_with_refs(t) - - // Wrap in type+braces. - return func(s *State) { - ptr := s.P - - // Open / close braces. - var open, close uint8 - open, close = '[', ']' - - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestr...) - open, close = '{', '}' - } - - // Prepend array brace. - s.B = append(s.B, open) - - for i := 0; i < n; i++ { - // Format at array index. - offset := esz * uintptr(i) - s.P = add(ptr, offset) - fn(s) - - // Append separator. - s.B = append(s.B, ',') - } - - // Drop final comma. - s.B = s.B[:len(s.B)-1] - - // Prepend array brace. - s.B = append(s.B, close) - } -} - -func wrapByteArray(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - n := t.Type.Len() - if !needs_typestr(t) { - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - var v string - p := (*xunsafe.Unsafeheader_String)(unsafe.Pointer(&v)) - p.Len = n - p.Data = s.P - appendString(s, v) - } else { - fn(s) - } - } - } - typestr := typestr_with_ptrs(t) - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - var v string - p := (*xunsafe.Unsafeheader_String)(unsafe.Pointer(&v)) - p.Len = n - p.Data = s.P - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")("...) - appendString(s, v) - s.B = append(s.B, ")"...) - } else { - appendString(s, v) - } - } else { - fn(s) - } - } -} - -func wrapRuneArray(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - n := t.Type.Len() - if !needs_typestr(t) { - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - var v []rune - p := (*xunsafe.Unsafeheader_Slice)(unsafe.Pointer(&v)) - p.Cap = n - p.Len = n - p.Data = s.P - appendString(s, string(v)) - } else { - fn(s) - } - } - } - typestr := typestr_with_ptrs(t) - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - var v []rune - p := (*xunsafe.Unsafeheader_Slice)(unsafe.Pointer(&v)) - p.Cap = n - p.Len = n - p.Data = s.P - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")("...) - appendString(s, string(v)) - s.B = append(s.B, ")"...) - } else { - appendString(s, string(v)) - } - } else { - fn(s) - } - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/cache.go b/vendor/codeberg.org/gruf/go-kv/v2/format/cache.go deleted file mode 100644 index 8c9bfa210..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/cache.go +++ /dev/null @@ -1,60 +0,0 @@ -package format - -import ( - "sync/atomic" - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// cache is a concurrency-safe map[xunsafe.TypeInfo]FormatFunc -// cache, designed for heavy reads but with unfortunately expensive -// writes. it is designed such that after some initial load period -// in which functions are cached by types, all future ops are reads. -type cache struct{ p unsafe.Pointer } - -// Get will check cache for format func under key. -func (c *cache) Get(t xunsafe.TypeInfo) FormatFunc { - if p := c.load(); p != nil { - return (*p)[t] - } - return nil -} - -// Put will place given format func in cache under key, if not already exists. -func (c *cache) Put(t xunsafe.TypeInfo, fn FormatFunc) { - for { - p := c.load() - - var cache map[xunsafe.TypeInfo]FormatFunc - - if p != nil { - if _, ok := (*p)[t]; ok { - return - } - - cache = make(map[xunsafe.TypeInfo]FormatFunc, len(*p)+1) - for key, value := range *p { - cache[key] = value - } - } else { - cache = make(map[xunsafe.TypeInfo]FormatFunc, 1) - } - - cache[t] = fn - - if c.cas(p, &cache) { - return - } - } -} - -// load is a typed wrapper around atomic.LoadPointer(). -func (c *cache) load() *map[xunsafe.TypeInfo]FormatFunc { - return (*map[xunsafe.TypeInfo]FormatFunc)(atomic.LoadPointer(&c.p)) -} - -// cas is a typed wrapper around atomic.CompareAndSwapPointer(). -func (c *cache) cas(old, new *map[xunsafe.TypeInfo]FormatFunc) bool { - return atomic.CompareAndSwapPointer(&c.p, unsafe.Pointer(old), unsafe.Pointer(new)) -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/format.go b/vendor/codeberg.org/gruf/go-kv/v2/format/format.go deleted file mode 100644 index 0629c6f96..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/format.go +++ /dev/null @@ -1,687 +0,0 @@ -package format - -import ( - "reflect" - "runtime/debug" - "strconv" - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// Global formatter instance. -var Global Formatter - -// FormatFunc defines a function capable of formatting -// the value contained in State{}.P, based on args in -// State{}.A, storing the result in buffer State{}.B. -type FormatFunc func(*State) - -// State contains all necessary -// arguments, buffer and value -// data pointer required for a -// FormatFunc operation, in a -// reusable structure if wanted. -type State struct { - - // A contains args - // passed to this - // FormatFunc call. - A Args - - // B is the buffer - // that values will - // be formatted into. - B []byte - - // P contains a ptr - // to the value type - // being formatted. - P unsafe.Pointer - - // stores pointers to the - // recent interface values - // we have visited. to prevent - // possible recursion of - // runtime defined data. - ifaces ptr_ring -} - -// ptr_ring size. -const ringsz = 16 - -// ptr_ring is a ring buffer of pointers, -// purposely stored as uintptrs as all we -// need them for is integer comparisons and -// we don't want to hold-up the GC. -type ptr_ring struct { - p [ringsz]uintptr - n uint8 -} - -func (p *ptr_ring) contains(ptr unsafe.Pointer) bool { - for _, eptr := range p.p { - if uintptr(ptr) == eptr { - return true - } - } - return false -} - -func (p *ptr_ring) set(ptr unsafe.Pointer) { - p.p[p.n%ringsz] = uintptr(ptr) - p.n++ -} - -func (p *ptr_ring) clear() { - p.p = [ringsz]uintptr{} - p.n = 0 -} - -// Formatter provides access to value formatting -// provided by this library. It encompasses a set -// of configurable default arguments for when none -// are set, and an internal concurrency-safe cache -// of FormatFuncs to passed value type. -type Formatter struct { - - // Defaults defines the default - // set of arguments to use when - // none are supplied to calls to - // Append() and AppendState(). - Defaults Args - - // internal - // format func - // cache map. - fns cache -} - -// Append calls AppendState() with a newly allocated State{}, returning byte buffer. -func (fmt *Formatter) Append(buf []byte, value any, args Args) []byte { - s := new(State) - s.A = args - s.B = buf - fmt.AppendState(s, value) - return s.B -} - -// AppendState will format the given value into the given -// State{}'s byte buffer, using currently-set arguments. -func (fmt *Formatter) AppendState(s *State, value any) { - switch { - case s.A != zeroArgs: - break - case fmt.Defaults != zeroArgs: - // use fmt defaults. - s.A = fmt.Defaults - default: - // global defaults. - s.A = defaultArgs - } - t := xunsafe.TypeIterFrom(value) - s.P = xunsafe.UnpackEface(value) - s.ifaces.clear() - s.ifaces.set(s.P) - fmt.loadOrStore(t)(s) -} - -func (fmt *Formatter) loadOrGet(t xunsafe.TypeIter) FormatFunc { - // Look for existing stored - // func under this type key. - fn := fmt.fns.Get(t.TypeInfo) - - if fn == nil { - // Load format func - // for typecontext. - fn = fmt.get(t) - if fn == nil { - panic("unreachable") - } - } - - return fn -} - -func (fmt *Formatter) loadOrStore(t xunsafe.TypeIter) FormatFunc { - // Get cache key. - key := t.TypeInfo - - // Look for existing stored - // func under this type key. - fn := fmt.fns.Get(key) - - if fn == nil { - // Load format func - // for typecontext. - fn = fmt.get(t) - if fn == nil { - panic("unreachable") - } - - // Store under type. - fmt.fns.Put(key, fn) - } - - return fn -} - -var ( - // reflectTypeType is the reflected type of the reflect type, - // used in fmt.get() to prevent iter of internal ABI structs. - reflectTypeType = reflect.TypeOf(reflect.TypeOf(0)) - - // stringable int types. - byteType = typeof[byte]() - runeType = typeof[rune]() - - // stringable slice types. - bytesType = typeof[[]byte]() - runesType = typeof[[]rune]() -) - -func (fmt *Formatter) get(t xunsafe.TypeIter) (fn FormatFunc) { - if t.Type == nil { - // catch nil type. - return appendNil - } - - defer func() { - if r := recover(); r != nil { - debug.PrintStack() - panic(r) // keep panicking - } else if fn == nil { - panic("nil func") - } - - // Don't allow method functions for map keys, - // to prevent situation of the method receiver - // attempting to modify stored map key itself. - if t.Flag&flagKeyType != 0 { - return - } - - // Check if type supports known method receiver. - if methodFn := getMethodType(t); methodFn != nil { - - // Keep ptr to existing - // non-method format fn. - noMethodFn := fn - - // Wrap 'fn' to switch - // between method / none. - fn = func(s *State) { - if s.A.NoMethod() { - noMethodFn(s) - } else { - methodFn(s) - } - } - } - }() - - if t.Type == reflectTypeType { - // DO NOT iterate down internal ABI - // types, some are in non-GC memory. - return getPointerType(t) - } - - if !visit(t) { - // On type recursion simply - // format as raw pointer. - return getPointerType(t) - } - - // Get func for type kind. - switch t.Type.Kind() { - case reflect.Interface: - return fmt.getInterfaceType(t) - case reflect.String: - return getStringType(t) - case reflect.Bool: - return getBoolType(t) - case reflect.Int, - reflect.Int8, - reflect.Int16, - reflect.Int32, - reflect.Int64: - return getIntType(t) - case reflect.Uint, - reflect.Uint8, - reflect.Uint16, - reflect.Uint32, - reflect.Uint64: - return getUintType(t) - case reflect.Float32, - reflect.Float64: - return getFloatType(t) - case reflect.Complex64, - reflect.Complex128: - return getComplexType(t) - case reflect.Pointer: - return fmt.derefPointerType(t) - case reflect.Array: - elem := t.Type.Elem() - switch fn := fmt.iterArrayType(t); { - case elem.AssignableTo(byteType): - return wrapByteArray(t, fn) - case elem.AssignableTo(runeType): - return wrapRuneArray(t, fn) - default: - return fn - } - case reflect.Slice: - switch fn := fmt.iterSliceType(t); { - case t.Type.AssignableTo(bytesType): - return wrapByteSlice(t, fn) - case t.Type.AssignableTo(runesType): - return wrapRuneSlice(t, fn) - default: - return fn - } - case reflect.Struct: - return fmt.iterStructType(t) - case reflect.Map: - return fmt.iterMapType(t) - default: - return getPointerType(t) - } -} - -func (fmt *Formatter) getInterfaceType(t xunsafe.TypeIter) FormatFunc { - if t.Type.NumMethod() == 0 { - return func(s *State) { - // Unpack empty interface. - eface := *(*any)(s.P) - s.P = xunsafe.UnpackEface(eface) - - // Get reflected type information. - rtype := reflect.TypeOf(eface) - if rtype == nil { - appendNil(s) - return - } - - // Check for ptr recursion. - if s.ifaces.contains(s.P) { - getPointerType(t)(s) - return - } - - // Store value ptr. - s.ifaces.set(s.P) - - // Wrap before load. - var t xunsafe.TypeIter - t.Flag = xunsafe.ReflectIfaceElemFlags(rtype) - t.Type = rtype - - // Load + pass to func. - fmt.loadOrStore(t)(s) - } - } else { - return func(s *State) { - // Unpack interface-with-method ptr. - iface := *(*interface{ M() })(s.P) - s.P = xunsafe.UnpackEface(iface) - - // Get reflected type information. - rtype := reflect.TypeOf(iface) - if rtype == nil { - appendNil(s) - return - } - - // Check for ptr recursion. - if s.ifaces.contains(s.P) { - getPointerType(t)(s) - return - } - - // Store value ptr. - s.ifaces.set(s.P) - - // Wrap before load. - var t xunsafe.TypeIter - t.Flag = xunsafe.ReflectIfaceElemFlags(rtype) - t.Type = rtype - - // Load + pass to func. - fmt.loadOrStore(t)(s) - } - } -} - -func getStringType(t xunsafe.TypeIter) FormatFunc { - return with_typestr_ptrs(t, func(s *State) { - appendString(s, *(*string)(s.P)) - }) -} - -func getBoolType(t xunsafe.TypeIter) FormatFunc { - return with_typestr_ptrs(t, func(s *State) { - s.B = strconv.AppendBool(s.B, *(*bool)(s.P)) - }) -} - -func getIntType(t xunsafe.TypeIter) FormatFunc { - switch t.Type.Bits() { - case 8: - return with_typestr_ptrs(t, func(s *State) { - appendInt(s, int64(*(*int8)(s.P))) - }) - case 16: - return with_typestr_ptrs(t, func(s *State) { - appendInt(s, int64(*(*int16)(s.P))) - }) - case 32: - return with_typestr_ptrs(t, func(s *State) { - switch { - case s.A.AsNumber(): - // fallthrough - case s.A.AsQuotedASCII(): - s.B = strconv.AppendQuoteRuneToASCII(s.B, *(*rune)(s.P)) - return - case s.A.AsText() || s.A.AsQuotedText(): - s.B = strconv.AppendQuoteRune(s.B, *(*rune)(s.P)) - return - } - appendInt(s, int64(*(*int32)(s.P))) - }) - case 64: - return with_typestr_ptrs(t, func(s *State) { - appendInt(s, int64(*(*int64)(s.P))) - }) - default: - panic("unreachable") - } -} - -func getUintType(t xunsafe.TypeIter) FormatFunc { - switch t.Type.Bits() { - case 8: - return with_typestr_ptrs(t, func(s *State) { - switch { - case s.A.AsNumber(): - // fallthrough - case s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII(): - s.B = AppendQuoteByte(s.B, *(*byte)(s.P)) - return - } - appendUint(s, uint64(*(*uint8)(s.P))) - }) - case 16: - return with_typestr_ptrs(t, func(s *State) { - appendUint(s, uint64(*(*uint16)(s.P))) - }) - case 32: - return with_typestr_ptrs(t, func(s *State) { - appendUint(s, uint64(*(*uint32)(s.P))) - }) - case 64: - return with_typestr_ptrs(t, func(s *State) { - appendUint(s, uint64(*(*uint64)(s.P))) - }) - default: - panic("unreachable") - } -} - -func getFloatType(t xunsafe.TypeIter) FormatFunc { - switch t.Type.Bits() { - case 32: - return with_typestr_ptrs(t, func(s *State) { - appendFloat(s, float64(*(*float32)(s.P)), 32) - }) - case 64: - return with_typestr_ptrs(t, func(s *State) { - appendFloat(s, float64(*(*float64)(s.P)), 64) - }) - default: - panic("unreachable") - } -} - -func getComplexType(t xunsafe.TypeIter) FormatFunc { - switch t.Type.Bits() { - case 64: - return with_typestr_ptrs(t, func(s *State) { - v := *(*complex64)(s.P) - r, i := real(v), imag(v) - appendComplex(s, float64(r), float64(i), 32) - }) - case 128: - return with_typestr_ptrs(t, func(s *State) { - v := *(*complex128)(s.P) - r, i := real(v), imag(v) - appendComplex(s, float64(r), float64(i), 64) - }) - default: - panic("unreachable") - } -} - -func getPointerType(t xunsafe.TypeIter) FormatFunc { - switch t.Indirect() { - case true: - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - appendPointer(s, s.P) - }) - case false: - return with_typestr_ptrs(t, func(s *State) { - appendPointer(s, s.P) - }) - default: - panic("unreachable") - } -} - -func with_typestr_ptrs(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - if fn == nil { - panic("nil func") - } - - // Check for type wrapping. - if !needs_typestr(t) { - return fn - } - - // Get type string with pointers. - typestr := typestr_with_ptrs(t) - - // Wrap format func to include - // type information when needed. - return func(s *State) { - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")("...) - fn(s) - s.B = append(s.B, ")"...) - } else { - fn(s) - } - } -} - -func appendString(s *State, v string) { - switch { - case s.A.WithType(): - if len(v) > SingleTermLine || !IsSafeASCII(v) { - // Requires quoting AND escaping - s.B = strconv.AppendQuote(s.B, v) - } else if ContainsDoubleQuote(v) { - // Contains double quotes, needs escaping - s.B = append(s.B, '"') - s.B = AppendEscape(s.B, v) - s.B = append(s.B, '"') - } else { - // All else, needs quotes - s.B = append(s.B, '"') - s.B = append(s.B, v...) - s.B = append(s.B, '"') - } - case s.A.Logfmt(): - if len(v) > SingleTermLine || !IsSafeASCII(v) { - // Requires quoting AND escaping - s.B = strconv.AppendQuote(s.B, v) - } else if ContainsDoubleQuote(v) { - // Contains double quotes, needs escaping - s.B = append(s.B, '"') - s.B = AppendEscape(s.B, v) - s.B = append(s.B, '"') - } else if len(v) == 0 || ContainsSpaceOrTab(v) { - // Contains space / empty, needs quotes - s.B = append(s.B, '"') - s.B = append(s.B, v...) - s.B = append(s.B, '"') - } else { - // All else write as-is - s.B = append(s.B, v...) - } - case s.A.AsQuotedText(): - s.B = strconv.AppendQuote(s.B, v) - case s.A.AsQuotedASCII(): - s.B = strconv.AppendQuoteToASCII(s.B, v) - default: - s.B = append(s.B, v...) - } -} - -func appendInt(s *State, v int64) { - args := s.A.Int - - // Set argument defaults. - if args == zeroArgs.Int { - args = defaultArgs.Int - } - - // Add any padding. - if args.Pad > 0 { - const zeros = `00000000000000000000` - if args.Pad > len(zeros) { - panic("cannot pad > " + zeros) - } - - if v == 0 { - s.B = append(s.B, zeros[:args.Pad]...) - return - } - - // Get absolute. - abs := abs64(v) - - // Get number of required chars. - chars := int(v / int64(args.Base)) - if v%int64(args.Base) != 0 { - chars++ - } - - if abs != v { - // If this is a negative value, - // prepend minus ourselves and - // set value as the absolute. - s.B = append(s.B, '-') - v = abs - } - - // Prepend required zeros. - n := args.Pad - chars - s.B = append(s.B, zeros[:n]...) - } - - // Append value as signed integer w/ args. - s.B = strconv.AppendInt(s.B, v, args.Base) -} - -func appendUint(s *State, v uint64) { - args := s.A.Int - - // Set argument defaults. - if args == zeroArgs.Int { - args = defaultArgs.Int - } - - // Add any padding. - if args.Pad > 0 { - const zeros = `00000000000000000000` - if args.Pad > len(zeros) { - panic("cannot pad > " + zeros) - } - - if v == 0 { - s.B = append(s.B, zeros[:args.Pad]...) - return - } - - // Get number of required chars. - chars := int(v / uint64(args.Base)) - if v%uint64(args.Base) != 0 { - chars++ - } - - // Prepend required zeros. - n := args.Pad - chars - s.B = append(s.B, zeros[:n]...) - } - - // Append value as unsigned integer w/ args. - s.B = strconv.AppendUint(s.B, v, args.Base) -} - -func appendFloat(s *State, v float64, bits int) { - args := s.A.Float - - // Set argument defaults. - if args == zeroArgs.Float { - args = defaultArgs.Float - } - - // Append value as float${bit} w/ args. - s.B = strconv.AppendFloat(s.B, float64(v), - args.Fmt, args.Prec, bits) -} - -func appendComplex(s *State, r, i float64, bits int) { - args := s.A.Complex - - // Set argument defaults. - if args == zeroArgs.Complex { - args = defaultArgs.Complex - } - - // Append real value as float${bit} w/ args. - s.B = strconv.AppendFloat(s.B, float64(r), - args.Real.Fmt, args.Real.Prec, bits) - s.B = append(s.B, '+') - - // Append imag value as float${bit} w/ args. - s.B = strconv.AppendFloat(s.B, float64(i), - args.Imag.Fmt, args.Imag.Prec, bits) - s.B = append(s.B, 'i') -} - -func appendPointer(s *State, v unsafe.Pointer) { - if v != nil { - s.B = append(s.B, "0x"...) - s.B = strconv.AppendUint(s.B, uint64(uintptr(v)), 16) - } else { - appendNil(s) - } -} - -func appendNilType(s *State, typestr string) { - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")(<nil>)"...) - } else { - s.B = append(s.B, "<nil>"...) - } -} - -func appendNil(s *State) { - s.B = append(s.B, "<nil>"...) -} - -func abs64(i int64) int64 { - u := uint64(i >> 63) - return (i ^ int64(u)) + int64(u&1) -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/formatting.go b/vendor/codeberg.org/gruf/go-kv/v2/format/formatting.go deleted file mode 100644 index e09edbefc..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/formatting.go +++ /dev/null @@ -1,161 +0,0 @@ -package format - -import ( - "strings" - "unicode" - "unicode/utf8" -) - -const ( - // SingleTermLine: beyond a certain length of string, all of the - // extra checks to handle quoting/not-quoting add a significant - // amount of extra processing time. Quoting in this manner only really - // effects readability on a single line, so a max string length that - // encompasses the maximum number of columns on *most* terminals was - // selected. This was chosen using the metric that 1080p is one of the - // most common display resolutions, and that a relatively small font size - // of 7 requires 223 columns. So 256 should be >= $COLUMNS (fullscreen) - // in 99% of usecases (these figures all pulled out of my ass). - SingleTermLine = 256 -) - -// IsSafeASCII checks whether string is printable (i.e. non-control char) ASCII text. -func IsSafeASCII(str string) bool { - for _, r := range str { - if (r < ' ' && r != '\t') || - r >= 0x7f { - return false - } - } - return true -} - -// ContainsSpaceOrTab checks if "s" contains space or tabs. EXPECTS ASCII. -func ContainsSpaceOrTab(s string) bool { - if i := strings.IndexByte(s, ' '); i >= 0 { - return true // note using indexbyte as it is ASM. - } else if i := strings.IndexByte(s, '\t'); i >= 0 { - return true - } - return false -} - -// ContainsDoubleQuote checks if "s" contains a double quote. EXPECTS ASCII. -func ContainsDoubleQuote(s string) bool { - return (strings.IndexByte(s, '"') >= 0) -} - -// AppendEscape will append 's' to 'buf' and escape any double quotes. EXPECTS ASCII. -func AppendEscape(buf []byte, str string) []byte { - for i := range str { - switch str[i] { - case '\\': - // Append delimited '\' - buf = append(buf, '\\', '\\') - - case '"': - // Append delimited '"' - buf = append(buf, '\\', '"') - default: - // Append char as-is - buf = append(buf, str[i]) - } - } - return buf -} - -const hex = "0123456789abcdef" - -// AppendEscapeByte ... -func AppendEscapeByte(buf []byte, c byte) []byte { - switch c { - case '\a': - return append(buf, `\a`...) - case '\b': - return append(buf, `\b`...) - case '\f': - return append(buf, `\f`...) - case '\n': - return append(buf, `\n`...) - case '\r': - return append(buf, `\r`...) - case '\t': - return append(buf, `\t`...) - case '\v': - return append(buf, `\v`...) - case '\\': - return append(buf, `\\`...) - default: - if c < ' ' { - return append(buf, '\\', 'x', hex[c>>4], hex[c&0xF]) - } - return append(buf, c) - } -} - -// AppendQuoteByte ... -func AppendQuoteByte(buf []byte, c byte) []byte { - if c == '\'' { - return append(buf, `'\''`...) - } - buf = append(buf, '\'') - buf = AppendEscapeByte(buf, c) - buf = append(buf, '\'') - return buf -} - -// AppendEscapeRune ... -func AppendEscapeRune(buf []byte, r rune) []byte { - if unicode.IsPrint(r) { - return utf8.AppendRune(buf, r) - } - switch r { - case '\a': - return append(buf, `\a`...) - case '\b': - return append(buf, `\b`...) - case '\f': - return append(buf, `\f`...) - case '\n': - return append(buf, `\n`...) - case '\r': - return append(buf, `\r`...) - case '\t': - return append(buf, `\t`...) - case '\v': - return append(buf, `\v`...) - case '\\': - return append(buf, `\\`...) - default: - switch { - case r < ' ' || r == 0x7f: - buf = append(buf, `\x`...) - buf = append(buf, hex[byte(r)>>4]) - buf = append(buf, hex[byte(r)&0xF]) - case !utf8.ValidRune(r): - r = 0xFFFD - fallthrough - case r < 0x10000: - buf = append(buf, `\u`...) - buf = append(buf, - hex[r>>uint(12)&0xF], - hex[r>>uint(8)&0xF], - hex[r>>uint(4)&0xF], - hex[r>>uint(0)&0xF], - ) - default: - buf = append(buf, `\U`...) - buf = append(buf, - hex[r>>uint(28)&0xF], - hex[r>>uint(24)&0xF], - hex[r>>uint(20)&0xF], - hex[r>>uint(16)&0xF], - hex[r>>uint(12)&0xF], - hex[r>>uint(8)&0xF], - hex[r>>uint(4)&0xF], - hex[r>>uint(0)&0xF], - ) - } - } - return buf -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/map.go b/vendor/codeberg.org/gruf/go-kv/v2/format/map.go deleted file mode 100644 index a3c4acd60..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/map.go +++ /dev/null @@ -1,134 +0,0 @@ -package format - -import ( - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// iterMapType returns a FormatFunc capable of iterating -// and formatting the given map type currently in TypeIter{}. -// note this will fetch sub-FormatFuncs for key / value types. -func (fmt *Formatter) iterMapType(t xunsafe.TypeIter) FormatFunc { - - // Key / value types. - key := t.Type.Key() - elem := t.Type.Elem() - - // Get nested k / v TypeIters with appropriate flags. - flagsKey := xunsafe.ReflectMapKeyFlags(key) | flagKeyType - flagsVal := xunsafe.ReflectMapElemFlags(elem) - kt := t.Child(key, flagsKey) - vt := t.Child(elem, flagsVal) - - // Get key format func. - kfn := fmt.loadOrGet(kt) - if kfn == nil { - panic("unreachable") - } - - // Get value format func. - vfn := fmt.loadOrGet(vt) - if vfn == nil { - panic("unreachable") - } - - // Final map type. - rtype := t.Type - flags := t.Flag - - // Map type string with ptrs / refs. - typestrPtrs := typestr_with_ptrs(t) - typestrRefs := typestr_with_refs(t) - - if !needs_typestr(t) { - return func(s *State) { - if s.P == nil || *(*unsafe.Pointer)(s.P) == nil { - // Append nil. - appendNil(s) - return - } - - // Build reflect value, and then a map iterator. - v := xunsafe.BuildReflectValue(rtype, s.P, flags) - i := xunsafe.GetMapIter(v) - - // Prepend object brace. - s.B = append(s.B, '{') - - // Before len. - l := len(s.B) - - for i.Next() { - // Pass to map key func. - s.P = xunsafe.Map_Key(i) - kfn(s) - - // Add key seperator. - s.B = append(s.B, '=') - - // Pass to map elem func. - s.P = xunsafe.Map_Elem(i) - vfn(s) - - // Add comma pair seperator. - s.B = append(s.B, ',', ' ') - } - - if len(s.B) != l { - // Drop final ", ". - s.B = s.B[:len(s.B)-2] - } - - // Append object brace. - s.B = append(s.B, '}') - } - } - - return func(s *State) { - if s.P == nil || *(*unsafe.Pointer)(s.P) == nil { - // Append nil value with type. - appendNilType(s, typestrPtrs) - return - } - - // Build reflect value, and then a map iter. - v := xunsafe.BuildReflectValue(rtype, s.P, flags) - i := xunsafe.GetMapIter(v) - - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestrRefs...) - } - - // Prepend object brace. - s.B = append(s.B, '{') - - // Before len. - l := len(s.B) - - for i.Next() { - // Pass to map key func. - s.P = xunsafe.Map_Key(i) - kfn(s) - - // Add key seperator. - s.B = append(s.B, '=') - - // Pass to map elem func. - s.P = xunsafe.Map_Elem(i) - vfn(s) - - // Add comma pair seperator. - s.B = append(s.B, ',', ' ') - } - - if len(s.B) != l { - // Drop final ", ". - s.B = s.B[:len(s.B)-2] - } - - // Append object brace. - s.B = append(s.B, '}') - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go deleted file mode 100644 index 4c0c1dbda..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go +++ /dev/null @@ -1,242 +0,0 @@ -package format - -import ( - "reflect" - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -type Stringer interface{ String() string } - -type Formattable interface{ Format(*State) } - -var ( - // stringer type for implement checks. - stringerType = typeof[Stringer]() - - // formattableType type for implement checks. - formattableType = typeof[Formattable]() - - // error type for implement checks. - errorType = typeof[error]() -) - -// getMethodType returns a *possible* FormatFunc to handle case -// of a type that implements any known interface{} types, else nil. -func getMethodType(t xunsafe.TypeIter) FormatFunc { - switch { - case t.Type.Implements(stringerType): - switch t.Type.Kind() { - case reflect.Interface: - return getInterfaceStringerType(t) - default: - return getConcreteStringerType(t) - } - case t.Type.Implements(formattableType): - switch t.Type.Kind() { - case reflect.Interface: - return getInterfaceFormattableType(t) - default: - return getConcreteFormattableType(t) - } - case t.Type.Implements(errorType): - switch t.Type.Kind() { - case reflect.Interface: - return getInterfaceErrorType(t) - default: - return getConcreteErrorType(t) - } - default: - return nil - } -} - -// getInterfaceStringerType returns a FormatFunc to handle case of an interface{} -// type that implements Stringer{}, i.e. Stringer{} itself and any superset of. -func getInterfaceStringerType(t xunsafe.TypeIter) FormatFunc { - switch t.Indirect() && !t.IfaceIndir() { - case true: - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*Stringer)(s.P) - appendString(s, v.String()) - }) - case false: - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*Stringer)(s.P) - appendString(s, v.String()) - }) - default: - panic("unreachable") - } -} - -// getConcreteStringerType returns a FormatFunc to handle case of concrete -// (i.e. non-interface{}) type that has a Stringer{} method receiver. -func getConcreteStringerType(t xunsafe.TypeIter) FormatFunc { - itab := xunsafe.GetIfaceITab[Stringer](t.Type) - switch { - case t.Indirect() && !t.IfaceIndir(): - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil { - appendNil(s) - return - } - v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.String()) - }) - case t.Type.Kind() == reflect.Pointer && t.Type.Implements(stringerType): - // if the interface implementation is received by - // value type, the pointer type will also support - // it but it requires an extra dereference check. - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil { - appendNil(s) - return - } - v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.String()) - }) - default: - return with_typestr_ptrs(t, func(s *State) { - v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.String()) - }) - } -} - -// getInterfaceFormattableType returns a FormatFunc to handle case of an interface{} -// type that implements Formattable{}, i.e. Formattable{} itself and any superset of. -func getInterfaceFormattableType(t xunsafe.TypeIter) FormatFunc { - switch t.Indirect() && !t.IfaceIndir() { - case true: - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*Formattable)(s.P) - v.Format(s) - }) - case false: - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*Formattable)(s.P) - v.Format(s) - }) - default: - panic("unreachable") - } -} - -// getConcreteFormattableType returns a FormatFunc to handle case of concrete -// (i.e. non-interface{}) type that has a Formattable{} method receiver. -func getConcreteFormattableType(t xunsafe.TypeIter) FormatFunc { - itab := xunsafe.GetIfaceITab[Formattable](t.Type) - switch { - case t.Indirect() && !t.IfaceIndir(): - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil { - appendNil(s) - return - } - v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) - v.Format(s) - }) - case t.Type.Kind() == reflect.Pointer && t.Type.Implements(formattableType): - // if the interface implementation is received by - // value type, the pointer type will also support - // it but it requires an extra dereference check. - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil { - appendNil(s) - return - } - v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) - v.Format(s) - }) - default: - return with_typestr_ptrs(t, func(s *State) { - v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) - v.Format(s) - }) - } -} - -// getInterfaceErrorType returns a FormatFunc to handle case of an interface{} -// type that implements error{}, i.e. error{} itself and any superset of. -func getInterfaceErrorType(t xunsafe.TypeIter) FormatFunc { - switch t.Indirect() && !t.IfaceIndir() { - case true: - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*error)(s.P) - appendString(s, v.Error()) - }) - case false: - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil { - appendNil(s) - return - } - v := *(*error)(s.P) - appendString(s, v.Error()) - }) - default: - panic("unreachable") - } -} - -// getConcreteErrorType returns a FormatFunc to handle case of concrete -// (i.e. non-interface{}) type that has an error{} method receiver. -func getConcreteErrorType(t xunsafe.TypeIter) FormatFunc { - itab := xunsafe.GetIfaceITab[error](t.Type) - switch { - case t.Indirect() && !t.IfaceIndir(): - return with_typestr_ptrs(t, func(s *State) { - s.P = *(*unsafe.Pointer)(s.P) - if s.P == nil { - appendNil(s) - return - } - v := *(*error)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.Error()) - }) - case t.Type.Kind() == reflect.Pointer && t.Type.Implements(errorType): - // if the interface implementation is received by - // value type, the pointer type will also support - // it but it requires an extra dereference check. - return with_typestr_ptrs(t, func(s *State) { - if s.P == nil { - appendNil(s) - return - } - v := *(*error)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.Error()) - }) - default: - return with_typestr_ptrs(t, func(s *State) { - v := *(*error)(xunsafe.PackIface(itab, s.P)) - appendString(s, v.Error()) - }) - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go b/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go deleted file mode 100644 index ec1e2e15d..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go +++ /dev/null @@ -1,130 +0,0 @@ -package format - -import ( - "reflect" - "unsafe" - - "codeberg.org/gruf/go-xunsafe" -) - -// derefPointerType returns a FormatFunc capable of dereferencing -// and formatting the given pointer type currently in TypeIter{}. -// note this will fetch a sub-FormatFunc for resulting value type. -func (fmt *Formatter) derefPointerType(t xunsafe.TypeIter) FormatFunc { - var n int - rtype := t.Type - flags := t.Flag - - // Iteratively dereference pointer types. - for rtype.Kind() == reflect.Pointer { - - // If this actual indirect memory, - // increase dereferences counter. - if flags&xunsafe.Reflect_flagIndir != 0 { - n++ - } - - // Get next elem type. - rtype = rtype.Elem() - - // Get next set of dereferenced element type flags. - flags = xunsafe.ReflectPointerElemFlags(flags, rtype) - } - - // Wrap value as TypeIter. - vt := t.Child(rtype, flags) - - // Get value format func. - fn := fmt.loadOrGet(vt) - if fn == nil { - panic("unreachable") - } - - if !needs_typestr(t) { - if n <= 0 { - // No derefs are needed. - return func(s *State) { - if s.P == nil { - // Final check. - appendNil(s) - return - } - - // Format - // final - // value. - fn(s) - } - } - - return func(s *State) { - // Deref n number times. - for i := n; i > 0; i-- { - - if s.P == nil { - // Nil check. - appendNil(s) - return - } - - // Further deref pointer value. - s.P = *(*unsafe.Pointer)(s.P) - } - - if s.P == nil { - // Final check. - appendNil(s) - return - } - - // Format - // final - // value. - fn(s) - } - } - - // Final type string with ptrs. - typestr := typestr_with_ptrs(t) - - if n <= 0 { - // No derefs are needed. - return func(s *State) { - if s.P == nil { - // Final nil value check. - appendNilType(s, typestr) - return - } - - // Format - // final - // value. - fn(s) - } - } - - return func(s *State) { - // Deref n number times. - for i := n; i > 0; i-- { - if s.P == nil { - // Check for nil value. - appendNilType(s, typestr) - return - } - - // Further deref pointer value. - s.P = *(*unsafe.Pointer)(s.P) - } - - if s.P == nil { - // Final nil value check. - appendNilType(s, typestr) - return - } - - // Format - // final - // value. - fn(s) - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go b/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go deleted file mode 100644 index e53fc5c4b..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go +++ /dev/null @@ -1,160 +0,0 @@ -package format - -import "codeberg.org/gruf/go-xunsafe" - -// iterSliceType returns a FormatFunc capable of iterating -// and formatting the given slice type currently in TypeIter{}. -// note this will fetch a sub-FormatFunc for the slice element -// type, and also handle special cases of []byte, []rune slices. -func (fmt *Formatter) iterSliceType(t xunsafe.TypeIter) FormatFunc { - - // Get nested element type. - elem := t.Type.Elem() - esz := elem.Size() - - // Get nested elem TypeIter{} with flags. - flags := xunsafe.ReflectSliceElemFlags(elem) - et := t.Child(elem, flags) - - // Get elem format func. - fn := fmt.loadOrGet(et) - if fn == nil { - panic("unreachable") - } - - if !needs_typestr(t) { - return func(s *State) { - ptr := s.P - - // Get data as unsafe slice header. - hdr := (*xunsafe.Unsafeheader_Slice)(ptr) - if hdr == nil || hdr.Data == nil { - - // Append nil. - appendNil(s) - return - } - - // Prepend array brace. - s.B = append(s.B, '[') - - if hdr.Len > 0 { - for i := 0; i < hdr.Len; i++ { - // Format at array index. - offset := esz * uintptr(i) - s.P = add(hdr.Data, offset) - fn(s) - - // Append separator. - s.B = append(s.B, ',') - } - - // Drop final comma. - s.B = s.B[:len(s.B)-1] - } - - // Append array brace. - s.B = append(s.B, ']') - } - } - - // Slice type string with ptrs / refs. - typestrPtrs := typestr_with_ptrs(t) - typestrRefs := typestr_with_refs(t) - - return func(s *State) { - ptr := s.P - - // Get data as unsafe slice header. - hdr := (*xunsafe.Unsafeheader_Slice)(ptr) - if hdr == nil || hdr.Data == nil { - - // Append nil value with type. - appendNilType(s, typestrPtrs) - return - } - - // Open / close braces. - var open, close uint8 - open, close = '[', ']' - - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestrRefs...) - open, close = '{', '}' - } - - // Prepend array brace. - s.B = append(s.B, open) - - if hdr.Len > 0 { - for i := 0; i < hdr.Len; i++ { - // Format at array index. - offset := esz * uintptr(i) - s.P = add(hdr.Data, offset) - fn(s) - - // Append separator. - s.B = append(s.B, ',') - } - - // Drop final comma. - s.B = s.B[:len(s.B)-1] - } - - // Append array brace. - s.B = append(s.B, close) - } -} - -func wrapByteSlice(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - if !needs_typestr(t) { - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - appendString(s, *(*string)(s.P)) - } else { - fn(s) - } - } - } - typestr := typestr_with_ptrs(t) - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")("...) - appendString(s, *(*string)(s.P)) - s.B = append(s.B, ")"...) - } else { - appendString(s, *(*string)(s.P)) - } - } else { - fn(s) - } - } -} - -func wrapRuneSlice(t xunsafe.TypeIter, fn FormatFunc) FormatFunc { - if !needs_typestr(t) { - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - appendString(s, string(*(*[]rune)(s.P))) - } else { - fn(s) - } - } - } - typestr := typestr_with_ptrs(t) - return func(s *State) { - if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { - if s.A.WithType() { - s.B = append(s.B, "("+typestr+")("...) - appendString(s, string(*(*[]rune)(s.P))) - s.B = append(s.B, ")"...) - } else { - appendString(s, string(*(*[]rune)(s.P))) - } - } else { - fn(s) - } - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/struct.go b/vendor/codeberg.org/gruf/go-kv/v2/format/struct.go deleted file mode 100644 index 617f65d81..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/struct.go +++ /dev/null @@ -1,175 +0,0 @@ -package format - -import "codeberg.org/gruf/go-xunsafe" - -// field stores the minimum necessary -// data for iterating and formatting -// each field in a given struct. -type field struct { - format FormatFunc - name string - offset uintptr -} - -// iterStructType returns a FormatFunc capable of iterating -// and formatting the given struct type currently in TypeIter{}. -// note this will fetch sub-FormatFuncs for each struct field. -func (fmt *Formatter) iterStructType(t xunsafe.TypeIter) FormatFunc { - // Number of struct fields. - n := t.Type.NumField() - - // Gather format functions. - fields := make([]field, n) - for i := 0; i < n; i++ { - - // Get struct field at index. - sfield := t.Type.Field(i) - rtype := sfield.Type - - // Get nested field TypeIter with appropriate flags. - flags := xunsafe.ReflectStructFieldFlags(t.Flag, rtype) - ft := t.Child(sfield.Type, flags) - - // Get field format func. - fn := fmt.loadOrGet(ft) - if fn == nil { - panic("unreachable") - } - - // Set field info. - fields[i] = field{ - format: fn, - name: sfield.Name, - offset: sfield.Offset, - } - } - - // Handle no. fields. - switch len(fields) { - case 0: - return emptyStructType(t) - case 1: - return iterSingleFieldStructType(t, fields[0]) - default: - return iterMultiFieldStructType(t, fields) - } -} - -func emptyStructType(t xunsafe.TypeIter) FormatFunc { - if !needs_typestr(t) { - return func(s *State) { - // Append empty object. - s.B = append(s.B, "{}"...) - } - } - - // Struct type string with refs. - typestr := typestr_with_refs(t) - - // Append empty object - // with type information. - return func(s *State) { - if s.A.WithType() { - s.B = append(s.B, typestr...) - } - s.B = append(s.B, "{}"...) - } -} - -func iterSingleFieldStructType(t xunsafe.TypeIter, field field) FormatFunc { - if field.format == nil { - panic("nil func") - } - - if !needs_typestr(t) { - return func(s *State) { - // Wrap 'fn' with braces + field name. - s.B = append(s.B, "{"+field.name+"="...) - field.format(s) - s.B = append(s.B, "}"...) - } - } - - // Struct type string with refs. - typestr := typestr_with_refs(t) - - return func(s *State) { - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestr...) - } - - // Wrap 'fn' with braces + field name. - s.B = append(s.B, "{"+field.name+"="...) - field.format(s) - s.B = append(s.B, "}"...) - } -} - -func iterMultiFieldStructType(t xunsafe.TypeIter, fields []field) FormatFunc { - for _, field := range fields { - if field.format == nil { - panic("nil func") - } - } - - if !needs_typestr(t) { - return func(s *State) { - ptr := s.P - - // Prepend object brace. - s.B = append(s.B, '{') - - for i := 0; i < len(fields); i++ { - // Get struct field ptr via offset. - s.P = add(ptr, fields[i].offset) - - // Append field name and value separator. - s.B = append(s.B, fields[i].name+"="...) - - // Format i'th field. - fields[i].format(s) - s.B = append(s.B, ',', ' ') - } - - // Drop final ", ". - s.B = s.B[:len(s.B)-2] - - // Append object brace. - s.B = append(s.B, '}') - } - } - - // Struct type string with refs. - typestr := typestr_with_refs(t) - - return func(s *State) { - ptr := s.P - - // Include type info. - if s.A.WithType() { - s.B = append(s.B, typestr...) - } - - // Prepend object brace. - s.B = append(s.B, '{') - - for i := 0; i < len(fields); i++ { - // Get struct field ptr via offset. - s.P = add(ptr, fields[i].offset) - - // Append field name and value separator. - s.B = append(s.B, fields[i].name+"="...) - - // Format i'th field. - fields[i].format(s) - s.B = append(s.B, ',', ' ') - } - - // Drop final ", ". - s.B = s.B[:len(s.B)-2] - - // Append object brace. - s.B = append(s.B, '}') - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/type.go b/vendor/codeberg.org/gruf/go-kv/v2/format/type.go deleted file mode 100644 index ce2235c88..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/type.go +++ /dev/null @@ -1,109 +0,0 @@ -package format - -import ( - "reflect" - "strings" - - "codeberg.org/gruf/go-xunsafe" -) - -// visit ... -func visit(iter xunsafe.TypeIter) bool { - t := iter.Type - - // Check if type is already encountered further up tree. - for node := iter.Parent; node != nil; node = node.Parent { - if node.Type == t { - return false - } - } - - return true -} - -// needs_typestr returns whether the type contained in the -// receiving TypeIter{} needs type string information prefixed -// when the TypeMask argument flag bit is set. Certain types -// don't need this as the parent type already indicates this. -func needs_typestr(iter xunsafe.TypeIter) bool { - if iter.Parent == nil { - return true - } - switch p := iter.Parent.Type; p.Kind() { - case reflect.Pointer: - return needs_typestr(*iter.Parent) - case reflect.Slice, - reflect.Array, - reflect.Map: - return false - default: - return true - } -} - -// typestr_with_ptrs returns the type string for -// current xunsafe.TypeIter{} with asterisks for pointers. -func typestr_with_ptrs(iter xunsafe.TypeIter) string { - t := iter.Type - - // Check for parent. - if iter.Parent == nil { - return t.String() - } - - // Get parent type. - p := iter.Parent.Type - - // If parent is not ptr, then - // this was not a deref'd ptr. - if p.Kind() != reflect.Pointer { - return t.String() - } - - // Return un-deref'd - // ptr (parent) type. - return p.String() -} - -// typestr_with_refs returns the type string for -// current xunsafe.TypeIter{} with ampersands for pointers. -func typestr_with_refs(iter xunsafe.TypeIter) string { - t := iter.Type - - // Check for parent. - if iter.Parent == nil { - return t.String() - } - - // Get parent type. - p := iter.Parent.Type - - var d int - - // Count number of dereferences. - for p.Kind() == reflect.Pointer { - p = p.Elem() - d++ - } - - if d <= 0 { - // Prefer just returning our - // own string if possible, to - // reduce number of strings - // we need to allocate. - return t.String() - } - - // Value type str. - str := t.String() - - // Return with type ptrs - // symbolized by 'refs'. - var buf strings.Builder - buf.Grow(len(str) + d) - for i := 0; i < d; i++ { - buf.WriteByte('&') - } - buf.WriteString(str) - return buf.String() -} diff --git a/vendor/codeberg.org/gruf/go-kv/v2/util.go b/vendor/codeberg.org/gruf/go-kv/v2/util.go deleted file mode 100644 index 5939ce844..000000000 --- a/vendor/codeberg.org/gruf/go-kv/v2/util.go +++ /dev/null @@ -1,147 +0,0 @@ -package kv - -import ( - "strconv" - "strings" - - "codeberg.org/gruf/go-byteutil" - "codeberg.org/gruf/go-kv/v2/format" -) - -// AppendQuoteString will append (and escape/quote where necessary) a field string. -func AppendQuoteString(buf *byteutil.Buffer, str string) { - switch { - case len(str) == 0: - // Append empty quotes. - buf.B = append(buf.B, `""`...) - return - - case len(str) == 1: - // Append quoted escaped single byte. - buf.B = format.AppendQuoteByte(buf.B, str[0]) - return - - case len(str) > format.SingleTermLine || !format.IsSafeASCII(str): - // Long line or contains non-ascii chars. - buf.B = strconv.AppendQuote(buf.B, str) - return - - case !isQuoted(str): - // Not single/double quoted already. - - if format.ContainsSpaceOrTab(str) { - // Quote un-enclosed spaces. - buf.B = append(buf.B, '"') - buf.B = append(buf.B, str...) - buf.B = append(buf.B, '"') - return - } - - if format.ContainsDoubleQuote(str) { - // Contains double quote, double quote - // and append escaped existing. - buf.B = append(buf.B, '"') - buf.B = format.AppendEscape(buf.B, str) - buf.B = append(buf.B, '"') - return - } - } - - // Double quoted, enclosed in braces, or - // literally anything else: append as-is. - buf.B = append(buf.B, str...) - return -} - -// AppendQuoteValue will append (and escape/quote where necessary) a formatted value string. -func AppendQuoteValue(buf *byteutil.Buffer, str string) { - switch { - case len(str) == 0: - // Append empty quotes. - buf.B = append(buf.B, `""`...) - return - - case len(str) == 1: - // Append quoted escaped single byte. - buf.B = format.AppendQuoteByte(buf.B, str[0]) - return - - case len(str) > format.SingleTermLine || !format.IsSafeASCII(str): - // Long line or contains non-ascii chars. - buf.B = strconv.AppendQuote(buf.B, str) - return - - case !isQuoted(str): - // Not single/double quoted already. - - // Get space / tab indices (if any). - s := strings.IndexByte(str, ' ') - t := strings.IndexByte(str, '\t') - - // Find first whitespace. - sp0 := smallest(s, t) - if sp0 < 0 { - break - } - - // Check if str is enclosed by braces. - // (but without any key-value separator). - if (enclosedBy(str, sp0, '{', '}') || - enclosedBy(str, sp0, '[', ']') || - enclosedBy(str, sp0, '(', ')')) && - strings.IndexByte(str, '=') < 0 { - break - } - - if format.ContainsDoubleQuote(str) { - // Contains double quote, double quote - // and append escaped existing. - buf.B = append(buf.B, '"') - buf.B = format.AppendEscape(buf.B, str) - buf.B = append(buf.B, '"') - return - } - - // Quote un-enclosed spaces. - buf.B = append(buf.B, '"') - buf.B = append(buf.B, str...) - buf.B = append(buf.B, '"') - return - } - - // Double quoted, enclosed in braces, or - // literally anything else: append as-is. - buf.B = append(buf.B, str...) - return -} - -// isQuoted checks if string is single or double quoted. -func isQuoted(str string) bool { - return (str[0] == '"' && str[len(str)-1] == '"') || - (str[0] == '\'' && str[len(str)-1] == '\'') -} - -// smallest attempts to return the smallest positive value of those given. -func smallest(i1, i2 int) int { - if i1 >= 0 && (i2 < 0 || i1 < i2) { - return i1 - } - return i2 -} - -// enclosedBy will check if given string is enclosed by end, and at least non-whitespace up to start. -func enclosedBy(str string, sp int, start, end byte) bool { - // Check for ending char in string. - if str[len(str)-1] != end { - return false - } - - // Check for starting char in string. - i := strings.IndexByte(str, start) - if i < 0 { - return false - } - - // Check before space. - return i < sp -} |
