mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-22 09:54:24 +01:00
e81ccc406b
Change all license headers to comply with REUSE specification. Fix #16132 Co-authored-by: flynnnnnnnnnn <flynnnnnnnnnn@github> Co-authored-by: John Olheiser <john.olheiser@gmail.com>
44 lines
780 B
Go
44 lines
780 B
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
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
|
|
}
|