Skip to content

Commit 432c974

Browse files
committed
Use a scanner to split a string into lines to handle line termination correctly
Windows-style line endings "\r\n" are now correctly handled. Signed-off-by: Cristian Maglie <[email protected]>
1 parent e5f39c5 commit 432c974

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/arduino.cc/builder/filter_sketch_source.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030
package builder
3131

3232
import (
33-
"arduino.cc/builder/types"
34-
"arduino.cc/builder/utils"
33+
"bufio"
3534
"strconv"
3635
"strings"
36+
37+
"arduino.cc/builder/types"
38+
"arduino.cc/builder/utils"
3739
)
3840

3941
type FilterSketchSource struct {
40-
Source *string
42+
Source *string
4143
}
4244

4345
func (s *FilterSketchSource) Run(ctx *types.Context) error {
44-
lines := strings.Split(*s.Source, "\n")
45-
4646
fileNames := []string{ctx.Sketch.MainFile.Name}
4747
for _, file := range ctx.Sketch.OtherSketchFiles {
4848
fileNames = append(fileNames, file.Name)
@@ -51,7 +51,9 @@ func (s *FilterSketchSource) Run(ctx *types.Context) error {
5151
inSketch := false
5252
filtered := ""
5353

54-
for _, line := range lines {
54+
scanner := bufio.NewScanner(strings.NewReader(*s.Source))
55+
for scanner.Scan() {
56+
line := scanner.Text()
5557
filename := parseLineMarker(line)
5658
if filename != "" {
5759
inSketch = utils.SliceContains(fileNames, filename)
@@ -96,4 +98,3 @@ func parseLineMarker(line string) string {
9698
}
9799
return ""
98100
}
99-

0 commit comments

Comments
 (0)