|
|
@ -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
|
|
|
|