summaryrefslogtreecommitdiffstats
path: root/modules/charset/breakwriter.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /modules/charset/breakwriter.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--modules/charset/breakwriter.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/modules/charset/breakwriter.go b/modules/charset/breakwriter.go
new file mode 100644
index 0000000..a87e846
--- /dev/null
+++ b/modules/charset/breakwriter.go
@@ -0,0 +1,43 @@
+// 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
+}