Skip to content

Commit c732424

Browse files
committed
revert change on util.go
1 parent a5a2e00 commit c732424

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

modules/util/util.go

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package util
77
import (
88
"bytes"
99
"errors"
10-
"io"
11-
"io/ioutil"
1210
"strings"
1311
)
1412

@@ -68,59 +66,40 @@ func IsEmptyString(s string) bool {
6866
return len(strings.TrimSpace(s)) == 0
6967
}
7068

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
8174
}
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++
10588
}
106-
j++
107-
}
108-
109-
return j, nil
110-
}
11189

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++
117101
}
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]
124103
}
125104

126105
// MergeInto merges pairs of values into a "dict"

0 commit comments

Comments
 (0)