github.com/hedzr/evendeep@v0.4.8/diff/c_bytesbuffer.go (about)

     1  package diff
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  
     7  	"github.com/hedzr/evendeep/internal/tool"
     8  )
     9  
    10  type bytesBufferComparer struct{}
    11  
    12  func (c *bytesBufferComparer) Match(typ reflect.Type) bool {
    13  	return typ.String() == "bytes.Buffer"
    14  }
    15  
    16  func (c *bytesBufferComparer) Equal(ctx Context, lhs, rhs reflect.Value, path Path) (equal bool) {
    17  	a := lhs.Interface().(bytes.Buffer) //nolint:errcheck //no need
    18  	b := rhs.Interface().(bytes.Buffer) //nolint:errcheck //no need
    19  	if equal = c.equal(a.Bytes(), b.Bytes()); !equal {
    20  		ctx.PutModified(ctx.PutPath(path), Update{Old: a.String(), New: b.String(), Typ: tool.Typfmtvlite(&lhs)})
    21  	}
    22  	return
    23  }
    24  
    25  func (c *bytesBufferComparer) equal(a, b []byte) bool {
    26  	if len(a) != len(b) {
    27  		return false
    28  	}
    29  	for i := 0; i < len(a); i++ {
    30  		if a[i] != b[i] {
    31  			return false
    32  		}
    33  	}
    34  	return true
    35  }