rscanner

package module
v0.0.0-...-4f90454 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 5 Imported by: 3

README

Reverse Scanner

Build Status Go Reference Go Report Card codecov

Have you ever found yourself in a situation where you need to read a file or another readable source from the end to the start? While bufio.Scanner only supports reading from the start, rscanner.Scanner offers the capability to read from the end.

rscanner.Scanner is an exceptionally efficient and thoroughly tested reverse scanner, even in production.

The rscanner package intentionally maintains the same interface as bufio.Scanner for reading, including scanner.Scan(),scanner.Bytes(), and scanner.Text(). However, rscanner introduces some slight differences to offer enhanced configurability, such as scanner.Buffer(), scanner.MaxTokenSize(), and scanner.MaxConsecutiveEmptyReads().

To achieve superior performance, it minimizes memory allocation and reuses the same block of memory for multiple reads.

Install

go get -t github.com/vallerion/rscanner

Usage

Simple
const text = "run\nforrest\nrun"
sc := rscanner.NewScanner(strings.NewReader(text), int64(len(text)))
for sc.Scan() {
    log.Println(sc.Text())
}
if sc.Err() != nil {
    log.Fatalf("woops! err: %v", sc.Err())
}
File
f, err := os.Open("service.log")
if err != nil {
    log.Fatalln(err)
}
defer f.Close()

fs, err := f.Stat()
if err != nil {
    log.Fatalln(err)
}

sc := rscanner.NewScanner(f, fs.Size())
for sc.Scan() {
    log.Println(sc.Text())
}

if sc.Err() != nil {
    log.Fatalf("woops! err: %v", sc.Err())
}

log.Println("File is finished!")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTooLong         = errors.New("rscanner.Scanner: token too long")
	ErrNegativeAdvance = errors.New("rscanner.Scanner: SplitFunc returns negative advance count")
	ErrAdvanceTooFar   = errors.New("rscanner.Scanner: SplitFunc returns advance count beyond input")
	ErrBadReadCount    = errors.New("rscanner.Scanner: Read returned impossible count")
)

Functions

func ScanLines

func ScanLines(data []byte) (advance int, token []byte, err error)

Types

type Scanner

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

func NewScanner

func NewScanner(r io.ReaderAt, readerSize int64) *Scanner

func (*Scanner) Buffer

func (bs *Scanner) Buffer(buf []byte)

func (*Scanner) Bytes

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

func (*Scanner) Err

func (bs *Scanner) Err() error

func (*Scanner) MaxConsecutiveEmptyReads

func (bs *Scanner) MaxConsecutiveEmptyReads(v int)

func (*Scanner) MaxTokenSize

func (bs *Scanner) MaxTokenSize(max int)

func (*Scanner) Scan

func (bs *Scanner) Scan() bool

func (*Scanner) Split

func (bs *Scanner) Split(split SplitFunc)

func (*Scanner) Text

func (bs *Scanner) Text() string

type SplitFunc

type SplitFunc func(data []byte) (advance int, token []byte, err error)

Jump to

Keyboard shortcuts

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