lib: Rename textutil.LineWriter to WrapWriter
Fixes v.io/i/1063
MultiPart: 2/3
Change-Id: Iaea39ef808a24f8a153388608436f1280a65d4e1
diff --git a/cmd/linewrap/doc.go b/cmd/linewrap/doc.go
index b61e8e7..ef2e57c 100644
--- a/cmd/linewrap/doc.go
+++ b/cmd/linewrap/doc.go
@@ -26,7 +26,7 @@
Example usage in a unix terminal:
$ cat myfile.txt | linewrap
-See http://godoc.org/v.io/x/lib/textutil#LineWriter for details on the
+See http://godoc.org/v.io/x/lib/textutil#WrapWriter for details on the
formatting algorithm.
Usage:
diff --git a/cmd/linewrap/linewrap.go b/cmd/linewrap/linewrap.go
index 03db4ab..efddfd6 100644
--- a/cmd/linewrap/linewrap.go
+++ b/cmd/linewrap/linewrap.go
@@ -45,7 +45,7 @@
Example usage in a unix terminal:
$ cat myfile.txt | linewrap
-See http://godoc.org/v.io/x/lib/textutil#LineWriter for details on the
+See http://godoc.org/v.io/x/lib/textutil#WrapWriter for details on the
formatting algorithm.
`,
}
@@ -109,7 +109,7 @@
indents = append(indents, indent)
}
}
- w := textutil.NewUTF8LineWriter(env.Stdout, flagWidth)
+ w := textutil.NewUTF8WrapWriter(env.Stdout, flagWidth)
if err := w.SetLineTerminator(lineTerm); err != nil {
return err
}
diff --git a/cmdline/help.go b/cmdline/help.go
index 69a652a..2dce91c 100644
--- a/cmdline/help.go
+++ b/cmdline/help.go
@@ -48,14 +48,14 @@
// Run implements the Runner interface method.
func (h helpRunner) Run(env *Env, args []string) error {
- w := textutil.NewUTF8LineWriter(env.Stdout, h.width)
+ w := textutil.NewUTF8WrapWriter(env.Stdout, h.width)
defer w.Flush()
return runHelp(w, env, args, h.path, h.helpConfig)
}
// usageFunc is used as the implementation of the Env.Usage function.
func (h helpRunner) usageFunc(env *Env, writer io.Writer) {
- w := textutil.NewUTF8LineWriter(writer, h.width)
+ w := textutil.NewUTF8WrapWriter(writer, h.width)
usage(w, env, h.path, h.helpConfig, h.helpConfig.firstCall)
w.Flush()
}
@@ -104,7 +104,7 @@
}
// runHelp implements the run-time behavior of the help command.
-func runHelp(w *textutil.LineWriter, env *Env, args []string, path []*Command, config *helpConfig) error {
+func runHelp(w *textutil.WrapWriter, env *Env, args []string, path []*Command, config *helpConfig) error {
if len(args) == 0 {
usage(w, env, path, config, config.firstCall)
return nil
@@ -185,7 +185,7 @@
return string(unicode.ToUpper(r)) + s[n:]
}
-func lineBreak(w *textutil.LineWriter, style style) {
+func lineBreak(w *textutil.WrapWriter, style style) {
w.Flush()
switch style {
case styleCompact, styleFull:
@@ -215,7 +215,7 @@
}
// usageAll prints usage recursively via DFS from the path onward.
-func usageAll(w *textutil.LineWriter, env *Env, path []*Command, config *helpConfig, firstCall bool) {
+func usageAll(w *textutil.WrapWriter, env *Env, path []*Command, config *helpConfig, firstCall bool) {
cmd, cmdPath := path[len(path)-1], pathName(config.prefix, path)
usage(w, env, path, config, firstCall)
for _, child := range cmd.Children {
@@ -278,7 +278,7 @@
// usage prints the usage of the last command in path to w. The bool firstCall
// is set to false when printing usage for multiple commands, and is used to
// avoid printing redundant information (e.g. help command, global flags).
-func usage(w *textutil.LineWriter, env *Env, path []*Command, config *helpConfig, firstCall bool) {
+func usage(w *textutil.WrapWriter, env *Env, path []*Command, config *helpConfig, firstCall bool) {
cmd, cmdPath := path[len(path)-1], pathName(config.prefix, path)
env.TimerPush("usage " + cmdPath)
defer env.TimerPop()
@@ -423,7 +423,7 @@
}
}
-func flagsUsage(w *textutil.LineWriter, path []*Command, config *helpConfig) bool {
+func flagsUsage(w *textutil.WrapWriter, path []*Command, config *helpConfig) bool {
cmd, cmdPath := path[len(path)-1], pathName(config.prefix, path)
allFlags := pathFlags(path)
numCompact := countFlags(&cmd.Flags, nil, true)
@@ -450,7 +450,7 @@
return false
}
-func globalFlagsUsage(w *textutil.LineWriter, config *helpConfig) bool {
+func globalFlagsUsage(w *textutil.WrapWriter, config *helpConfig) bool {
numCompact := countFlags(globalFlags, nonHiddenGlobalFlags, true)
numFull := countFlags(globalFlags, nonHiddenGlobalFlags, false)
if config.style == styleCompact {
@@ -484,7 +484,7 @@
return
}
-func printFlags(w *textutil.LineWriter, flags, filter *flag.FlagSet, style style, regexps []*regexp.Regexp, match bool) {
+func printFlags(w *textutil.WrapWriter, flags, filter *flag.FlagSet, style style, regexps []*regexp.Regexp, match bool) {
flags.VisitAll(func(f *flag.Flag) {
if filter != nil && filter.Lookup(f.Name) != nil {
return
diff --git a/textutil/.api b/textutil/.api
index b57fbcb..9e302f8 100644
--- a/textutil/.api
+++ b/textutil/.api
@@ -3,23 +3,22 @@
pkg textutil, const ParagraphSeparator ideal-char
pkg textutil, func ByteReplaceWriter(io.Writer, byte, string) io.Writer
pkg textutil, func FlushRuneChunk(RuneChunkDecoder, func(rune) error) error
-pkg textutil, func NewLineWriter(io.Writer, int, RuneChunkDecoder, RuneEncoder) *LineWriter
-pkg textutil, func NewUTF8LineWriter(io.Writer, int) *LineWriter
+pkg textutil, func NewUTF8WrapWriter(io.Writer, int) *WrapWriter
+pkg textutil, func NewWrapWriter(io.Writer, int, RuneChunkDecoder, RuneEncoder) *WrapWriter
pkg textutil, func PrefixLineWriter(io.Writer, string) WriteFlusher
pkg textutil, func PrefixWriter(io.Writer, string) io.Writer
pkg textutil, func TerminalSize() (int, int, error)
pkg textutil, func WriteRuneChunk(RuneChunkDecoder, func(rune) error, []byte) (int, error)
-pkg textutil, method (*LineWriter) Flush() error
-pkg textutil, method (*LineWriter) ForceVerbatim(bool) error
-pkg textutil, method (*LineWriter) SetIndents(...string) error
-pkg textutil, method (*LineWriter) SetLineTerminator(string) error
-pkg textutil, method (*LineWriter) SetParagraphSeparator(string) error
-pkg textutil, method (*LineWriter) Width() int
-pkg textutil, method (*LineWriter) Write([]byte) (int, error)
pkg textutil, method (*UTF8ChunkDecoder) DecodeRune([]byte) (rune, int)
pkg textutil, method (*UTF8ChunkDecoder) FlushRune() rune
+pkg textutil, method (*WrapWriter) Flush() error
+pkg textutil, method (*WrapWriter) ForceVerbatim(bool) error
+pkg textutil, method (*WrapWriter) SetIndents(...string) error
+pkg textutil, method (*WrapWriter) SetLineTerminator(string) error
+pkg textutil, method (*WrapWriter) SetParagraphSeparator(string) error
+pkg textutil, method (*WrapWriter) Width() int
+pkg textutil, method (*WrapWriter) Write([]byte) (int, error)
pkg textutil, method (UTF8Encoder) Encode(rune, *bytes.Buffer)
-pkg textutil, type LineWriter struct
pkg textutil, type RuneChunkDecoder interface { DecodeRune, FlushRune }
pkg textutil, type RuneChunkDecoder interface, DecodeRune([]byte) (rune, int)
pkg textutil, type RuneChunkDecoder interface, FlushRune() rune
@@ -27,6 +26,7 @@
pkg textutil, type RuneEncoder interface, Encode(rune, *bytes.Buffer)
pkg textutil, type UTF8ChunkDecoder struct
pkg textutil, type UTF8Encoder struct
+pkg textutil, type WrapWriter struct
pkg textutil, type WriteFlusher interface { Flush, Write }
pkg textutil, type WriteFlusher interface, Flush() error
pkg textutil, type WriteFlusher interface, Write([]byte) (int, error)
diff --git a/textutil/doc.go b/textutil/doc.go
index cbd7e04..27c4bdc 100644
--- a/textutil/doc.go
+++ b/textutil/doc.go
@@ -6,7 +6,8 @@
//
// This package includes a combination of low-level and high-level utilities.
// The main high-level utilities are:
-// NewUTF8LineWriter: Line-based text formatter.
+// NewUTF8WrapWriter: Text formatter with line-based word wrapping.
// PrefixWriter: Add prefix to output.
+// PrefixLineWriter: Add prefix to each line in output.
// ByteReplaceWriter: Replace single byte with bytes in output.
package textutil
diff --git a/textutil/line_writer.go b/textutil/wrap_writer.go
similarity index 91%
rename from textutil/line_writer.go
rename to textutil/wrap_writer.go
index c4dcb1f..7aa3103 100644
--- a/textutil/line_writer.go
+++ b/textutil/wrap_writer.go
@@ -10,7 +10,7 @@
"unicode"
)
-// LineWriter implements an io.Writer filter that formats input text into output
+// WrapWriter implements an io.Writer filter that formats input text into output
// lines with a given target width in runes.
//
// Each input rune is classified into one of three kinds:
@@ -52,7 +52,7 @@
//
// http://www.unicode.org/reports/tr14 [Unicode Line Breaking Algorithm]
// http://www.unicode.org/versions/Unicode4.0.0/ch05.pdf [5.8 Newline Guidelines]
-type LineWriter struct {
+type WrapWriter struct {
// State configured by the user.
w io.Writer
runeDecoder RuneChunkDecoder
@@ -93,11 +93,11 @@
stateSkipSpace // Skip spaces in input line.
)
-// NewLineWriter returns a new LineWriter with the given target width in runes,
+// NewWrapWriter returns a new WrapWriter with the given target width in runes,
// producing output on the underlying writer w. The dec and enc are used to
// respectively decode runes from Write calls, and encode runes to w.
-func NewLineWriter(w io.Writer, width int, dec RuneChunkDecoder, enc RuneEncoder) *LineWriter {
- ret := &LineWriter{
+func NewWrapWriter(w io.Writer, width int, dec RuneChunkDecoder, enc RuneEncoder) *WrapWriter {
+ ret := &WrapWriter{
w: w,
runeDecoder: dec,
width: runePos(width),
@@ -111,23 +111,23 @@
return ret
}
-// NewUTF8LineWriter returns a new LineWriter filter that implements io.Writer,
+// NewUTF8WrapWriter returns a new WrapWriter filter that implements io.Writer,
// and decodes and encodes runes in UTF-8.
-func NewUTF8LineWriter(w io.Writer, width int) *LineWriter {
- return NewLineWriter(w, width, &UTF8ChunkDecoder{}, UTF8Encoder{})
+func NewUTF8WrapWriter(w io.Writer, width int) *WrapWriter {
+ return NewWrapWriter(w, width, &UTF8ChunkDecoder{}, UTF8Encoder{})
}
// Width returns the target width in runes. If width < 0 the width is
// unlimited; each paragraph is output as a single line.
-func (w *LineWriter) Width() int { return int(w.width) }
+func (w *WrapWriter) Width() int { return int(w.width) }
// SetLineTerminator sets the line terminator for subsequent Write calls. Every
// output line is terminated with term; EOL runes from the input are never
-// written to the output. A new LineWriter instance uses "\n" as the default
+// written to the output. A new WrapWriter instance uses "\n" as the default
// line terminator.
//
// Calls Flush internally, and returns any Flush error.
-func (w *LineWriter) SetLineTerminator(term string) error {
+func (w *WrapWriter) SetLineTerminator(term string) error {
if err := w.Flush(); err != nil {
return err
}
@@ -138,11 +138,11 @@
// SetParagraphSeparator sets the paragraph separator for subsequent Write
// calls. Every consecutive pair of non-empty paragraphs is separated with sep;
-// EOL runes from the input are never written to the output. A new LineWriter
+// EOL runes from the input are never written to the output. A new WrapWriter
// instance uses "\n" as the default paragraph separator.
//
// Calls Flush internally, and returns any Flush error.
-func (w *LineWriter) SetParagraphSeparator(sep string) error {
+func (w *WrapWriter) SetParagraphSeparator(sep string) error {
if err := w.Flush(); err != nil {
return err
}
@@ -160,10 +160,10 @@
//
// SetIndents() is equivalent to SetIndents(""), SetIndents("", ""), etc.
//
-// A new LineWriter instance has no indents by default.
+// A new WrapWriter instance has no indents by default.
//
// Calls Flush internally, and returns any Flush error.
-func (w *LineWriter) SetIndents(indents ...string) error {
+func (w *WrapWriter) SetIndents(indents ...string) error {
if err := w.Flush(); err != nil {
return err
}
@@ -190,17 +190,17 @@
// lines don't start with spaces.
//
// Calls Flush internally, and returns any Flush error.
-func (w *LineWriter) ForceVerbatim(v bool) error {
+func (w *WrapWriter) ForceVerbatim(v bool) error {
w.forceVerbatim = v
return w.Flush()
}
-// Write implements io.Writer by buffering data into the LineWriter w. Actual
+// Write implements io.Writer by buffering data into the WrapWriter w. Actual
// writes to the underlying writer may occur, and may include data buffered in
// either this Write call or previous Write calls.
//
// Flush must be called after the last call to Write.
-func (w *LineWriter) Write(data []byte) (int, error) {
+func (w *WrapWriter) Write(data []byte) (int, error) {
return WriteRuneChunk(w.runeDecoder, w.addRune, data)
}
@@ -211,7 +211,7 @@
//
// Flush must be called after the last call to Write, and may be called an
// arbitrary number of times before the last Write.
-func (w *LineWriter) Flush() error {
+func (w *WrapWriter) Flush() error {
if err := FlushRuneChunk(w.runeDecoder, w.addRune); err != nil {
return err
}
@@ -226,7 +226,7 @@
}
// addRune is called every time w.runeDecoder decodes a full rune.
-func (w *LineWriter) addRune(r rune) error {
+func (w *WrapWriter) addRune(r rune) error {
state, lineBreak := w.nextState(r, w.updateRune(r))
if lineBreak {
if err := w.writeLine(); err != nil {
@@ -259,7 +259,7 @@
return kindLetter
}
-func (w *LineWriter) updateRune(r rune) bool {
+func (w *WrapWriter) updateRune(r rune) bool {
forceLineBreak := false
switch kind := runeKind(r); kind {
case kindEOL:
@@ -343,7 +343,7 @@
//
// Note that Flush calls behave exactly as if an explicit U+2028 line separator
// were added to the end of all buffered data.
-func (w *LineWriter) nextState(r rune, forceLineBreak bool) (state, bool) {
+func (w *WrapWriter) nextState(r rune, forceLineBreak bool) (state, bool) {
kind := runeKind(r)
if w.forceVerbatim {
return stateVerbatim, forceLineBreak || kind == kindEOL
@@ -388,7 +388,7 @@
return stateWordWrap, false
}
-func (w *LineWriter) writeLine() error {
+func (w *WrapWriter) writeLine() error {
if w.lastWordEnd == -1 {
// Don't write blank lines, but we must reset the line in case the paragraph
// has just been terminated.
@@ -419,7 +419,7 @@
return nil
}
-func (w *LineWriter) resetLine() {
+func (w *WrapWriter) resetLine() {
w.lineBuf.Reset()
w.newWordStart = -1
w.lastWordEnd = -1
@@ -441,7 +441,7 @@
w.lineStart = w.lineBuf.ByteLen()
}
-func (w *LineWriter) bufferRune(r rune, state state, lineBreak bool) {
+func (w *WrapWriter) bufferRune(r rune, state state, lineBreak bool) {
// Never add leading spaces to the buffer in the wordWrap state.
wordWrapNoLeadingSpaces := state == stateWordWrap && !lineBreak
switch kind := runeKind(r); kind {
diff --git a/textutil/line_writer_test.go b/textutil/wrap_writer_test.go
similarity index 88%
rename from textutil/line_writer_test.go
rename to textutil/wrap_writer_test.go
index 06fdc9f..986df11 100644
--- a/textutil/line_writer_test.go
+++ b/textutil/wrap_writer_test.go
@@ -20,7 +20,7 @@
allIndents1 = [][]int{{1}, {2}, {1, 2}, {2, 1}}
)
-func TestLineWriter(t *testing.T) {
+func TestWrapWriter(t *testing.T) {
tests := []struct {
Width int
Indents [][]int
@@ -164,8 +164,8 @@
}
for _, indents := range test.Indents {
var buf bytes.Buffer
- w := newUTF8LineWriter(t, &buf, test.Width, lp, indents)
- lineWriterWriteFlush(t, w, xlateIn(test.In), sizes)
+ w := newUTF8WrapWriter(t, &buf, test.Width, lp, indents)
+ wrapWriterWriteFlush(t, w, xlateIn(test.In), sizes)
if got, want := buf.String(), xlateWant(test.Want, lp, indents); got != want {
t.Errorf("%q sizes:%v lp:%q indents:%v got %q, want %q", test.In, sizes, lp, indents, got, want)
}
@@ -175,7 +175,7 @@
}
}
-func TestLineWriterForceVerbatim(t *testing.T) {
+func TestWrapWriterForceVerbatim(t *testing.T) {
tests := []struct {
In string // See xlateIn for details on the format
Want string // See xlateIn for details on the format
@@ -206,9 +206,9 @@
// Run with a variety of chunk sizes.
for _, sizes := range [][]int{nil, {1}, {2}, {1, 2}, {2, 1}} {
var buf bytes.Buffer
- w := newUTF8LineWriter(t, &buf, 1, lp{}, nil)
+ w := newUTF8WrapWriter(t, &buf, 1, lp{}, nil)
w.ForceVerbatim(true)
- lineWriterWriteFlush(t, w, xlateIn(test.In), sizes)
+ wrapWriterWriteFlush(t, w, xlateIn(test.In), sizes)
if got, want := buf.String(), xlateIn(test.Want); got != want {
t.Errorf("%q sizes:%v got %q, want %q", test.In, sizes, got, want)
}
@@ -273,8 +273,8 @@
return strings.Repeat(" ", count)
}
-func newUTF8LineWriter(t testing.TB, buf io.Writer, width int, lp lp, indents []int) *LineWriter {
- w := NewUTF8LineWriter(buf, width)
+func newUTF8WrapWriter(t testing.TB, buf io.Writer, width int, lp lp, indents []int) *WrapWriter {
+ w := NewUTF8WrapWriter(buf, width)
if lp.line != "" || lp.para != "" {
if err := w.SetLineTerminator(lp.line); err != nil {
t.Errorf("SetLineTerminator(%q) got %v, want nil", lp.line, err)
@@ -295,7 +295,7 @@
return w
}
-func lineWriterWriteFlush(t testing.TB, w *LineWriter, text string, sizes []int) {
+func wrapWriterWriteFlush(t testing.TB, w *WrapWriter, text string, sizes []int) {
// Write chunks of different sizes until we've exhausted the input.
remain := []byte(text)
for ix := 0; len(remain) > 0; ix++ {
@@ -312,40 +312,40 @@
}
}
-func benchUTF8LineWriter(b *testing.B, width int, sizes []int) {
+func benchUTF8WrapWriter(b *testing.B, width int, sizes []int) {
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
- w := newUTF8LineWriter(b, &buf, width, lp{}, nil)
- lineWriterWriteFlush(b, w, benchText, sizes)
+ w := newUTF8WrapWriter(b, &buf, width, lp{}, nil)
+ wrapWriterWriteFlush(b, w, benchText, sizes)
}
}
-func BenchmarkUTF8LineWriter_Sizes_0_Width_0(b *testing.B) {
- benchUTF8LineWriter(b, 0, nil)
+func BenchmarkUTF8WrapWriter_Sizes_0_Width_0(b *testing.B) {
+ benchUTF8WrapWriter(b, 0, nil)
}
-func BenchmarkUTF8LineWriter_Sizes_0_Width_10(b *testing.B) {
- benchUTF8LineWriter(b, 10, nil)
+func BenchmarkUTF8WrapWriter_Sizes_0_Width_10(b *testing.B) {
+ benchUTF8WrapWriter(b, 10, nil)
}
-func BenchmarkUTF8LineWriter_Sizes_0_Width_Inf(b *testing.B) {
- benchUTF8LineWriter(b, -1, nil)
+func BenchmarkUTF8WrapWriter_Sizes_0_Width_Inf(b *testing.B) {
+ benchUTF8WrapWriter(b, -1, nil)
}
-func BenchmarkUTF8LineWriter_Sizes_1_Width_0(b *testing.B) {
- benchUTF8LineWriter(b, 0, []int{1})
+func BenchmarkUTF8WrapWriter_Sizes_1_Width_0(b *testing.B) {
+ benchUTF8WrapWriter(b, 0, []int{1})
}
-func BenchmarkUTF8LineWriter_Sizes_1_Width_10(b *testing.B) {
- benchUTF8LineWriter(b, 10, []int{1})
+func BenchmarkUTF8WrapWriter_Sizes_1_Width_10(b *testing.B) {
+ benchUTF8WrapWriter(b, 10, []int{1})
}
-func BenchmarkUTF8LineWriter_Sizes_1_Width_Inf(b *testing.B) {
- benchUTF8LineWriter(b, -1, []int{1})
+func BenchmarkUTF8WrapWriter_Sizes_1_Width_Inf(b *testing.B) {
+ benchUTF8WrapWriter(b, -1, []int{1})
}
-func BenchmarkUTF8LineWriter_Sizes_1_2_3_Width_0(b *testing.B) {
- benchUTF8LineWriter(b, 0, []int{1, 2, 3})
+func BenchmarkUTF8WrapWriter_Sizes_1_2_3_Width_0(b *testing.B) {
+ benchUTF8WrapWriter(b, 0, []int{1, 2, 3})
}
-func BenchmarkUTF8LineWriter_Sizes_1_2_3_Width_10(b *testing.B) {
- benchUTF8LineWriter(b, 10, []int{1, 2, 3})
+func BenchmarkUTF8WrapWriter_Sizes_1_2_3_Width_10(b *testing.B) {
+ benchUTF8WrapWriter(b, 10, []int{1, 2, 3})
}
-func BenchmarkUTF8LineWriter_Sizes_1_2_3_Width_Inf(b *testing.B) {
- benchUTF8LineWriter(b, -1, []int{1, 2, 3})
+func BenchmarkUTF8WrapWriter_Sizes_1_2_3_Width_Inf(b *testing.B) {
+ benchUTF8WrapWriter(b, -1, []int{1, 2, 3})
}