github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/encoding/encoding.go (about)

     1  package encoding
     2  
     3  import "github.com/prometheus/prometheus/tsdb/encoding"
     4  
     5  func EncWith(b []byte) (res Encbuf) {
     6  	res.B = b
     7  	return res
     8  }
     9  
    10  func EncWrap(inner encoding.Encbuf) Encbuf { return Encbuf{Encbuf: inner} }
    11  
    12  // Encbuf extends encoding.Encbuf with support for multi byte encoding
    13  type Encbuf struct {
    14  	encoding.Encbuf
    15  }
    16  
    17  func (e *Encbuf) PutString(s string) { e.B = append(e.B, s...) }
    18  
    19  func DecWith(b []byte) (res Decbuf) {
    20  	res.B = b
    21  	return res
    22  }
    23  
    24  func DecWrap(inner encoding.Decbuf) Decbuf { return Decbuf{Decbuf: inner} }
    25  
    26  // Decbuf extends encoding.Decbuf with support for multi byte decoding
    27  type Decbuf struct {
    28  	encoding.Decbuf
    29  }
    30  
    31  func (d *Decbuf) Bytes(n int) []byte {
    32  	if d.E != nil {
    33  		return nil
    34  	}
    35  	if len(d.B) < n {
    36  		d.E = encoding.ErrInvalidSize
    37  		return nil
    38  	}
    39  	x := d.B[:n]
    40  	d.B = d.B[n:]
    41  	return x
    42  }