grep

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Grep

Tool find text at files.

Documentation online

Author

Jonás Melián (https://bitbucket.org/ares)

License

The source files are distributed under the Apache License, version 2.0.

Documentation

Overview

Package grep allows find text at files.

== TODO

Add function to get several lines, from start to end

func Lines(start, end int) (string, error)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Finder

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

Finder represents the file or data where find a string or line.

func New added in v1.1.0

func New(src []byte, comment string, mode ModeFind) (*Finder, error)

New prepares the 'Finder' via the `src` parameter.

func NewFromFile added in v1.1.0

func NewFromFile(filename, comment string, mode ModeFind) (*Finder, error)

New prepares the 'Finder' via the `filename`. Must use `Close()` to close the file.

func (*Finder) Close

func (fi *Finder) Close() error

Close closes the file.

func (*Finder) Contains

func (fi *Finder) Contains(s string, mode ModeFind) (found bool, err error)

Contains reports whether the file contains `s`. If `mod < 0`, uses the mode set at 'Finder'.

The modes allowed are `ModIgnoreCase`, `ModSkipComment` and `ModTrimSpace`.

func (*Finder) Count

func (fi *Finder) Count(s string, mode ModeFind) (n int, err error)

Count returns the number of matching lines. If `mod < 0`, uses the mode set at 'Finder'.

The modes allowed are `ModIgnoreCase` and `ModSkipComment`.

func (*Finder) HasPrefix

func (fi *Finder) HasPrefix(s string, mode ModeFind) (found bool, err error)

HasPrefix reports whether the file has a line that begins with `s`. If `mod < 0`, uses the mode set at 'Finder'.

The modes allowed are `ModIgnoreCase`, `ModSkipComment` and `ModTrimSpace`.

func (*Finder) HasSuffix

func (fi *Finder) HasSuffix(s string, mode ModeFind) (found bool, err error)

HasSuffix reports whether the file has a line that ends with `s`. If `mod < 0`, uses the mode set at 'Finder'.

The modes allowed are `ModIgnoreCase`, `ModSkipComment` and `ModTrimSpace`.

func (*Finder) Line

func (fi *Finder) Line(n int) (string, error)

Line returns the line indicated according to the value of n:

if 'n == 0', returns an empty string
if 'n > 0', finds from start
if 'n < 0', finds from end.

Do not add the character of new line ('\n') at the end of the line returned. If the last line (-1) is '\n', then it returns the anterior one.

func (*Finder) LineMatched

func (fi *Finder) LineMatched(p LinePlace) (string, error)

LineMatched returns the line indicated by `LinePlace`.

Must be called after of functions that find text into a line: `Contains`, `HasPrefix` and `HasSuffix`.

func (*Finder) Pos

func (fi *Finder) Pos() (start, end int64)

Pos returns the position where the string was found.

Must be called after of functions that find text into a line: `Contains`, `HasPrefix` and `HasSuffix`.

type LinePlace

type LinePlace uint8

LinePlace represents the line to return.

const (
	LnCurrent LinePlace = iota // line current
	LnBefore                   // line before
	LnAfter                    // line after
)

type ModeFind

type ModeFind int8

A ModeFind value is a set of flags (or 0) to control behavior at find into a file.

const (
	// Ignore case distinctions.
	ModIgnoreCase ModeFind = 1 << iota
	// Skip skip lines that start with the comment string and remove both spaces
	// and tabulations at the beginning to math the comment string.
	ModSkipComment
	// Removes all leading and trailing white spaces.
	ModTrimSpace
)

Modes used to find into a file.

Jump to

Keyboard shortcuts

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