lines

package module
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 6 Imported by: 0

README

Documentation

Overview

Package lines provides line-oriented text processing primitives and a buffered line processor that applies a transform to each line of an input.

The per-line operations are small and pure; Process owns the scanning, context-cancellation, counting, and joining. The package holds no CLI or orchestration logic and is reusable by any domain that needs line processing.

Index

Constants

View Source
const ErrReadInput errs.Const = "failed to read input"

ErrReadInput is returned when reading the input fails. The underlying cause is wrapped via errs.Const.With, so the result matches both ErrReadInput and the cause under errors.Is.

Variables

This section is empty.

Functions

func Contains

func Contains(line Line, filter Filter) bool

Contains reports whether the line contains the filter substring.

func Process

func Process(ctx context.Context, reader io.Reader, transform Transform) (Output, Stats, error)

Process scans reader line by line, applies transform, and joins the kept lines with "\n". It stops early if ctx is cancelled and reports the total and kept line counts.

Process is a buffered line processor, not a streaming one: every kept line is retained in memory and joined into a single Output, so peak memory is O(input).

A trailing newline is not round-tripped: lines are joined with "\n" and no terminator, so "a\nb\nc\n" and "a\nb\nc" both yield "a\nb\nc". CRLF is normalized to LF, because the scanner strips a trailing "\r" from each line.

A single line longer than MaxLine fails with ErrReadInput wrapping bufio.ErrTooLong.

Types

type Filter

type Filter string

Filter is a substring a line must contain to be kept.

type Line

type Line string

Line is a single line of input or output.

func Numbered

func Numbered(line Line, number LineNumber) Line

Numbered returns the line prefixed with its right-aligned line number.

func Uppercase

func Uppercase(line Line) Line

Uppercase returns the line converted to uppercase.

func WithPrefix

func WithPrefix(line Line, prefix Prefix) Line

WithPrefix returns the line with prefix prepended.

type LineBytes

type LineBytes int

LineBytes is a size, in bytes, of a single scanned line.

const MaxLine LineBytes = 1 << 20

MaxLine is the largest single line Process accepts. It raises bufio's default 64 KiB scan ceiling to 1 MiB; a line longer than this makes Process fail with ErrReadInput wrapping bufio.ErrTooLong.

type LineNumber

type LineNumber int

LineNumber is a one-based line position.

type Output

type Output string

Output is the joined result of processing.

type Prefix

type Prefix string

Prefix is text prepended to a line.

type Stats

type Stats struct {
	Total LineNumber
	Kept  LineNumber
}

Stats reports the line counts observed during processing.

type Transform

type Transform func(Line, LineNumber) (Line, bool)

Transform maps a numbered line to its processed form and whether to keep it.

Jump to

Keyboard shortcuts

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