mirror of https://sc.cryxtal.org/crystal/forgejo
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
852 B
45 lines
852 B
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package charset
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
)
|
|
|
|
// BreakWriter wraps an io.Writer to always write '\n' as '<br>'
|
|
type BreakWriter struct {
|
|
io.Writer
|
|
}
|
|
|
|
// Write writes the provided byte slice transparently replacing '\n' with '<br>'
|
|
func (b *BreakWriter) Write(bs []byte) (n int, err error) {
|
|
pos := 0
|
|
for pos < len(bs) {
|
|
idx := bytes.IndexByte(bs[pos:], '\n')
|
|
if idx < 0 {
|
|
wn, err := b.Writer.Write(bs[pos:])
|
|
return n + wn, err
|
|
}
|
|
|
|
if idx > 0 {
|
|
wn, err := b.Writer.Write(bs[pos : pos+idx])
|
|
n += wn
|
|
if err != nil {
|
|
return n, err
|
|
}
|
|
}
|
|
|
|
if _, err = b.Writer.Write([]byte("<br>")); err != nil {
|
|
return n, err
|
|
}
|
|
pos += idx + 1
|
|
|
|
n++
|
|
}
|
|
|
|
return n, err
|
|
}
|