1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 18:47:54 +01:00
liquid/render/trimwriter.go

52 lines
870 B
Go
Raw Normal View History

2017-07-16 23:43:04 +02:00
package render
import (
"bytes"
"io"
"unicode"
)
type trimWriter struct {
w io.Writer
buf []byte
trimRight bool
}
func (tw *trimWriter) Write(b []byte) (int, error) {
if tw.trimRight {
b = bytes.TrimLeftFunc(b, unicode.IsSpace)
} else if len(tw.buf) > 0 {
_, err := tw.w.Write(tw.buf)
tw.buf = []byte{}
if err != nil {
return 0, err
}
}
nonWS := bytes.TrimRightFunc(b, unicode.IsSpace)
if len(nonWS) < len(b) {
tw.buf = append(tw.buf, b[len(nonWS):]...)
}
return tw.w.Write(nonWS)
}
func (tw *trimWriter) Flush() (err error) {
if tw.buf != nil {
_, err = tw.w.Write(tw.buf)
tw.buf = []byte{}
}
return
}
func (tw *trimWriter) TrimLeft(f bool) {
if !f {
if err := tw.Flush(); err != nil {
panic(err)
}
}
tw.buf = []byte{}
tw.trimRight = false
}
func (tw *trimWriter) TrimRight(f bool) {
tw.trimRight = f
}