我有一个Go文本/模板来呈现文件,但是我发现很难在保留输出中的换行符的同时干净地构建模板。
我希望在模板中添加额外的,不必要的换行符,使其更具可读性,但要从输出中删除它们。任何超过正常段落中断的换行组都应该压缩到正常的段落中断,例如
lines with
too many breaks should become lines with
normal paragraph breaks.
字符串可能太大而无法安全存储在内存中,因此我希望将其保留为输出流。
我的第一次尝试:
type condensingWriter struct {
writer io.Writer
lastLineIsEmpty bool
}
func (c condensingWriter) Write(b []byte) (n int, err error){
thisLineIsEmpty := strings.TrimSpace(string(b)) == ""
defer func(){
c.lastLineIsEmpty = thisLineIsEmpty
}()
if c.lastLineIsEmpty && thisLineIsEmpty{
return 0, nil
} else {
return c.writer.Write(b)
}
}
这不起作用,因为我天真地认为它会缓冲新行字符,但它没有。
有关如何使其发挥作用的任何建议吗?
答案 0 :(得分:1)
受到zmb方法的启发,我提出了以下方案:
//Package striplines strips runs of consecutive empty lines from an output stream.
package striplines
import (
"io"
"strings"
)
// Striplines wraps an output stream, stripping runs of consecutive empty lines.
// You must call Flush before the output stream will be complete.
// Implements io.WriteCloser, Writer, Closer.
type Striplines struct {
Writer io.Writer
lastLine []byte
currentLine []byte
}
func (w *Striplines) Write(p []byte) (int, error) {
totalN := 0
s := string(p)
if !strings.Contains(s, "\n") {
w.currentLine = append(w.currentLine, p...)
return 0, nil
}
cur := string(append(w.currentLine, p...))
lastN := strings.LastIndex(cur, "\n")
s = cur[:lastN]
for _, line := range strings.Split(s, "\n") {
n, err := w.writeLn(line + "\n")
w.lastLine = []byte(line)
if err != nil {
return totalN, err
}
totalN += n
}
rem := cur[(lastN + 1):]
w.currentLine = []byte(rem)
return totalN, nil
}
// Close flushes the last of the output into the underlying writer.
func (w *Striplines) Close() error {
_, err := w.writeLn(string(w.currentLine))
return err
}
func (w *Striplines) writeLn(line string) (n int, err error) {
if strings.TrimSpace(string(w.lastLine)) == "" && strings.TrimSpace(line) == "" {
return 0, nil
} else {
return w.Writer.Write([]byte(line))
}
}
在此处查看此行动:http://play.golang.org/p/t8BGPUMYhb
答案 1 :(得分:0)
一般的想法是你必须在输入切片的任何地方查找连续的换行符,如果存在这样的情况,请跳过除第一个换行符之外的所有换行符。
此外,您必须跟踪写入的最后一个字节是否为换行符,因此下一次调用Write
将知道在必要时删除换行符。通过向作者类型添加bool
,您走在了正确的轨道上。但是,您需要在此处使用指针接收器而不是值接收器,否则您将修改结构的副本。
你想要改变
func (c condensingWriter) Write(b []byte)
到
func (c *condensingWriter) Write(b []byte)
您可以尝试this之类的内容。您必须使用较大的输入进行测试,以确保它能正确处理所有情况。
package main
import (
"bytes"
"io"
"os"
)
var Newline byte = byte('\n')
type ReduceNewlinesWriter struct {
w io.Writer
lastByteNewline bool
}
func (r *ReduceNewlinesWriter) Write(b []byte) (int, error) {
// if the previous call to Write ended with a \n
// then we have to skip over any starting newlines here
i := 0
if r.lastByteNewline {
for i < len(b) && b[i] == Newline {
i++
}
b = b[i:]
}
r.lastByteNewline = b[len(b) - 1] == Newline
i = bytes.IndexByte(b, Newline)
if i == -1 {
// no newlines - just write the entire thing
return r.w.Write(b)
}
// write up to the newline
i++
n, err := r.w.Write(b[:i])
if err != nil {
return n, err
}
// skip over immediate newline and recurse
i++
for i < len(b) && b[i] == Newline {
i++
}
i--
m, err := r.Write(b[i:])
return n + m, nil
}
func main() {
r := ReduceNewlinesWriter{
w: os.Stdout,
}
io.WriteString(&r, "this\n\n\n\n\n\n\nhas\nmultiple\n\n\nnewline\n\n\n\ncharacters")
}