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