refactor
diff --git a/indent.go b/indent.go
index 39196f7..626de75 100644
--- a/indent.go
+++ b/indent.go
@@ -1,5 +1,9 @@
package text
+import (
+ "io"
+)
+
// Indent inserts prefix at the beginning of each non-empty line of s. The
// end-of-line marker is NL.
func Indent(s, prefix string) string {
@@ -20,3 +24,44 @@
}
return res
}
+
+// Writer indents each line of its input.
+type indentWriter struct {
+ w io.Writer
+ bol bool
+ pre [][]byte
+ sel int
+}
+
+// NewIndentWriter makes a new write filter that indents the input
+// lines. Each line is prefixed in order with the corresponding
+// element of pre. If there are more lines than elements, the last
+// element of pre is repeated for each subsequent line.
+func NewIndentWriter(w io.Writer, pre ...[]byte) io.Writer {
+ return &indentWriter{
+ w: w,
+ pre: pre,
+ bol: true,
+ }
+}
+
+// The only errors returned are from the underlying indentWriter.
+func (w *indentWriter) Write(p []byte) (n int, err error) {
+ for _, c := range p {
+ if w.bol {
+ if _, err = w.w.Write(w.pre[w.sel]); err != nil {
+ return n, err
+ }
+ }
+ _, err = w.w.Write([]byte{c})
+ if err != nil {
+ return n, err
+ }
+ n++
+ w.bol = c == '\n'
+ if w.bol && w.sel < len(w.pre)-1 {
+ w.sel++
+ }
+ }
+ return n, nil
+}
diff --git a/indent_test.go b/indent_test.go
index bffaf7a..5c723ee 100644
--- a/indent_test.go
+++ b/indent_test.go
@@ -1,6 +1,7 @@
package text
import (
+ "bytes"
"testing"
)
@@ -29,3 +30,90 @@
}
}
}
+
+type IndentWriterTest struct {
+ inp, exp string
+ pre []string
+}
+
+var ts = []IndentWriterTest{
+ {
+ `
+The quick brown fox
+jumps over the lazy
+dog.
+But not quickly.
+`[1:],
+ `
+xxxThe quick brown fox
+xxxjumps over the lazy
+xxxdog.
+xxxBut not quickly.
+`[1:],
+ []string{"xxx"},
+ },
+ {
+ `
+The quick brown fox
+jumps over the lazy
+dog.
+But not quickly.
+`[1:],
+ `
+xxaThe quick brown fox
+xxxjumps over the lazy
+xxxdog.
+xxxBut not quickly.
+`[1:],
+ []string{"xxa", "xxx"},
+ },
+ {
+ `
+The quick brown fox
+jumps over the lazy
+dog.
+But not quickly.
+`[1:],
+ `
+xxaThe quick brown fox
+xxbjumps over the lazy
+xxcdog.
+xxxBut not quickly.
+`[1:],
+ []string{"xxa", "xxb", "xxc", "xxx"},
+ },
+ {
+ `
+The quick brown fox
+jumps over the lazy
+dog.
+
+But not quickly.`[1:],
+ `
+xxaThe quick brown fox
+xxxjumps over the lazy
+xxxdog.
+xxx
+xxxBut not quickly.`[1:],
+ []string{"xxa", "xxx"},
+ },
+}
+
+func TestIndentWriter(t *testing.T) {
+ for _, test := range ts {
+ b := new(bytes.Buffer)
+ pre := make([][]byte, len(test.pre))
+ for i := range test.pre {
+ pre[i] = []byte(test.pre[i])
+ }
+ w := NewIndentWriter(b, pre...)
+ if _, err := w.Write([]byte(test.inp)); err != nil {
+ t.Error(err)
+ }
+ if got := b.String(); got != test.exp {
+ t.Errorf("mismatch %q != %q", got, test.exp)
+ t.Log(got)
+ t.Log(test.exp)
+ }
+ }
+}
diff --git a/indentwriter/indent.go b/indentwriter/indent.go
deleted file mode 100755
index cb28159..0000000
--- a/indentwriter/indent.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package indentwriter
-
-import (
- "io"
-)
-
-// Writer indents each line of its input.
-type Writer struct {
- w io.Writer
- bol bool
- pre [][]byte
- sel int
-}
-
-// NewWriter makes a new write filter that indents the input lines.
-// Each line is prefixed with the corresponding element of pre. If
-// there are more lines than elements, the last element of pre is
-// repeated for each subsequent line.
-func NewWriter(w io.Writer, pre [][]byte) io.Writer {
- return &Writer{
- w: w,
- pre: pre,
- bol: true,
- }
-}
-
-// The only errors returned are from the underlying writer.
-func (w *Writer) Write(p []byte) (n int, err error) {
- for _, c := range p {
- if w.bol {
- if _, err = w.w.Write(w.pre[w.sel]); err != nil {
- return n, err
- }
- }
- _, err = w.w.Write([]byte{c})
- if err != nil {
- return n, err
- }
- n++
- w.bol = c == '\n'
- if w.bol && w.sel < len(w.pre)-1 {
- w.sel++
- }
- }
- return n, nil
-}
diff --git a/indentwriter/indent_test.go b/indentwriter/indent_test.go
deleted file mode 100644
index 57c10d5..0000000
--- a/indentwriter/indent_test.go
+++ /dev/null
@@ -1,93 +0,0 @@
-package indentwriter
-
-import (
- "bytes"
- "testing"
-)
-
-type T struct {
- inp, exp string
- pre []string
-}
-
-var ts = []T{
- {
- `
-The quick brown fox
-jumps over the lazy
-dog.
-But not quickly.
-`[1:],
- `
-xxxThe quick brown fox
-xxxjumps over the lazy
-xxxdog.
-xxxBut not quickly.
-`[1:],
- []string{"xxx"},
- },
- {
- `
-The quick brown fox
-jumps over the lazy
-dog.
-But not quickly.
-`[1:],
- `
-xxaThe quick brown fox
-xxxjumps over the lazy
-xxxdog.
-xxxBut not quickly.
-`[1:],
- []string{"xxa", "xxx"},
- },
- {
- `
-The quick brown fox
-jumps over the lazy
-dog.
-But not quickly.
-`[1:],
- `
-xxaThe quick brown fox
-xxbjumps over the lazy
-xxcdog.
-xxxBut not quickly.
-`[1:],
- []string{"xxa", "xxb", "xxc", "xxx"},
- },
- {
- `
-The quick brown fox
-jumps over the lazy
-dog.
-
-But not quickly.`[1:],
- `
-xxaThe quick brown fox
-xxxjumps over the lazy
-xxxdog.
-xxx
-xxxBut not quickly.`[1:],
- []string{"xxa", "xxx"},
- },
-}
-
-func TestIndent(t *testing.T) {
- for _, test := range ts {
- b := new(bytes.Buffer)
- pre := make([][]byte, len(test.pre))
- for i := range test.pre {
- pre[i] = []byte(test.pre[i])
- }
- w := NewWriter(b, pre)
- if _, err := w.Write([]byte(test.inp)); err != nil {
- t.Error(err)
- }
- if got := b.String(); got != test.exp {
- t.Errorf("mismatch %q != %q", got, test.exp)
- t.Log(got)
- t.Log(test.exp)
- }
- }
-}