@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"io"
23
23
"io/fs"
24
+ "strings"
24
25
"unicode"
25
26
"unicode/utf8"
26
27
@@ -110,48 +111,42 @@ func writeInsertionPoint(
110
111
// bufio.Scanner.Scan() inline
111
112
newline := []byte {'\n' }
112
113
var found bool
113
- for targetScanner .Scan () {
114
+ for i := 0 ; targetScanner .Scan (); i ++ {
115
+ if i > 0 {
116
+ // These writes cannot fail, they will panic if they cannot allocate.
117
+ _ , _ = postInsertionContent .Write (newline )
118
+ }
114
119
targetLine := targetScanner .Bytes ()
115
120
if ! bytes .Contains (targetLine , match ) {
116
- // these writes cannot fail, they will panic if they cannot
117
- // allocate
121
+ // These writes cannot fail, they will panic if they cannot allocate.
118
122
_ , _ = postInsertionContent .Write (targetLine )
119
- _ , _ = postInsertionContent .Write (newline )
120
123
continue
121
124
}
122
125
// For each line in then new content, apply the
123
126
// same amount of whitespace. This is important
124
127
// for specific languages, e.g. Python.
125
128
whitespace := leadingWhitespace (targetLine )
126
129
127
- // Create another scanner so that we can seamlessly handle
128
- // newlines in a platform-agnostic manner.
129
- insertedContentScanner := bufio .NewScanner (bytes .NewBufferString (insertionPointFile .GetContent ()))
130
- insertedContent := scanWithPrefixAndLineEnding (insertedContentScanner , whitespace , newline )
131
- // This write cannot fail, it will panic if it cannot
132
- // allocate
133
- _ , _ = postInsertionContent .Write (insertedContent )
130
+ // Insert the content from the insertion point file. Handle newlines in
131
+ // a platform-agnostic manner.
132
+ insertedContentReader := strings .NewReader (insertionPointFile .GetContent ())
133
+ writeWithPrefixAndLineEnding (postInsertionContent , insertedContentReader , whitespace , newline )
134
134
135
135
// Code inserted at this point is placed immediately
136
136
// above the line containing the insertion point, so
137
137
// we include it last.
138
138
// These writes cannot fail, they will panic if they cannot
139
139
// allocate
140
140
_ , _ = postInsertionContent .Write (targetLine )
141
- _ , _ = postInsertionContent .Write (newline )
142
141
found = true
143
142
}
144
-
145
143
if err := targetScanner .Err (); err != nil {
146
144
return nil , err
147
145
}
148
-
149
146
if ! found {
150
147
return nil , fmt .Errorf ("could not find insertion point %q in %q" , insertionPointFile .GetInsertionPoint (), insertionPointFile .GetName ())
151
148
}
152
- // trim the trailing newline
153
- postInsertionBytes := postInsertionContent .Bytes ()
154
- return bytes .TrimSuffix (postInsertionBytes , newline ), nil
149
+ return postInsertionContent .Bytes (), nil
155
150
}
156
151
157
152
// leadingWhitespace iterates through the given string,
@@ -179,19 +174,16 @@ func leadingWhitespace(buf []byte) []byte {
179
174
return buf
180
175
}
181
176
182
- // scanWithPrefixAndLineEnding iterates over each of the given scanner 's lines
177
+ // writeWithPrefixAndLineEnding iterates over each of the given reader 's lines
183
178
// prepends prefix, and appends the newline sequence.
184
- func scanWithPrefixAndLineEnding (scanner * bufio.Scanner , prefix []byte , newline []byte ) []byte {
185
- result := bytes .NewBuffer (nil )
186
- result .Grow (averageInsertionPointSize )
179
+ func writeWithPrefixAndLineEnding (dst * bytes.Buffer , src io.Reader , prefix , newline []byte ) {
180
+ scanner := bufio .NewScanner (src )
187
181
for scanner .Scan () {
188
- // These writes cannot fail, they will panic if they cannot
189
- // allocate
190
- _ , _ = result .Write (prefix )
191
- _ , _ = result .Write (scanner .Bytes ())
192
- _ , _ = result .Write (newline )
182
+ // These writes cannot fail, they will panic if they cannot allocate.
183
+ _ , _ = dst .Write (prefix )
184
+ _ , _ = dst .Write (scanner .Bytes ())
185
+ _ , _ = dst .Write (newline )
193
186
}
194
- return result .Bytes ()
195
187
}
196
188
197
189
type writeResponseOptions struct {
0 commit comments