search

package
v0.0.0-...-7447bd4 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeData

func DecodeData(data []byte, encName string) ([]byte, bool, error)

DecodeData transcode raw file data to UTF-8 based on the configured encoding name. If encoding is empty or "auto", it sniffs for UTF-16LE, UTF-16BE, or UTF-8 BOMs. Returns the decoded data, a boolean indicating whether data was modified/copied, and any error.

func LookupEncoding

func LookupEncoding(name string) (encoding.Encoding, error)

LookupEncoding resolves an encoding name to a supported encoding.Encoding. Case-insensitive, supporting common aliases (e.g. "utf-16le", "latin1", "windows-1252"). Special names: - "" or "auto": returns (nil, nil) indicating automatic BOM sniffing. - "none", "binary", "raw": returns (encoding.Nop, nil) indicating raw byte scanning without BOM sniffing.

Types

type Config

type Config struct {
	// MaxCount limits matches per file. 0 means unlimited.
	MaxCount int
	// Before is the number of context lines before each match.
	Before int
	// After is the number of context lines after each match.
	After int
	// SearchBinary includes binary files in results.
	SearchBinary bool
	// OnlyBinary restricts results to binary files only.
	OnlyBinary bool
	// MmapThreshold is the minimum file size in bytes that triggers mmap
	// instead of a full read. 0 defaults to 128KB.
	MmapThreshold int64
	// Multiline enables matching across line boundaries.
	Multiline bool
	// OnlyMatching returns only the matched parts of a line.
	OnlyMatching bool
	// Replace is the replacement template.
	Replace string
	// Encoding specifies the text encoding (e.g. "auto", "utf-16le", "latin1", "none").
	// Empty string or "auto" enables automatic UTF-16/UTF-8 BOM sniffing.
	Encoding string
}

Config holds search behavior options.

type Entry

type Entry struct {
	// Kind is either EntryMatch or EntryContext.
	Kind EntryKind
	// Line is the 1-based line number.
	Line int
	// LineBytes is the full line content.
	LineBytes []byte
	// Column is the 1-based byte offset of the match start.
	// 0 for context lines.
	Column int
}

Entry is an ordered output line — either a match or context.

type EntryKind

type EntryKind int

EntryKind distinguishes match lines from context lines.

const (
	// EntryMatch is a line containing a match.
	EntryMatch EntryKind = iota
	// EntryContext is a context line (before or after a match).
	EntryContext
)

type Match

type Match struct {
	// Line is the 1-based line number.
	Line int
	// Column is the 1-based byte offset of the match start.
	Column int
	// LineBytes is the full line content.
	LineBytes []byte
	// Submatches holds [start, end) byte offsets for each submatch.
	Submatches [][2]int
	// ReplaceBytes holds the expanded replacement if requested.
	ReplaceBytes []byte
}

Match represents a single match within a line.

type Result

type Result struct {
	// Path is the file path.
	Path string
	// Matches is the list of matches found in the file.
	Matches []Match
	// Entries is the ordered list of match and context entries.
	// Only populated if context lines are requested.
	Entries []Entry
	// Binary is true if the file was detected as binary.
	Binary bool
	// ModTime is the file's last modification time.
	ModTime time.Time
	// CreatedAt is the file's birth time (creation time).
	CreatedAt time.Time
	// AccessedAt is the file's last access time.
	AccessedAt time.Time
	// Error is any error encountered while searching this file.
	Error error
}

Result holds all matches for a single file.

func (*Result) Release

func (r *Result) Release()

Release returns pooled resources to the searcher.

type Searcher

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

Searcher scans files for matches against a compiled pattern.

func NewSearcher

func NewSearcher(fsys fs.FS, cfg Config, matcher pattern.Matcher) *Searcher

NewSearcher creates a searcher with the given config and matcher. If fsys is nil, it defaults to the local OS filesystem.

func (*Searcher) Search

func (s *Searcher) Search(ref fsref.Ref) (Result, error)

Search reads a file via the provided fsref.Ref and returns all matches. When context lines are requested, a ring buffer retains only the last <before> lines, avoiding O(N) memory for large files with few matches.

func (*Searcher) SearchBytes

func (s *Searcher) SearchBytes(data []byte, path string) (Result, error)

SearchBytes scans an in-memory byte slice for matches without accessing any filesystem.

func (*Searcher) SearchPath

func (s *Searcher) SearchPath(path string, info fs.FileInfo) (Result, error)

SearchPath is a compatibility shim that creates a pathRef and calls Search.

func (*Searcher) SearchReader

func (s *Searcher) SearchReader(r io.Reader, path string) (Result, error)

SearchReader reads all data from r and scans it for matches.

Jump to

Keyboard shortcuts

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