Commits

John Floren committed ca102ea

initial commit

Comments (0)

Files changed (4)

+package censor
+
+import (
+	"bytes"
+	"io"
+)
+
+type Reader struct {
+	Replacements	[]string
+	reader		io.Reader
+}
+
+func NewReader(r io.Reader, replacements []string) (ret *Reader) {
+	ret = &Reader{replacements, r}
+	return ret
+}
+
+func (r *Reader) Read(b []byte) (n int, err error) {
+	res := b
+	n, err = r.reader.Read(res)
+	if err == nil {
+		for _, s := range r.Replacements {
+			toreplace := bytes.Repeat([]byte{ 88 }, len(s))
+			res = bytes.Replace(res, []byte(s), toreplace, -1)
+		}
+	}
+	copy(b, res)
+	return n, err
+}
+

censor/censor_test.go

+package censor_test
+
+import (
+	"testing"
+	"io"
+	"bitbucket.org/floren/lesson/censor"
+	"os"
+)
+
+func TestNewReader(t *testing.T) {
+	f, _ := os.Open("sample.txt")
+	c := censor.NewReader(f, []string{"Fritz", "Sandia"})
+	io.Copy(os.Stdout, c)
+}

censor/sample.txt

+David Fritz works for Sandia National Labs

censor/xfiles.txt

+UFO reports in Livermore, California
+--------------------------------------
+Agents Mulder and Scully found no evidence of UFO activity at this location.