Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5080935

Browse files
committed
FIXUP: Switch from bufio.Scanner to strings.Lines
1 parent c6f9602 commit 5080935

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

‎internal/testing/cmp/cmp.go‎

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

77
import (
8-
"bufio"
98
"regexp"
109
"strings"
1110

@@ -98,11 +97,14 @@ var leadingTabs = regexp.MustCompile(`^\t+`)
9897
// dedentLines finds the shortest leading whitespace of every line in data and then removes it from every line.
9998
// When tabWidth is positive, leading tabs are converted to spaces first.
10099
func dedentLines(data string, tabWidth int) string {
100+
if len(data) < 1 {
101+
return ""
102+
}
103+
101104
var lines = make([]string, 0, 20)
102105
var lowest, highest string
103106

104-
for s := bufio.NewScanner(strings.NewReader(data)); s.Scan(); {
105-
line := s.Text()
107+
for line := range strings.Lines(data) {
106108
tabs := leadingTabs.FindString(line)
107109

108110
// Replace any leading tabs with spaces when tabWidth is positive.
@@ -137,14 +139,10 @@ func dedentLines(data string, tabWidth int) string {
137139
if len(lines[i]) > width {
138140
lines[i] = lines[i][width:]
139141
} else {
140-
lines[i] = ""
142+
lines[i] = "\n"
141143
}
142144
}
143145
}
144146

145-
result := strings.Join(lines, "\n")
146-
if len(lines) > 0 {
147-
result += "\n"
148-
}
149-
return result
147+
return strings.TrimSuffix(strings.Join(lines, ""), "\n") + "\n"
150148
}

‎internal/testing/cmp/cmp_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func TestDedentLines(t *testing.T) {
5555
{input: " ", expected: "\n"},
5656
{input: "\t", expected: "\n"},
5757
{input: "\t\t", expected: "\n"},
58+
59+
// blank lines preserved
60+
{input: " ~\n\n ~\n", expected: "~\n\n~\n"},
5861
} {
5962
t.Run(fmt.Sprintf("%v:%#v", tc.width, tc.input), func(t *testing.T) {
6063
assert.DeepEqual(t, dedentLines(tc.input, tc.width), tc.expected)

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package v1beta1_test
66

77
import (
8-
"bufio"
98
"fmt"
109
"regexp"
1110
"strings"
@@ -32,11 +31,14 @@ var leadingTabs = regexp.MustCompile(`^\t+`)
3231
// dedentLines finds the shortest leading whitespace of every line in data and then removes it from every line.
3332
// When tabWidth is positive, leading tabs are converted to spaces first.
3433
func dedentLines(data string, tabWidth int) string {
34+
if len(data) < 1 {
35+
return ""
36+
}
37+
3538
var lines = make([]string, 0, 20)
3639
var lowest, highest string
3740

38-
for s := bufio.NewScanner(strings.NewReader(data)); s.Scan(); {
39-
line := s.Text()
41+
for line := range strings.Lines(data) {
4042
tabs := leadingTabs.FindString(line)
4143

4244
// Replace any leading tabs with spaces when tabWidth is positive.
@@ -71,16 +73,12 @@ func dedentLines(data string, tabWidth int) string {
7173
if len(lines[i]) > width {
7274
lines[i] = lines[i][width:]
7375
} else {
74-
lines[i] = ""
76+
lines[i] = "\n"
7577
}
7678
}
7779
}
7880

79-
result := strings.Join(lines, "\n")
80-
if len(lines) > 0 {
81-
result += "\n"
82-
}
83-
return result
81+
return strings.TrimSuffix(strings.Join(lines, ""), "\n") + "\n"
8482
}
8583

8684
func TestDedentLines(t *testing.T) {
@@ -127,6 +125,9 @@ func TestDedentLines(t *testing.T) {
127125
{input: " ", expected: "\n"},
128126
{input: "\t", expected: "\n"},
129127
{input: "\t\t", expected: "\n"},
128+
129+
// blank lines preserved
130+
{input: " ~\n\n ~\n", expected: "~\n\n~\n"},
130131
} {
131132
t.Run(fmt.Sprintf("%v:%#v", tc.width, tc.input), func(t *testing.T) {
132133
assert.DeepEqual(t, dedentLines(tc.input, tc.width), tc.expected)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /