diff --git a/core/iox/bufferpool.go b/core/iox/bufferpool.go index 3f3c9102..9304a34e 100644 --- a/core/iox/bufferpool.go +++ b/core/iox/bufferpool.go @@ -5,11 +5,13 @@ import ( "sync" ) +// A BufferPool is a pool to buffer bytes.Buffer objects. type BufferPool struct { capability int pool *sync.Pool } +// NewBufferPool returns a BufferPool. func NewBufferPool(capability int) *BufferPool { return &BufferPool{ capability: capability, @@ -21,12 +23,14 @@ func NewBufferPool(capability int) *BufferPool { } } +// Get returns a bytes.Buffer object from bp. func (bp *BufferPool) Get() *bytes.Buffer { buf := bp.pool.Get().(*bytes.Buffer) buf.Reset() return buf } +// Put returns buf into bp. func (bp *BufferPool) Put(buf *bytes.Buffer) { if buf.Cap() < bp.capability { bp.pool.Put(buf) diff --git a/core/iox/nopcloser.go b/core/iox/nopcloser.go index 2fefc444..ba27a9f4 100644 --- a/core/iox/nopcloser.go +++ b/core/iox/nopcloser.go @@ -10,6 +10,7 @@ func (nopCloser) Close() error { return nil } +// NopCloser returns a io.WriteCloser that does nothing on calling Close. func NopCloser(w io.Writer) io.WriteCloser { return nopCloser{w} } diff --git a/core/iox/read.go b/core/iox/read.go index 6c8a76a2..481173e5 100644 --- a/core/iox/read.go +++ b/core/iox/read.go @@ -16,9 +16,11 @@ type ( omitPrefix string } + // TextReadOption defines the method to customize the text reading functions. TextReadOption func(*textReadOptions) ) +// DupReadCloser returns two io.ReadCloser that read from the first will be written to the second. // The first returned reader needs to be read first, because the content // read from it will be written to the underlying buffer of the second reader. func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) { @@ -27,6 +29,7 @@ func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) { return ioutil.NopCloser(tee), ioutil.NopCloser(&buf) } +// KeepSpace customizes the reading functions to keep leading and tailing spaces. func KeepSpace() TextReadOption { return func(o *textReadOptions) { o.keepSpace = true @@ -49,6 +52,7 @@ func ReadBytes(reader io.Reader, buf []byte) error { return nil } +// ReadText reads content from the given file with leading and tailing spaces trimmed. func ReadText(filename string) (string, error) { content, err := ioutil.ReadFile(filename) if err != nil { @@ -58,6 +62,7 @@ func ReadText(filename string) (string, error) { return strings.TrimSpace(string(content)), nil } +// ReadTextLines reads the text lines from given file. func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) { var readOpts textReadOptions for _, opt := range opts { @@ -90,12 +95,14 @@ func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) { return lines, scanner.Err() } +// WithoutBlank customizes the reading functions to ignore blank lines. func WithoutBlank() TextReadOption { return func(o *textReadOptions) { o.withoutBlanks = true } } +// OmitWithPrefix customizes the reading functions to ignore the lines with given leading prefix. func OmitWithPrefix(prefix string) TextReadOption { return func(o *textReadOptions) { o.omitPrefix = prefix diff --git a/core/iox/textfile.go b/core/iox/textfile.go index 1cc2cb0b..8feced43 100644 --- a/core/iox/textfile.go +++ b/core/iox/textfile.go @@ -8,6 +8,7 @@ import ( const bufSize = 32 * 1024 +// CountLines returns the number of lines in file. func CountLines(file string) (int, error) { f, err := os.Open(file) if err != nil { diff --git a/core/iox/textlinescanner.go b/core/iox/textlinescanner.go index ade9049b..ec28e27a 100644 --- a/core/iox/textlinescanner.go +++ b/core/iox/textlinescanner.go @@ -6,6 +6,7 @@ import ( "strings" ) +// A TextLineScanner is a scanner that can scan lines from given reader. type TextLineScanner struct { reader *bufio.Reader hasNext bool @@ -13,6 +14,7 @@ type TextLineScanner struct { err error } +// NewTextLineScanner returns a TextLineScanner with given reader. func NewTextLineScanner(reader io.Reader) *TextLineScanner { return &TextLineScanner{ reader: bufio.NewReader(reader), @@ -20,6 +22,7 @@ func NewTextLineScanner(reader io.Reader) *TextLineScanner { } } +// Scan checks if scanner has more lines to read. func (scanner *TextLineScanner) Scan() bool { if !scanner.hasNext { return false @@ -37,6 +40,7 @@ func (scanner *TextLineScanner) Scan() bool { return true } +// Line returns the next available line. func (scanner *TextLineScanner) Line() (string, error) { return scanner.line, scanner.err }