Documentation ¶
Index ¶
- Constants
- func ByteReadLine(r *bufio.Reader) ([]byte, error)
- func DirExists(dir string) bool
- func FileExists(file string) bool
- func NewScannerByDelim(r io.Reader, delim []byte, flags ScannerByDelimFlag) *bufio.Scanner
- func NewScannerByDelim2(r io.Reader, delim, escape []byte, flags ScannerByDelimFlag) *bufio.Scanner
- func NewScannerByDelim3(r io.Reader, delim, escape []byte, flags ScannerByDelimFlag, buf []byte) *bufio.Scanner
- func ReadLine(r *bufio.Reader) (string, error)
- func StripBOM(reader io.Reader) (io.Reader, error)
- type BytesReplacer
- type BytesReplacingReader
- type LineCountingReader
- type LineEditFunc
- type LineEditingReader
- type LineNumReportingCsvReader
- type ScannerByDelimFlag
Constants ¶
const ( // ScannerByDelimFlagEofAsDelim specifies that the scanner should treat EOF as the delimiter as well. ScannerByDelimFlagEofAsDelim ScannerByDelimFlag = 1 << iota // ScannerByDelimFlagDropDelimInReturn specifies that the delimiter should NOT be included in the return value. ScannerByDelimFlagDropDelimInReturn // ScannerByDelimFlagEofNotAsDelim specifies that the scanner should NOT treat EOF as the delimiter. ScannerByDelimFlagEofNotAsDelim = 0 // ScannerByDelimFlagIncludeDelimInReturn specifies that the delimiter should be included in the return value. ScannerByDelimFlagIncludeDelimInReturn = 0 )
const ( // ScannerByDelimFlagDefault specifies the most commonly used flags for the scanner. ScannerByDelimFlagDefault = ScannerByDelimFlagEofAsDelim | ScannerByDelimFlagDropDelimInReturn )
Variables ¶
This section is empty.
Functions ¶
func ByteReadLine ¶ added in v0.0.13
ByteReadLine reads in a single line from a bufio.Reader and returns it in []byte. Note the returned []byte may be pointing directly into the bufio.Reader, so assume the returned []byte will be invalidated and shouldn't be used upon next ByteReadLine call.
func FileExists ¶ added in v0.0.4
FileExists checks if a file exists or not.
func NewScannerByDelim ¶
NewScannerByDelim creates a scanner that returns tokens from the source reader separated by a delimiter.
func NewScannerByDelim2 ¶
NewScannerByDelim2 creates a scanner that returns tokens from the source reader separated by a delimiter, with consideration of potential presence of escaping sequence. Note: the token returned from the scanner will **NOT** do any unescaping, thus keeping the original value.
func NewScannerByDelim3 ¶ added in v0.0.5
func NewScannerByDelim3(r io.Reader, delim, escape []byte, flags ScannerByDelimFlag, buf []byte) *bufio.Scanner
NewScannerByDelim3 creates a scanner that utilizes given buf to avoid/minimize allocation and returns tokens from the source reader separated by a delimiter, with consideration of potential presence of escaping sequence. Note: the token returned from the scanner will **NOT** do any unescaping, thus keeping the original value.
Types ¶
type BytesReplacer ¶ added in v0.0.15
type BytesReplacer interface { // GetSizingHints returns hints for BytesReplacingReader to do sizing estimate and allocation. // Return values: // - 1st: max search token len // - 2nd: max replace token len // - 3rd: max (search_len / replace_len) ratio that is less than 1, // if none of the search/replace ratio is less than 1, then return a negative number. // will only be called once during BytesReplacingReader initialization/reset. GetSizingHints() (int, int, float64) // Index does token search for BytesReplacingReader. // Return values: // - 1st: index of the first found search token; -1, if not found; // - 2nd: the found search token; ignored if not found; // - 3rd: the matching replace token; ignored if not found; Index(buf []byte) (int, []byte, []byte) }
BytesReplacer allows customization on how BytesReplacingReader does sizing estimate during initialization/reset and does search and replacement during the execution.
type BytesReplacingReader ¶
type BytesReplacingReader struct {
// contains filtered or unexported fields
}
BytesReplacingReader allows transparent replacement of a given token during read operation.
func NewBytesReplacingReader ¶
func NewBytesReplacingReader(r io.Reader, search, replace []byte) *BytesReplacingReader
NewBytesReplacingReader creates a new `*BytesReplacingReader` for a single pair of search:replace token replacement. `search` cannot be nil/empty. `replace` can.
func NewBytesReplacingReaderEx ¶ added in v0.0.15
func NewBytesReplacingReaderEx(r io.Reader, replacer BytesReplacer) *BytesReplacingReader
NewBytesReplacingReaderEx creates a new `*BytesReplacingReader` for a given BytesReplacer customization.
func (*BytesReplacingReader) Read ¶
func (r *BytesReplacingReader) Read(p []byte) (int, error)
Read implements the `io.Reader` interface.
func (*BytesReplacingReader) Reset ¶
func (r *BytesReplacingReader) Reset(r1 io.Reader, search1, replace1 []byte) *BytesReplacingReader
Reset allows reuse of a previous allocated `*BytesReplacingReader` for buf allocation optimization. `search` cannot be nil/empty. `replace` can.
func (*BytesReplacingReader) ResetEx ¶ added in v0.0.15
func (r *BytesReplacingReader) ResetEx(r1 io.Reader, replacer BytesReplacer) *BytesReplacingReader
ResetEx allows reuse of a previous allocated `*BytesReplacingReader` for buf allocation optimization.
type LineCountingReader ¶
type LineCountingReader struct {
// contains filtered or unexported fields
}
LineCountingReader wraps an io.Reader and reports currently which line the reader is at. Note the LineAt starts a 1.
func NewLineCountingReader ¶
func NewLineCountingReader(r io.Reader) *LineCountingReader
NewLineCountingReader creates new LineCountingReader wrapping around an input io.Reader.
func (*LineCountingReader) AtLine ¶
func (r *LineCountingReader) AtLine() int
AtLine returns the current line number. Note it starts with 1.
type LineEditFunc ¶ added in v0.0.16
LineEditFunc edits a line and returns a resulting line. Note in-place editing is highly encouraged, for performance reasons, when the resulting line is no longer than the original. If your edited line is longer then the original `line`, however, you MUST allocate and return a new []byte. Directly appending at the end of the original `line` will result in undefined behavior.
type LineEditingReader ¶ added in v0.0.16
type LineEditingReader struct {
// contains filtered or unexported fields
}
LineEditingReader implements io.Reader interface with a line editing mechanism. LineEditingReader reads data from underlying io.Reader and invokes the caller supplied edit function for each of the line (defined as []byte ending with '\n', therefore it works on both Mac/Linux and Windows, where '\r\n' is used). Note the last line before EOF will be edited as well even if it doesn't end with '\n'. Usage is highly flexible: the editing function can do in-place editing such as character replacement, prefix/suffix stripping, or word replacement, etc., as long as the line length isn't changed; or it can replace a line with a completely newly allocated and written line with no length restriction (although performance would be slower compared to in-place editing).
func NewLineEditingReader ¶ added in v0.0.16
func NewLineEditingReader(r io.Reader, edit LineEditFunc) *LineEditingReader
NewLineEditingReader creates a new LineEditingReader with the default buffer size.
func NewLineEditingReader2 ¶ added in v0.0.16
func NewLineEditingReader2(r io.Reader, edit LineEditFunc, bufSize int) *LineEditingReader
NewLineEditingReader2 creates a new LineEditingReader with custom buffer size.
type LineNumReportingCsvReader ¶
LineNumReportingCsvReader wraps std lib `*csv.Reader` and exposes the current line number.
func NewLineNumReportingCsvReader ¶
func NewLineNumReportingCsvReader(r io.Reader) *LineNumReportingCsvReader
NewLineNumReportingCsvReader creates a new `*LineNumReportingCsvReader`.
func (*LineNumReportingCsvReader) LineNum ¶
func (r *LineNumReportingCsvReader) LineNum() int
LineNum returns the current line number
type ScannerByDelimFlag ¶
type ScannerByDelimFlag uint
ScannerByDelimFlag is the type of flags passed to NewScannerByDelim/NewScannerByDelim2.