Skip to content

Commit 0328d31

Browse files
toofishesjackc
authored andcommitted
Use bytes.Equal rather than bytes.Compare ==/!= 0
As recommended by go-staticcheck, but also might be a bit more efficient for the compiler to implement, since we don't care about which slice of bytes is greater than the other one.
1 parent cd46cdd commit 0328d31

File tree

12 files changed

+19
-19
lines changed

12 files changed

+19
-19
lines changed

bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func BenchmarkMinimalPgConnPreparedSelect(b *testing.B) {
151151

152152
for rr.NextRow() {
153153
for i := range rr.Values() {
154-
if bytes.Compare(rr.Values()[0], encodedBytes) != 0 {
154+
if !bytes.Equal(rr.Values()[0], encodedBytes) {
155155
b.Fatalf("unexpected values: %s %s", rr.Values()[i], encodedBytes)
156156
}
157157
}

conn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ func TestRawValuesUnderlyingMemoryReused(t *testing.T) {
12421242
rows.Close()
12431243
require.NoError(t, rows.Err())
12441244

1245-
if bytes.Compare(original, buf) != 0 {
1245+
if !bytes.Equal(original, buf) {
12461246
return
12471247
}
12481248
}

pgproto3/chunkreader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ func TestChunkReaderNextDoesNotReadIfAlreadyBuffered(t *testing.T) {
1717
if err != nil {
1818
t.Fatal(err)
1919
}
20-
if bytes.Compare(n1, src[0:2]) != 0 {
20+
if !bytes.Equal(n1, src[0:2]) {
2121
t.Fatalf("Expected read bytes to be %v, but they were %v", src[0:2], n1)
2222
}
2323

2424
n2, err := r.Next(2)
2525
if err != nil {
2626
t.Fatal(err)
2727
}
28-
if bytes.Compare(n2, src[2:4]) != 0 {
28+
if !bytes.Equal(n2, src[2:4]) {
2929
t.Fatalf("Expected read bytes to be %v, but they were %v", src[2:4], n2)
3030
}
3131

32-
if bytes.Compare((*r.buf)[:len(src)], src) != 0 {
32+
if !bytes.Equal((*r.buf)[:len(src)], src) {
3333
t.Fatalf("Expected r.buf to be %v, but it was %v", src, r.buf)
3434
}
3535

pgtype/bits_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func isExpectedEqBits(a any) func(any) bool {
1313
return func(v any) bool {
1414
ab := a.(pgtype.Bits)
1515
vb := v.(pgtype.Bits)
16-
return bytes.Compare(ab.Bytes, vb.Bytes) == 0 && ab.Len == vb.Len && ab.Valid == vb.Valid
16+
return bytes.Equal(ab.Bytes, vb.Bytes) && ab.Len == vb.Len && ab.Valid == vb.Valid
1717
}
1818
}
1919

pgtype/bytea_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func isExpectedEqBytes(a any) func(any) bool {
2525
return true
2626
}
2727

28-
return bytes.Compare(ab, vb) == 0
28+
return bytes.Equal(ab, vb)
2929
}
3030
}
3131

@@ -64,7 +64,7 @@ func TestDriverBytes(t *testing.T) {
6464
rowCount++
6565

6666
// At some point the buffer should be reused and change.
67-
if bytes.Compare(argBuf, resultBuf) != 0 {
67+
if !bytes.Equal(argBuf, resultBuf) {
6868
detectedResultMutation = true
6969
}
7070

pgtype/macaddr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func isExpectedEqHardwareAddr(a any) func(any) bool {
2222
return true
2323
}
2424

25-
return bytes.Compare(aa, vv) == 0
25+
return bytes.Equal(aa, vv)
2626
}
2727
}
2828

pgtype/numeric.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
241241
}
242242

243243
func (n *Numeric) UnmarshalJSON(src []byte) error {
244-
if bytes.Compare(src, []byte(`null`)) == 0 {
244+
if bytes.Equal(src, []byte(`null`)) {
245245
*n = Numeric{}
246246
return nil
247247
}
248-
if bytes.Compare(src, []byte(`"NaN"`)) == 0 {
248+
if bytes.Equal(src, []byte(`"NaN"`)) {
249249
*n = Numeric{NaN: true, Valid: true}
250250
return nil
251251
}

pgtype/point.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (p Point) PointValue() (Point, error) {
4040
}
4141

4242
func parsePoint(src []byte) (*Point, error) {
43-
if src == nil || bytes.Compare(src, []byte("null")) == 0 {
43+
if src == nil || bytes.Equal(src, []byte("null")) {
4444
return &Point{}, nil
4545
}
4646

pgtype/range_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ func TestParseUntypedBinaryRange(t *testing.T) {
166166
t.Errorf("%d. `%v`: expected result upper type %v, got %v", i, tt.src, string(tt.result.UpperType), string(r.UpperType))
167167
}
168168

169-
if bytes.Compare(r.Lower, tt.result.Lower) != 0 {
169+
if !bytes.Equal(r.Lower, tt.result.Lower) {
170170
t.Errorf("%d. `%v`: expected result lower %v, got %v", i, tt.src, tt.result.Lower, r.Lower)
171171
}
172172

173-
if bytes.Compare(r.Upper, tt.result.Upper) != 0 {
173+
if !bytes.Equal(r.Upper, tt.result.Upper) {
174174
t.Errorf("%d. `%v`: expected result upper %v, got %v", i, tt.src, tt.result.Upper, r.Upper)
175175
}
176176
}

pgtype/uuid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (src UUID) MarshalJSON() ([]byte, error) {
9797
}
9898

9999
func (dst *UUID) UnmarshalJSON(src []byte) error {
100-
if bytes.Compare(src, []byte("null")) == 0 {
100+
if bytes.Equal(src, []byte("null")) {
101101
*dst = UUID{}
102102
return nil
103103
}

query_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ func TestConnSimpleProtocol(t *testing.T) {
15181518
if err != nil {
15191519
t.Error(err)
15201520
}
1521-
if bytes.Compare(actual, expected) != 0 {
1521+
if !bytes.Equal(actual, expected) {
15221522
t.Errorf("expected %v got %v", expected, actual)
15231523
}
15241524
}
@@ -1825,7 +1825,7 @@ func TestConnSimpleProtocol(t *testing.T) {
18251825
if expectedBool != actualBool {
18261826
t.Errorf("expected %v got %v", expectedBool, actualBool)
18271827
}
1828-
if bytes.Compare(expectedBytes, actualBytes) != 0 {
1828+
if !bytes.Equal(expectedBytes, actualBytes) {
18291829
t.Errorf("expected %v got %v", expectedBytes, actualBytes)
18301830
}
18311831
if expectedString != actualString {

stdlib/sql_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func TestConnQueryJSONIntoByteSlice(t *testing.T) {
547547
t.Errorf("Unexpected failure: %v (sql -> %v)", err, sql)
548548
}
549549

550-
if bytes.Compare(actual, expected) != 0 {
550+
if !bytes.Equal(actual, expected) {
551551
t.Errorf(`Expected "%v", got "%v" (sql -> %v)`, string(expected), string(actual), sql)
552552
}
553553

@@ -579,7 +579,7 @@ func TestConnExecInsertByteSliceIntoJSON(t *testing.T) {
579579
err = db.QueryRow(`select body from docs`).Scan(&actual)
580580
require.NoError(t, err)
581581

582-
if bytes.Compare(actual, expected) != 0 {
582+
if !bytes.Equal(actual, expected) {
583583
t.Errorf(`Expected "%v", got "%v"`, string(expected), string(actual))
584584
}
585585

0 commit comments

Comments
 (0)