forked from MJKWoolnough/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder.go
59 lines (52 loc) · 900 Bytes
/
folder.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
package ics
import (
"io"
"unicode/utf8"
)
type folder struct {
w io.Writer
err error
line uint8
}
const maxLineLength = 75
var eol = [...]byte{'\r', '\n', ' '}
func (f *folder) Write(q []byte) (int, error) {
if f.err != nil {
return 0, f.err
}
var (
r rune
s, n, m int
)
for pos := 0; pos < len(q); pos += s {
r, s = utf8.DecodeRune(q[pos:])
f.line += uint8(s)
if r == '\n' {
f.line = 0
} else if r == '\r' {
} else if f.line > maxLineLength {
if pos > 0 {
m, f.err = f.w.Write(q[:pos])
n += m
if f.err != nil {
return n, f.err
}
q = q[pos:]
}
_, f.err = f.w.Write(eol[:])
if f.err != nil {
return n, f.err
}
pos = 0
f.line = uint8(s) + 1
}
}
if len(q) > 0 {
m, f.err = f.w.Write(q)
n += m
}
return n, f.err
}
func (f *folder) WriteString(q string) (int, error) {
return f.Write([]byte(q))
}