reverse

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2021 License: MIT Imports: 3 Imported by: 0

README

reverse

A reverse (LIFO) line scanner to process a source line-by-line from tail-to-head that will accept any souce that can support an io.ReaderAt interface (eg. bytes.Buffer, io.File, etc)

This reverse.Scanner operates similarly to a bufio.Scanner, however since this reads from tail-to-head you MUST tell it the size (or start position) for the source for proper retrograde line seek operation.


	scanner := reverse.NewScanner(r, size, nil)
	for scanner.Scan() {
		log.Println(scanner.Text(), scanner.Bytes(), scanner.Err())
    }
    
    // 2021/01/08 21:58:08 0 line-2 [108 105 110 101 45 50] <nil>
    // 2021/01/08 21:58:08 1 line-1 [108 105 110 101 45 49] <nil>
    // 2021/01/08 21:58:08 2 line-0 [108 105 110 101 45 48] EOF

Considerations

  1. Passing a size of zero or less to reverse.NewScanner will result in a nil *Scanner being returned.

  2. When nil is passed for opt the default ChunkSize, BufferSize, and IgnoreEmptyLine settsion will be used to configure reverse.NewScanner

  • See test suite for sample use case examples.

Documentation

Index

Constants

View Source
const (
	// DefaultChunkSize is the default value for the option ChunkSize
	DefaultChunkSize = 1024 // 1K

	// DefaultBufferSize is the default value for the option BufferSize which
	// also limits the max line size that can be processed before ErrOverflow
	DefaultBufferSize = 1 << 20 // 1MB

	// DefaultIgnoreEmptyLine is the default behavior for option IgnoreEmptyLine
	DefaultIgnoreEmptyLine = true
)

Variables

View Source
var (
	// ErrOverflow reports that the line is longer than the internal read buffer
	// and allows the error state condition Err() to report it was not io.EOF
	ErrOverflow = errors.New("line overflow")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	// ChunkSize configures the size of the byte chunk that is read from the input
	ChunkSize int

	// BufferSize configures the maximum byte size limit of the internal buffer
	// Note: This also indirectly limits the max line size
	BufferSize int

	// IgnoreEmptyLine will skip returning empty line content until io.EOF
	IgnoreEmptyLine bool
}

Options configures scanner parameters

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner is a reverse (LIFO) tail-to-head line scanner

func NewScanner

func NewScanner(r io.ReaderAt, size int, opt *Options) *Scanner

NewScanner returns a new reverse (LIFO) tail-to-head line Scanner, because the scanner reads retrograde lines (tail-to-head toward toward zero) the size parameter must be non-zero or a nil Scanner will be returned, nil for Options applies default values

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the byte slice form of the current line read from data source

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the most recent Scan() error state encountered (if any)

func (*Scanner) IsEmpty

func (s *Scanner) IsEmpty() bool

IsEmpty returns a boolean reporting if the current line is empty

func (*Scanner) Len

func (s *Scanner) Len() int

Len returns the len of the current line waiting in the extraction buffer

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan reads the next line until the head is reached which signals termination by returning false with an io.EOF (or reverse.ErrOverflow) for any error state, and once an error state is encountered it continues to be returned until reset

func (*Scanner) Text

func (s *Scanner) Text() string

Text returns the string form of the current line read from data source

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL