textread

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package textread provides bounded UTF-8 line scanning for filesystem adapters with different consumer result policies.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLimits = errors.New("textread: invalid scan limits")
	ErrInputTooLarge = errors.New("textread: input exceeds its byte limit")
	ErrLineTooLarge  = errors.New("textread: line exceeds its byte limit")
	ErrInvalidText   = errors.New("textread: input is not UTF-8 text")
)

Functions

func LineNumber

func LineNumber(err error) int

LineNumber returns the one-based line attached to a scan failure, or zero when the failure is not line-specific.

func VisitLines

func VisitLines(ctx context.Context, source io.Reader, limits Limits, visit func(number int, line []byte) error) error

VisitLines validates and normalizes a complete bounded input, then calls visit for each one-based line. The line slice is valid only until visit returns. A trailing newline contributes a final empty line, matching Scan's editor-facing line identity.

Types

type Limits

type Limits struct {
	InputBytes int64
	LineBytes  int
}

Limits define one complete UTF-8 line scan without prescribing what a consumer retains. InputBytes includes line separators and a UTF-8 BOM; LineBytes applies after BOM/CRLF normalization.

type Options

type Options struct {
	InputBytes  int64
	LineBytes   int
	OutputBytes int
	StartLine   int
	MaxLines    int
	PartialLine bool
}

Options defines one complete scan. StartLine is zero-based, MaxLines zero means the rest of the file, and PartialLine permits the last selected line to be clipped at a UTF-8 boundary when the output budget is exhausted.

type Result

type Result struct {
	Content         string
	StartLine       int
	EndLine         int
	TotalLines      int
	Truncated       bool
	OutputTruncated bool
}

Result reports normalized LF text and the whole-file line count. StartLine is zero-based and EndLine is exclusive.

func Scan

func Scan(ctx context.Context, source io.Reader, options Options) (Result, error)

Scan validates and counts the complete admitted input while retaining only the selected output window.

Example
package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/Tangerg/scope/tools/textread"
)

func main() {
	result, err := textread.Scan(context.Background(), strings.NewReader("zero\none\ntwo"), textread.Options{
		InputBytes: 64, LineBytes: 16, OutputBytes: 64, StartLine: 1, MaxLines: 1,
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(result.Content, result.TotalLines)
}
Output:
one 3

Jump to

Keyboard shortcuts

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