Documentation
¶
Overview ¶
Borrowed from jsoniter (https://github.com/json-iterator/go)
Index ¶
- func BytesToPointer[T any](b []byte) *T
- func BytesToString(b []byte) string
- func MakeNoZero(l int) []byte
- func MakeNoZeroCap(l int, c int) []byte
- func Nanotime() int64
- func NanotimeSince(ts int64) time.Duration
- func Noescape[T any](p T) T
- func NoescapeUnsafe(p unsafe.Pointer) unsafe.Pointer
- func PointerToBytes[T any](val *T, length int) []byte
- func PointerToBytesOffset[T any](val *T, length, offset int) []byte
- func SliceToSlice[From any, To any](from []From, toLength int) []To
- func SliceUnsafePointer[T any](slice []T) unsafe.Pointer
- func StringToBytes(s string) []byte
- type AtomicInt32Pair
- func (x *AtomicInt32Pair) Add(deltaA, deltaB int32) (newA, newB int32)
- func (x *AtomicInt32Pair) CompareAndSwap(oldA, oldB, newA, newB int32) (swapped bool)
- func (x *AtomicInt32Pair) Load() (int32, int32)
- func (x *AtomicInt32Pair) Store(a, b int32)
- func (x *AtomicInt32Pair) Swap(newA, newB int32) (oldA, oldB int32)
- type BinaryAppender
- type Clock
- type JsonAppender
- type Pool
- type StringBuffer
- func (b StringBuffer) Bytes() []byte
- func (b *StringBuffer) Cap() int
- func (b *StringBuffer) Grow(n int)
- func (b *StringBuffer) Len() int
- func (b *StringBuffer) Reset()
- func (b StringBuffer) String() string
- func (b *StringBuffer) Write(p []byte) (int, error)
- func (b *StringBuffer) WriteBool(v bool)
- func (b *StringBuffer) WriteByte(c byte) error
- func (b *StringBuffer) WriteEnc(v StringEncoder)
- func (b *StringBuffer) WriteFloat32(val float32)
- func (b *StringBuffer) WriteFloat32Lossy(val float32)
- func (b *StringBuffer) WriteFloat64(val float64)
- func (b *StringBuffer) WriteFloat64Lossy(val float64)
- func (b *StringBuffer) WriteInt(val int)
- func (b *StringBuffer) WriteInt16(nval int16)
- func (b *StringBuffer) WriteInt32(nval int32)
- func (b *StringBuffer) WriteInt64(nval int64)
- func (b *StringBuffer) WriteInt8(nval int8)
- func (b *StringBuffer) WriteRune(r rune) (int, error)
- func (b *StringBuffer) WriteString(s string) (int, error)
- func (b *StringBuffer) WriteUint(val uint)
- func (b *StringBuffer) WriteUint16(val uint16)
- func (b *StringBuffer) WriteUint32(val uint32)
- func (b *StringBuffer) WriteUint64(val uint64)
- func (b *StringBuffer) WriteUint8(val uint8)
- func (b *StringBuffer) WriteVal(val any)
- type StringEncoder
- type StringWriter
- func (w StringWriter) Available() int
- func (w StringWriter) AvailableBuffer() []byte
- func (w StringWriter) Close() (err error)
- func (w StringWriter) Flush() (err error)
- func (w StringWriter) Write(p []byte) (n int, err error)
- func (w StringWriter) WriteBool(v bool)
- func (w StringWriter) WriteByte(c byte) error
- func (w StringWriter) WriteFloat32(val float32)
- func (w StringWriter) WriteFloat32Lossy(val float32)
- func (w StringWriter) WriteFloat64(val float64)
- func (w StringWriter) WriteFloat64Lossy(val float64)
- func (w StringWriter) WriteInt(val int)
- func (w StringWriter) WriteInt16(val int16)
- func (w StringWriter) WriteInt32(val int32)
- func (w StringWriter) WriteInt64(val int64)
- func (w StringWriter) WriteInt8(val int8)
- func (w StringWriter) WriteRune(r rune) (int, error)
- func (w StringWriter) WriteString(s string) (int, error)
- func (w StringWriter) WriteTo(w2 io.Writer) (int64, error)
- func (w StringWriter) WriteUint(val uint)
- func (w StringWriter) WriteUint16(val uint16)
- func (w StringWriter) WriteUint32(val uint32)
- func (w StringWriter) WriteUint64(val uint64)
- func (w StringWriter) WriteUint8(val uint8)
- func (w StringWriter) WriteVal(val any) (err error)
- type TextAppender
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToPointer ¶
func BytesToString ¶
func MakeNoZero ¶
MakeNoZero makes a slice of length and capacity l without zeroing the bytes. It is the caller's responsibility to ensure uninitialized bytes do not leak to the end user.
Example ¶
b := MakeNoZero(16)
fmt.Printf("Length %d, capacity %d", len(b), cap(b))
Output: Length 16, capacity 16
func MakeNoZeroCap ¶
MakeNoZero makes a slice of length l and capacity c without zeroing the bytes. It is the caller's responsibility to ensure uninitialized bytes do not leak to the end user.
Example ¶
b := MakeNoZeroCap(0, 16)
fmt.Printf("Length %d, capacity %d", len(b), cap(b))
Output: Length 0, capacity 16
func Nanotime ¶ added in v0.16.0
func Nanotime() int64
Returns a fast, monotonic time suitable for measuring time taken between two calls.
Example ¶
start := Nanotime() time.Sleep(100 * time.Millisecond) end := Nanotime() // This will be ~100 ms (most likely 101 ms) taken := end - start fmt.Println(taken / int64(time.Millisecond))
Output: 101
func NanotimeSince ¶ added in v0.16.0
Returns the duration since a given timestamp acquired from Nanotime.
func NoescapeUnsafe ¶ added in v0.17.0
noescape hides a pointer from escape analysis. It is the identity function but escape analysis doesn't think the output depends on the input. noescape is inlined and currently compiles down to zero instructions. USE CAREFULLY! This was copied from the runtime; see issues 23382 and 7921.
func PointerToBytes ¶
func PointerToBytesOffset ¶
func SliceToSlice ¶
func SliceUnsafePointer ¶
func StringToBytes ¶
Types ¶
type AtomicInt32Pair ¶
type AtomicInt32Pair struct {
// contains filtered or unexported fields
}
Example ¶
var v AtomicInt32Pair
for i := 0; i < 100; i++ {
v.Add(1, -1)
}
fmt.Println(v.Load())
Output: 100 -100
func (*AtomicInt32Pair) Add ¶
func (x *AtomicInt32Pair) Add(deltaA, deltaB int32) (newA, newB int32)
Add atomically adds delta to x and returns the new value.
func (*AtomicInt32Pair) CompareAndSwap ¶
func (x *AtomicInt32Pair) CompareAndSwap(oldA, oldB, newA, newB int32) (swapped bool)
CompareAndSwap executes the compare-and-swap operation for x.
func (*AtomicInt32Pair) Load ¶
func (x *AtomicInt32Pair) Load() (int32, int32)
Load atomically loads and returns the value stored in x.
func (*AtomicInt32Pair) Store ¶
func (x *AtomicInt32Pair) Store(a, b int32)
Store atomically stores val into x.
func (*AtomicInt32Pair) Swap ¶
func (x *AtomicInt32Pair) Swap(newA, newB int32) (oldA, oldB int32)
Swap atomically stores new into x and returns the previous value.
type BinaryAppender ¶ added in v0.11.0
type BinaryAppender interface {
// AppendBinary appends the binary representation of itself to the end of b
// (allocating a larger slice if necessary) and returns the updated slice.
//
// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
AppendBinary(b []byte) ([]byte, error)
}
type JsonAppender ¶ added in v0.13.0
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
func NewPool ¶
Created a new pool and accepts two (2) optional callbacks. The first is a initializer, and will be called whenever a new item (T) is created. The last is a resetter, and will be called whenever an item is released back to the pool.
func NewStringBufferPool ¶
func NewStringBufferPool() *Pool[StringBuffer]
type StringBuffer ¶
type StringBuffer struct {
// contains filtered or unexported fields
}
A StringBuffer is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero StringBuffer.
Example ¶
var b StringBuffer
b.WriteString("hello")
b.WriteByte(' ')
b.WriteInt(123)
fmt.Println(b)
b.Reset()
b.WriteFloat64Lossy(123.4567891)
b.WriteByte(' ')
b.WriteBool(true)
fmt.Println(b)
Output: hello 123 123.456789 true
func NewStringBuffer ¶
func NewStringBuffer(cap int) *StringBuffer
func (StringBuffer) Bytes ¶
func (b StringBuffer) Bytes() []byte
String returns the accumulated string as bytes.
func (*StringBuffer) Cap ¶
func (b *StringBuffer) Cap() int
Cap returns the capacity of the StringBuffer's underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.
func (*StringBuffer) Grow ¶
func (b *StringBuffer) Grow(n int)
Grow grows b's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation. If n is negative, Grow panics.
func (*StringBuffer) Len ¶
func (b *StringBuffer) Len() int
Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (*StringBuffer) Reset ¶
func (b *StringBuffer) Reset()
Reset resets the StringBuffer to be empty.
func (StringBuffer) String ¶
func (b StringBuffer) String() string
String returns the accumulated string.
func (*StringBuffer) Write ¶
func (b *StringBuffer) Write(p []byte) (int, error)
Write appends the contents of p to b's buffer. Write always returns len(p), nil.
func (*StringBuffer) WriteBool ¶
func (b *StringBuffer) WriteBool(v bool)
func (*StringBuffer) WriteByte ¶
func (b *StringBuffer) WriteByte(c byte) error
WriteByte appends the byte c to b's buffer. The returned error is always nil.
func (*StringBuffer) WriteEnc ¶
func (b *StringBuffer) WriteEnc(v StringEncoder)
Write a type that implements StringEncoder
func (*StringBuffer) WriteFloat32Lossy ¶
func (b *StringBuffer) WriteFloat32Lossy(val float32)
Write float32 with ONLY 6 digits precision although much much faster
func (*StringBuffer) WriteFloat64Lossy ¶
func (b *StringBuffer) WriteFloat64Lossy(val float64)
Write float64 with ONLY 6 digits precision although much much faster
func (*StringBuffer) WriteRune ¶
func (b *StringBuffer) WriteRune(r rune) (int, error)
WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer. It returns the length of r and a nil error.
func (*StringBuffer) WriteString ¶
func (b *StringBuffer) WriteString(s string) (int, error)
WriteString appends the contents of s to b's buffer. It returns the length of s and a nil error.
func (*StringBuffer) WriteVal ¶
func (b *StringBuffer) WriteVal(val any)
type StringEncoder ¶
type StringEncoder interface {
EncodeString(b *StringBuffer)
}
type StringWriter ¶ added in v0.11.0
type StringWriter struct {
// contains filtered or unexported fields
}
func NewStringWriter ¶ added in v0.11.0
func NewStringWriter(w io.Writer, buf ...*StringBuffer) (sw StringWriter)
func (StringWriter) Available ¶ added in v0.11.0
func (w StringWriter) Available() int
func (StringWriter) AvailableBuffer ¶ added in v0.11.0
func (w StringWriter) AvailableBuffer() []byte
func (StringWriter) Close ¶ added in v0.11.0
func (w StringWriter) Close() (err error)
Close implements io.WriteCloser.
func (StringWriter) Flush ¶ added in v0.11.0
func (w StringWriter) Flush() (err error)
func (StringWriter) Write ¶ added in v0.11.0
func (w StringWriter) Write(p []byte) (n int, err error)
func (StringWriter) WriteBool ¶ added in v0.11.0
func (w StringWriter) WriteBool(v bool)
func (StringWriter) WriteByte ¶ added in v0.11.0
func (w StringWriter) WriteByte(c byte) error
func (StringWriter) WriteFloat32 ¶ added in v0.11.0
func (w StringWriter) WriteFloat32(val float32)
func (StringWriter) WriteFloat32Lossy ¶ added in v0.11.0
func (w StringWriter) WriteFloat32Lossy(val float32)
func (StringWriter) WriteFloat64 ¶ added in v0.11.0
func (w StringWriter) WriteFloat64(val float64)
func (StringWriter) WriteFloat64Lossy ¶ added in v0.11.0
func (w StringWriter) WriteFloat64Lossy(val float64)
func (StringWriter) WriteInt ¶ added in v0.11.0
func (w StringWriter) WriteInt(val int)
func (StringWriter) WriteInt16 ¶ added in v0.11.0
func (w StringWriter) WriteInt16(val int16)
func (StringWriter) WriteInt32 ¶ added in v0.11.0
func (w StringWriter) WriteInt32(val int32)
func (StringWriter) WriteInt64 ¶ added in v0.11.0
func (w StringWriter) WriteInt64(val int64)
func (StringWriter) WriteInt8 ¶ added in v0.11.0
func (w StringWriter) WriteInt8(val int8)
func (StringWriter) WriteRune ¶ added in v0.11.0
func (w StringWriter) WriteRune(r rune) (int, error)
func (StringWriter) WriteString ¶ added in v0.11.0
func (w StringWriter) WriteString(s string) (int, error)
func (StringWriter) WriteTo ¶ added in v0.11.0
func (w StringWriter) WriteTo(w2 io.Writer) (int64, error)
WriteTo implements io.WriterTo.
func (StringWriter) WriteUint ¶ added in v0.11.0
func (w StringWriter) WriteUint(val uint)
func (StringWriter) WriteUint16 ¶ added in v0.11.0
func (w StringWriter) WriteUint16(val uint16)
func (StringWriter) WriteUint32 ¶ added in v0.11.0
func (w StringWriter) WriteUint32(val uint32)
func (StringWriter) WriteUint64 ¶ added in v0.11.0
func (w StringWriter) WriteUint64(val uint64)
func (StringWriter) WriteUint8 ¶ added in v0.11.0
func (w StringWriter) WriteUint8(val uint8)
func (StringWriter) WriteVal ¶ added in v0.11.0
func (w StringWriter) WriteVal(val any) (err error)