github.com/zeebo/mon@v0.0.0-20211012163247-13d39bdb54fa/internal/buffer/buffer.go (about)

     1  package buffer
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  //
     8  // custom slice support :sonic:
     9  //
    10  
    11  type (
    12  	ptr  = unsafe.Pointer
    13  	uptr = uintptr
    14  )
    15  
    16  type T struct {
    17  	base ptr
    18  	pos  uptr
    19  	cap  uptr
    20  }
    21  
    22  func Of(n []byte) T {
    23  	return T{
    24  		base: *(*ptr)(ptr(&n)),
    25  		pos:  0,
    26  		cap:  uptr(cap(n)),
    27  	}
    28  }
    29  
    30  func OfLen(n []byte) T {
    31  	return T{
    32  		base: *(*ptr)(ptr(&n)),
    33  		pos:  0,
    34  		cap:  uptr(len(n)),
    35  	}
    36  }
    37  
    38  func (buf T) Base() ptr {
    39  	return buf.base
    40  }
    41  
    42  func (buf T) Pos() uptr {
    43  	return buf.pos
    44  }
    45  
    46  func (buf T) Cap() uptr {
    47  	return buf.cap
    48  }
    49  
    50  func (buf T) SetPos(pos uintptr) T {
    51  	buf.pos = pos
    52  	return buf
    53  }
    54  
    55  func (buf T) Prefix() []byte {
    56  	return *(*[]byte)(unsafe.Pointer(&buf))
    57  }
    58  
    59  func (buf T) At(n uptr) ptr {
    60  	return ptr(uptr(buf.base) + buf.pos + n)
    61  }
    62  
    63  func (buf T) Reset() T {
    64  	buf.pos = 0
    65  	return buf
    66  }
    67  
    68  func (buf T) Front() *byte {
    69  	return (*byte)(ptr(uptr(buf.base) + buf.pos))
    70  }
    71  
    72  func (buf T) Front4() *[4]byte {
    73  	return (*[4]byte)(ptr(uptr(buf.base) + buf.pos))
    74  }
    75  
    76  func (buf T) Front8() *[8]byte {
    77  	return (*[8]byte)(ptr(uptr(buf.base) + buf.pos))
    78  }
    79  
    80  func (buf T) Front9() *[9]byte {
    81  	return (*[9]byte)(ptr(uptr(buf.base) + buf.pos))
    82  }
    83  
    84  func (buf T) Remaining() uptr {
    85  	return buf.cap - buf.pos
    86  }
    87  
    88  func (buf T) Grow() T {
    89  	if rem := buf.Remaining(); rem < 9 {
    90  		buf.cap *= 2
    91  		n := make([]byte, buf.cap)
    92  		copy(n, buf.Prefix())
    93  		buf.base = *(*ptr)(ptr(&n))
    94  	}
    95  	return buf
    96  }
    97  
    98  func (buf T) GrowN(n uintptr) T {
    99  	if rem := buf.Remaining(); rem < n {
   100  		buf.cap *= 2
   101  		n := make([]byte, buf.cap)
   102  		copy(n, buf.Prefix())
   103  		buf.base = *(*ptr)(ptr(&n))
   104  	}
   105  	return buf
   106  }
   107  
   108  func (buf T) Index(n uintptr) *byte {
   109  	return (*byte)(ptr(uptr(buf.base) + n))
   110  }
   111  
   112  func (buf T) Index4(n uintptr) *[4]byte {
   113  	return (*[4]byte)(ptr(uptr(buf.base) + n))
   114  }
   115  
   116  func (buf T) Index8(n uintptr) *[8]byte {
   117  	return (*[8]byte)(ptr(uptr(buf.base) + n))
   118  }
   119  
   120  func (buf T) Index9(n uintptr) *[9]byte {
   121  	return (*[9]byte)(ptr(uptr(buf.base) + n))
   122  }
   123  
   124  func (buf T) Advance(n uptr) T {
   125  	buf.pos += n
   126  	return buf
   127  }
   128  
   129  func (buf T) Retreat(n uptr) T {
   130  	buf.pos -= n
   131  	return buf
   132  }