Skip to content

Commit c6f9602

Browse files
committed
FIXUP: switch from []byte to string
1 parent 54c8b7d commit c6f9602

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

internal/testing/cmp/cmp.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ package cmp
66

77
import (
88
"bufio"
9-
"bytes"
109
"regexp"
11-
"slices"
1210
"strings"
1311

1412
gocmp "github.com/google/go-cmp/cmp"
@@ -83,9 +81,9 @@ func MarshalMatches(actual any, expected string) Comparison {
8381
if err != nil {
8482
return func() gotest.Result { return gotest.ResultFromError(err) }
8583
}
86-
return gotest.DeepEqual(string(b), string(dedentLines(
87-
[]byte(strings.TrimLeft(strings.TrimRight(expected, "\t\n"), "\n")), 2,
88-
)))
84+
return gotest.DeepEqual(string(b), dedentLines(
85+
strings.TrimLeft(strings.TrimRight(expected, "\t\n"), "\n"), 2,
86+
))
8987
}
9088

9189
// Regexp succeeds if value contains any match of the regular expression.
@@ -99,32 +97,27 @@ var leadingTabs = regexp.MustCompile(`^\t+`)
9997

10098
// dedentLines finds the shortest leading whitespace of every line in data and then removes it from every line.
10199
// When tabWidth is positive, leading tabs are converted to spaces first.
102-
func dedentLines(data []byte, tabWidth int) []byte {
103-
var lines = make([][]byte, 0, 20)
104-
var lowest, highest []byte
100+
func dedentLines(data string, tabWidth int) string {
101+
var lines = make([]string, 0, 20)
102+
var lowest, highest string
105103

106-
for s := bufio.NewScanner(bytes.NewReader(data)); s.Scan(); {
107-
line := slices.Clone(s.Bytes())
108-
tabs := leadingTabs.Find(line)
104+
for s := bufio.NewScanner(strings.NewReader(data)); s.Scan(); {
105+
line := s.Text()
106+
tabs := leadingTabs.FindString(line)
109107

110108
// Replace any leading tabs with spaces when tabWidth is positive.
111109
// NOTE: [strings.Repeat] has a fast-path for spaces.
112110
if need := tabWidth * len(tabs); need > 0 {
113-
line = append(append(make([]byte, 0, need+len(line)),
114-
strings.Repeat(" ", need)...), line[len(tabs):]...)
111+
line = strings.Repeat(" ", need) + line[len(tabs):]
115112
}
116113

117114
switch {
118-
case lowest == nil, highest == nil:
115+
case lowest == "", highest == "":
119116
lowest, highest = line, line
120117

121-
case len(bytes.TrimSpace(line)) > 0:
122-
if bytes.Compare(line, lowest) < 0 {
123-
lowest = line
124-
}
125-
if bytes.Compare(line, highest) > 0 {
126-
highest = line
127-
}
118+
case len(strings.TrimSpace(line)) > 0:
119+
lowest = min(lowest, line)
120+
highest = max(highest, line)
128121
}
129122

130123
lines = append(lines, line)
@@ -144,14 +137,14 @@ func dedentLines(data []byte, tabWidth int) []byte {
144137
if len(lines[i]) > width {
145138
lines[i] = lines[i][width:]
146139
} else {
147-
lines[i] = nil
140+
lines[i] = ""
148141
}
149142
}
150143
}
151144

152-
result := bytes.Join(lines, []byte("\n"))
145+
result := strings.Join(lines, "\n")
153146
if len(lines) > 0 {
154-
result = append(result, '\n')
147+
result += "\n"
155148
}
156149
return result
157150
}

internal/testing/cmp/cmp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestDedentLines(t *testing.T) {
5757
{input: "\t\t", expected: "\n"},
5858
} {
5959
t.Run(fmt.Sprintf("%v:%#v", tc.width, tc.input), func(t *testing.T) {
60-
assert.DeepEqual(t, dedentLines([]byte(tc.input), tc.width), []byte(tc.expected))
60+
assert.DeepEqual(t, dedentLines(tc.input, tc.width), tc.expected)
6161
})
6262
}
6363
}

pkg/apis/postgres-operator.crunchydata.com/v1beta1/cmp_test.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ package v1beta1_test
66

77
import (
88
"bufio"
9-
"bytes"
109
"fmt"
1110
"regexp"
12-
"slices"
1311
"strings"
1412
"testing"
1513

@@ -24,41 +22,36 @@ func MarshalsTo[T []byte | string](x any, y T) cmp.Comparison {
2422
if err != nil {
2523
return func() cmp.Result { return cmp.ResultFromError(err) }
2624
}
27-
return cmp.DeepEqual(string(b), string(dedentLines(
28-
bytes.TrimLeft(bytes.TrimRight([]byte(y), "\t\n"), "\n"), 2,
29-
)))
25+
return cmp.DeepEqual(string(b), dedentLines(
26+
strings.TrimLeft(strings.TrimRight(string(y), "\t\n"), "\n"), 2,
27+
))
3028
}
3129

3230
var leadingTabs = regexp.MustCompile(`^\t+`)
3331

3432
// dedentLines finds the shortest leading whitespace of every line in data and then removes it from every line.
3533
// When tabWidth is positive, leading tabs are converted to spaces first.
36-
func dedentLines(data []byte, tabWidth int) []byte {
37-
var lines = make([][]byte, 0, 20)
38-
var lowest, highest []byte
34+
func dedentLines(data string, tabWidth int) string {
35+
var lines = make([]string, 0, 20)
36+
var lowest, highest string
3937

40-
for s := bufio.NewScanner(bytes.NewReader(data)); s.Scan(); {
41-
line := slices.Clone(s.Bytes())
42-
tabs := leadingTabs.Find(line)
38+
for s := bufio.NewScanner(strings.NewReader(data)); s.Scan(); {
39+
line := s.Text()
40+
tabs := leadingTabs.FindString(line)
4341

4442
// Replace any leading tabs with spaces when tabWidth is positive.
4543
// NOTE: [strings.Repeat] has a fast-path for spaces.
4644
if need := tabWidth * len(tabs); need > 0 {
47-
line = append(append(make([]byte, 0, need+len(line)),
48-
strings.Repeat(" ", need)...), line[len(tabs):]...)
45+
line = strings.Repeat(" ", need) + line[len(tabs):]
4946
}
5047

5148
switch {
52-
case lowest == nil, highest == nil:
49+
case lowest == "", highest == "":
5350
lowest, highest = line, line
5451

55-
case len(bytes.TrimSpace(line)) > 0:
56-
if bytes.Compare(line, lowest) < 0 {
57-
lowest = line
58-
}
59-
if bytes.Compare(line, highest) > 0 {
60-
highest = line
61-
}
52+
case len(strings.TrimSpace(line)) > 0:
53+
lowest = min(lowest, line)
54+
highest = max(highest, line)
6255
}
6356

6457
lines = append(lines, line)
@@ -78,14 +71,14 @@ func dedentLines(data []byte, tabWidth int) []byte {
7871
if len(lines[i]) > width {
7972
lines[i] = lines[i][width:]
8073
} else {
81-
lines[i] = nil
74+
lines[i] = ""
8275
}
8376
}
8477
}
8578

86-
result := bytes.Join(lines, []byte("\n"))
79+
result := strings.Join(lines, "\n")
8780
if len(lines) > 0 {
88-
result = append(result, '\n')
81+
result += "\n"
8982
}
9083
return result
9184
}
@@ -136,7 +129,7 @@ func TestDedentLines(t *testing.T) {
136129
{input: "\t\t", expected: "\n"},
137130
} {
138131
t.Run(fmt.Sprintf("%v:%#v", tc.width, tc.input), func(t *testing.T) {
139-
assert.DeepEqual(t, dedentLines([]byte(tc.input), tc.width), []byte(tc.expected))
132+
assert.DeepEqual(t, dedentLines(tc.input, tc.width), tc.expected)
140133
})
141134
}
142135
}

0 commit comments

Comments
 (0)