linebuf

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 4 Imported by: 0

README

linebuf

A fast, bounded-memory line scanner for Go.

Features

  • Callback and Go 1.23 iterator APIs
  • Configurable initial and maximum buffer sizes
  • trimming for Windows-style line endings
  • Memory-bounded long-line handling via ErrTokenTooLong
  • Race-tested

Installation

go get github.com/monitoring-forge/linebuf

Usage

Callback API
package main

import (
    "os"

    "github.com/monitoring-forge/linebuf"
)

func main() {
    err := linebuf.Scan(os.Stdin, func(line []byte) error {
        // process line
        return nil
    })
    if err != nil {
        // handle error
    }
}
Iterator API
scanner := linebuf.New()
for line := range scanner.Iter(r) {
    // process line
}
if err := scanner.IterErr(); err != nil {
    // handle error
}
Options
scanner := linebuf.New(
    linebuf.WithStartBufSize(4096), // initial buffer size
    linebuf.WithMaxBufSize(65536),  // maximum buffer size
)

If a line exceeds MaxBufSize, scanning stops with linebuf.ErrTokenTooLong.

License

See LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTokenTooLong = errors.New("reader: token too long")

ErrTokenTooLong is returned when a line exceeds the maximum allowed buffer size.

Functions

func Scan

func Scan(r io.Reader, cb CB, opts ...Option) error

Scan reads from the provided reader and invokes the callback for each line.

Types

type CB

type CB func([]byte) error

type Option

type Option func(*Scanner)

func WithMaxBufSize

func WithMaxBufSize(size int) Option

WithMaxBufSize sets the maximum buffer size for the scanner.

func WithStartBufSize

func WithStartBufSize(size int) Option

WithStartBufSize sets the initial buffer size for the scanner.

type Scanner

type Scanner struct {
	StartBufSize int
	MaxBufSize   int
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Scanner

New creates a new Scanner instance with the provided options.

func (*Scanner) Iter

func (s *Scanner) Iter(r io.Reader) iter.Seq[[]byte]

Iter returns a sequence of lines from the provided reader. Any error encountered during iteration can be retrieved using IterErr.

func (*Scanner) IterErr

func (s *Scanner) IterErr() error

IterErr returns the error encountered during iteration, if any. It should be called after Iter to check for errors.

func (*Scanner) Scan

func (s *Scanner) Scan(r io.Reader, cb CB) error

Scan reads from the provided reader and invokes the callback for each line. It returns an error if any occurs during scanning.

Jump to

Keyboard shortcuts

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