Beispiel #1
0
func (w wordWrapWriter) Write(p []byte) (nn int, err error) {
	if w.limit == 0 {
		return w.writer.Write(p)
	}
	original := string(p)
	wrapped := wordwrap.WrapString(original, w.limit)
	return w.writer.Write([]byte(wrapped))
}
func (w wordWrapWriter) Write(p []byte) (nn int, err error) {
	if w.limit == 0 {
		return w.writer.Write(p)
	}
	original := string(p)
	wrapped := wordwrap.WrapString(original, w.limit)
	l, e := w.writer.Write([]byte(wrapped))

	// This wrapped word writer could modify input in some cases such that
	// the length of output is not equal to the length of input and this
	// can cause ErrShortWrite error. So to avoid the error, make sure the
	// returned length of output bytes is equal to the length of input.
	// This returns length of original in non-erroneous cases, and in erroneous
	// cases min(len(original), l).
	if e == nil || l > len(original) {
		l = len(original)
	}

	return l, e
}