fix golint issues in core/iox (#488)
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A BufferPool is a pool to buffer bytes.Buffer objects.
|
||||||
type BufferPool struct {
|
type BufferPool struct {
|
||||||
capability int
|
capability int
|
||||||
pool *sync.Pool
|
pool *sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBufferPool returns a BufferPool.
|
||||||
func NewBufferPool(capability int) *BufferPool {
|
func NewBufferPool(capability int) *BufferPool {
|
||||||
return &BufferPool{
|
return &BufferPool{
|
||||||
capability: capability,
|
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 {
|
func (bp *BufferPool) Get() *bytes.Buffer {
|
||||||
buf := bp.pool.Get().(*bytes.Buffer)
|
buf := bp.pool.Get().(*bytes.Buffer)
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put returns buf into bp.
|
||||||
func (bp *BufferPool) Put(buf *bytes.Buffer) {
|
func (bp *BufferPool) Put(buf *bytes.Buffer) {
|
||||||
if buf.Cap() < bp.capability {
|
if buf.Cap() < bp.capability {
|
||||||
bp.pool.Put(buf)
|
bp.pool.Put(buf)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func (nopCloser) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NopCloser returns a io.WriteCloser that does nothing on calling Close.
|
||||||
func NopCloser(w io.Writer) io.WriteCloser {
|
func NopCloser(w io.Writer) io.WriteCloser {
|
||||||
return nopCloser{w}
|
return nopCloser{w}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,11 @@ type (
|
|||||||
omitPrefix string
|
omitPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TextReadOption defines the method to customize the text reading functions.
|
||||||
TextReadOption func(*textReadOptions)
|
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
|
// 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.
|
// read from it will be written to the underlying buffer of the second reader.
|
||||||
func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
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)
|
return ioutil.NopCloser(tee), ioutil.NopCloser(&buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeepSpace customizes the reading functions to keep leading and tailing spaces.
|
||||||
func KeepSpace() TextReadOption {
|
func KeepSpace() TextReadOption {
|
||||||
return func(o *textReadOptions) {
|
return func(o *textReadOptions) {
|
||||||
o.keepSpace = true
|
o.keepSpace = true
|
||||||
@@ -49,6 +52,7 @@ func ReadBytes(reader io.Reader, buf []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadText reads content from the given file with leading and tailing spaces trimmed.
|
||||||
func ReadText(filename string) (string, error) {
|
func ReadText(filename string) (string, error) {
|
||||||
content, err := ioutil.ReadFile(filename)
|
content, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -58,6 +62,7 @@ func ReadText(filename string) (string, error) {
|
|||||||
return strings.TrimSpace(string(content)), nil
|
return strings.TrimSpace(string(content)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadTextLines reads the text lines from given file.
|
||||||
func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) {
|
func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) {
|
||||||
var readOpts textReadOptions
|
var readOpts textReadOptions
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -90,12 +95,14 @@ func ReadTextLines(filename string, opts ...TextReadOption) ([]string, error) {
|
|||||||
return lines, scanner.Err()
|
return lines, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithoutBlank customizes the reading functions to ignore blank lines.
|
||||||
func WithoutBlank() TextReadOption {
|
func WithoutBlank() TextReadOption {
|
||||||
return func(o *textReadOptions) {
|
return func(o *textReadOptions) {
|
||||||
o.withoutBlanks = true
|
o.withoutBlanks = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OmitWithPrefix customizes the reading functions to ignore the lines with given leading prefix.
|
||||||
func OmitWithPrefix(prefix string) TextReadOption {
|
func OmitWithPrefix(prefix string) TextReadOption {
|
||||||
return func(o *textReadOptions) {
|
return func(o *textReadOptions) {
|
||||||
o.omitPrefix = prefix
|
o.omitPrefix = prefix
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
const bufSize = 32 * 1024
|
const bufSize = 32 * 1024
|
||||||
|
|
||||||
|
// CountLines returns the number of lines in file.
|
||||||
func CountLines(file string) (int, error) {
|
func CountLines(file string) (int, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A TextLineScanner is a scanner that can scan lines from given reader.
|
||||||
type TextLineScanner struct {
|
type TextLineScanner struct {
|
||||||
reader *bufio.Reader
|
reader *bufio.Reader
|
||||||
hasNext bool
|
hasNext bool
|
||||||
@@ -13,6 +14,7 @@ type TextLineScanner struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTextLineScanner returns a TextLineScanner with given reader.
|
||||||
func NewTextLineScanner(reader io.Reader) *TextLineScanner {
|
func NewTextLineScanner(reader io.Reader) *TextLineScanner {
|
||||||
return &TextLineScanner{
|
return &TextLineScanner{
|
||||||
reader: bufio.NewReader(reader),
|
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 {
|
func (scanner *TextLineScanner) Scan() bool {
|
||||||
if !scanner.hasNext {
|
if !scanner.hasNext {
|
||||||
return false
|
return false
|
||||||
@@ -37,6 +40,7 @@ func (scanner *TextLineScanner) Scan() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Line returns the next available line.
|
||||||
func (scanner *TextLineScanner) Line() (string, error) {
|
func (scanner *TextLineScanner) Line() (string, error) {
|
||||||
return scanner.line, scanner.err
|
return scanner.line, scanner.err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user