This repository has been archived on 2023-08-12. You can view files and clone it. You cannot open issues or pull requests or push a commit.
2017-04-03 10:00:38 -04:00
|
|
|
package srnd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type LineWriter struct {
|
2017-11-06 19:09:45 -05:00
|
|
|
w io.Writer
|
|
|
|
|
limit int
|
2017-04-03 10:00:38 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-06 19:09:45 -05:00
|
|
|
func NewLineWriter(w io.Writer, limit int) *LineWriter {
|
2017-04-03 10:00:38 -04:00
|
|
|
return &LineWriter{
|
2017-11-06 19:09:45 -05:00
|
|
|
w: w,
|
|
|
|
|
limit: limit,
|
2017-04-03 10:00:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LineWriter) Write(data []byte) (n int, err error) {
|
2017-11-06 19:09:45 -05:00
|
|
|
dl := len(data)
|
2017-04-03 10:00:38 -04:00
|
|
|
data = bytes.Replace(data, []byte{13, 10}, []byte{10}, -1)
|
2017-11-06 19:09:45 -05:00
|
|
|
parts := bytes.Split(data, []byte{10})
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
for len(part) > l.limit {
|
|
|
|
|
d := make([]byte, l.limit)
|
|
|
|
|
copy(d, part[:l.limit])
|
|
|
|
|
d = append(d, 10)
|
|
|
|
|
_, err = l.w.Write(d)
|
|
|
|
|
part = part[l.limit:]
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
part = append(part, 10)
|
|
|
|
|
_, err = l.w.Write(part)
|
2017-04-04 11:04:14 -04:00
|
|
|
}
|
2017-11-06 19:09:45 -05:00
|
|
|
n = dl
|
2017-04-03 10:00:38 -04:00
|
|
|
return
|
|
|
|
|
}
|