-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunfolder.go
83 lines (80 loc) · 1.22 KB
/
unfolder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ics
import (
"bytes"
"io"
)
type unfolder struct {
r io.Reader
bufLen int
buf [2]byte
}
func (u *unfolder) Read(p []byte) (int, error) {
var (
m, n int
err error
)
q := p
if u.bufLen > 0 {
n = copy(p, u.buf[:u.bufLen])
if n > 0 {
u.buf[0] = u.buf[1]
u.bufLen -= n
p = p[n:]
m += n
}
}
for len(p) > 0 && err == nil {
n, err = u.r.Read(p)
m += n
p = p[:n]
var toRead int
for {
pos := bytes.Index(p, eol[:])
if pos == -1 {
break
}
copy(p[pos:], p[pos+3:])
p = p[pos:]
toRead += 3
}
p = p[len(p)-toRead:]
m -= toRead
}
q = q[:m]
for err == nil {
if lq := len(q); lq > 0 && q[lq-1] == '\r' {
u.bufLen, err = u.r.Read(u.buf[:])
if u.bufLen < 2 {
break
}
if u.buf[0] == '\n' && u.buf[1] == ' ' {
m--
if err == nil {
n, err = u.r.Read(q[lq-1:])
m += n
u.bufLen = 0
}
} else {
break
}
} else if lq > 1 && q[lq-2] == '\r' && q[lq-1] == '\n' {
u.bufLen, err = u.r.Read(u.buf[:1])
if u.bufLen < 1 {
break
}
if u.buf[0] == ' ' {
m -= 2
if err == nil {
n, err = u.Read(q[lq-2:])
m += n
u.bufLen = 0
}
} else {
break
}
} else {
break
}
}
return m, err
}