syncthing/lib/osutil/replacingwriter.go

49 lines
896 B
Go
Raw Permalink Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 20:36:35 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package osutil
import (
"bytes"
"io"
)
type ReplacingWriter struct {
Writer io.Writer
From byte
To []byte
}
func (w ReplacingWriter) Write(bs []byte) (int, error) {
var n, written int
var err error
newlineIdx := bytes.IndexByte(bs, w.From)
for newlineIdx >= 0 {
n, err = w.Writer.Write(bs[:newlineIdx])
written += n
if err != nil {
break
}
if len(w.To) > 0 {
n, err := w.Writer.Write(w.To)
if n == len(w.To) {
written++
}
if err != nil {
break
}
}
bs = bs[newlineIdx+1:]
newlineIdx = bytes.IndexByte(bs, w.From)
}
n, err = w.Writer.Write(bs)
written += n
return written, err
}