search

package module
v1.0.2 Latest Latest
Warning

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

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

README

Package search searches plain-text data sets for lines.

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 search searches plain-text data sets for lines.

During tests, can be used the next tags:

-tags=debug

	Show data used at case of failure.

== 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

func CountLines added in v1.0.2

func CountLines(filename string, src []byte) (n int, err error)

CountLines returns the number of total lines.

If 'filename == ""', uses 'src'. It returns an error, if any.

Types

type ModeSearch

type ModeSearch int8

A ModeSearch value is a set of flags (or 0) to control behavior at searching.

const (
	// Ignore case distinctions.
	ModIgnoreCase ModeSearch = 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 search.

type Searcher

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

Searcher represents the file or data where search plain-text.

func NewSearcher

func NewSearcher(filename string, src []byte, comment string, mode ModeSearch,
) (se *Searcher, err error)

NewSearcher prepares the 'Searcher' for a file at 'filename' or data at 'src'. If 'filename == ""', uses 'src'.

Must use 'Close()' to close the file.

func (*Searcher) Close

func (se *Searcher) Close() error

Close closes the file.

func (*Searcher) Contains

func (se *Searcher) Contains(s string, mode ModeSearch) (found bool, err error)

Contains reports whether the file contains 's'.

If 'mod < 0', uses the mode set at 'Searcher'. The modes allowed are 'ModIgnoreCase', 'ModSkipComment' and 'ModTrimSpace'.

func (*Searcher) Count

func (se *Searcher) Count(s string, mode ModeSearch) (n int, err error)

Count returns the number of matching lines.

The modes allowed are 'ModIgnoreCase' and 'ModSkipComment'. If 'mode < 0', uses the mode set at 'Searcher'.

func (*Searcher) CountLines added in v1.0.2

func (se *Searcher) CountLines() (n int, err error)

CountLines returns the number of total lines.

func (*Searcher) HasPrefix

func (se *Searcher) HasPrefix(s string, mode ModeSearch) (found bool, err error)

HasPrefix reports whether the file has a line that begins with 's'.

If 'mod < 0', uses the mode set at 'Searcher'. The modes allowed are 'ModIgnoreCase', 'ModSkipComment' and 'ModTrimSpace'.

func (*Searcher) HasSuffix

func (se *Searcher) HasSuffix(s string, mode ModeSearch) (found bool, err error)

HasSuffix reports whether the file has a line that ends with 's'.

If 'mod < 0', uses the mode set at 'Searcher'. The modes allowed are 'ModIgnoreCase', 'ModSkipComment' and 'ModTrimSpace'.

func (*Searcher) Line

func (se *Searcher) 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', searchs from start
if 'n < 0', searchs 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 (*Searcher) LineMatched

func (se *Searcher) LineMatched(w WhichLine) (string, error)

LineMatched returns the line indicated by 'WhichLine'.

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

func (*Searcher) Pos

func (se *Searcher) Pos() (start, end int64)

Pos returns the position where the string was found.

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

type WhichLine

type WhichLine uint8

WhichLine represents the line to return.

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

Jump to

Keyboard shortcuts

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