@@ -7,8 +7,6 @@ package util
7
7
import (
8
8
"bytes"
9
9
"errors"
10
- "io"
11
- "io/ioutil"
12
10
"strings"
13
11
)
14
12
@@ -68,59 +66,40 @@ func IsEmptyString(s string) bool {
68
66
return len (strings .TrimSpace (s )) == 0
69
67
}
70
68
71
- type normalizeEOLReader struct {
72
- rd io.Reader
73
- isLastReturn bool
74
- }
75
-
76
- func (r * normalizeEOLReader ) Read (bs []byte ) (int , error ) {
77
- var p = make ([]byte , len (bs ))
78
- n , err := r .rd .Read (p )
79
- if err != nil {
80
- return n , err
69
+ // NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
70
+ func NormalizeEOL (input []byte ) []byte {
71
+ var right , left , pos int
72
+ if right = bytes .IndexByte (input , '\r' ); right == - 1 {
73
+ return input
81
74
}
82
-
83
- var j = 0
84
- for i , c := range p [:n ] {
85
- if i == 0 {
86
- if c == '\n' && r .isLastReturn {
87
- r .isLastReturn = false
88
- continue
89
- }
90
- r .isLastReturn = false
91
- }
92
- if c == '\r' {
93
- if i < n - 1 {
94
- if p [i + 1 ] != '\n' {
95
- bs [j ] = '\n'
96
- } else {
97
- continue
98
- }
99
- } else {
100
- r .isLastReturn = true
101
- bs [j ] = '\n'
102
- }
103
- } else {
104
- bs [j ] = c
75
+ length := len (input )
76
+ tmp := make ([]byte , length )
77
+
78
+ // We know that left < length because otherwise right would be -1 from IndexByte.
79
+ copy (tmp [pos :pos + right ], input [left :left + right ])
80
+ pos += right
81
+ tmp [pos ] = '\n'
82
+ left += right + 1
83
+ pos ++
84
+
85
+ for left < length {
86
+ if input [left ] == '\n' {
87
+ left ++
105
88
}
106
- j ++
107
- }
108
-
109
- return j , nil
110
- }
111
89
112
- // NormalizeEOLReader will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF) from a reader
113
- func NormalizeEOLReader (rd io.Reader ) io.Reader {
114
- return & normalizeEOLReader {
115
- rd : rd ,
116
- isLastReturn : false ,
90
+ right = bytes .IndexByte (input [left :], '\r' )
91
+ if right == - 1 {
92
+ copy (tmp [pos :], input [left :])
93
+ pos += length - left
94
+ break
95
+ }
96
+ copy (tmp [pos :pos + right ], input [left :left + right ])
97
+ pos += right
98
+ tmp [pos ] = '\n'
99
+ left += right + 1
100
+ pos ++
117
101
}
118
- }
119
-
120
- // NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
121
- func NormalizeEOL (input []byte ) []byte {
122
- bs , _ := ioutil .ReadAll (NormalizeEOLReader (bytes .NewReader (input )))
123
- return bs
102
+ return tmp [:pos ]
124
103
}
125
104
126
105
// MergeInto merges pairs of values into a "dict"
0 commit comments