github.com/ipfans/trojan-go@v0.11.0/log/golog/buffer/buffer.go (about)

     1  // Buffer-like byte slice
     2  // Copyright (c) 2017 Fadhli Dzil Ikram
     3  
     4  package buffer
     5  
     6  // Buffer type wrap up byte slice built-in type
     7  type Buffer []byte
     8  
     9  // Reset buffer position to start
    10  func (b *Buffer) Reset() {
    11  	*b = Buffer([]byte(*b)[:0])
    12  }
    13  
    14  // Append byte slice to buffer
    15  func (b *Buffer) Append(data []byte) {
    16  	*b = append(*b, data...)
    17  }
    18  
    19  // AppendByte to buffer
    20  func (b *Buffer) AppendByte(data byte) {
    21  	*b = append(*b, data)
    22  }
    23  
    24  // AppendInt to buffer
    25  func (b *Buffer) AppendInt(val int, width int) {
    26  	var repr [8]byte
    27  	reprCount := len(repr) - 1
    28  	for val >= 10 || width > 1 {
    29  		reminder := val / 10
    30  		repr[reprCount] = byte('0' + val - reminder*10)
    31  		val = reminder
    32  		reprCount--
    33  		width--
    34  	}
    35  	repr[reprCount] = byte('0' + val)
    36  	b.Append(repr[reprCount:])
    37  }
    38  
    39  // Bytes return underlying slice data
    40  func (b Buffer) Bytes() []byte {
    41  	return []byte(b)
    42  }