inc

package module
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 2 Imported by: 1

README

inc

inc is a (headless) incremental text search library.

install

go get -u github.com/tbistr/inc

Usage

For more information, see godoc.

// initialize with the initial query and the target strings
// Strs2Cands converts []string to []inc.Candidate
target := []string{"foobar", "hogehuga", "bazbar"}
e := inc.New("initial query", inc.Strs2Cands(target))

e.DelQuery()             // delete all runes from the query
e.AddQuery('o')          // add 'o' to the query
fmt.Println(e.Matched()) // ["foobar", "hogehuga"]
e.RmQuery()              // remove the last rune from the query
fmt.Println(e.Matched())

a := struct{ Name string }{Name: "some struct A"}
b := struct{ Name string }{Name: "some struct B"}

// you can get some matched object or pointer
e = inc.New("", []inc.Candidate{
    {Ptr: &a, Text: []rune("abc")},
    {Ptr: &b, Text: []rune("def")},
})

// some query operations

for _, c := range e.Matched() {
    // get matched pointer as *struct{ Name string }
    fmt.Println(c.Ptr.(*struct{ Name string }).Name)
}

For the following text,

[]string{"foobar", "hogehuga", "foobaz"}

query "ob" matches "foobar" and "foobaz".
Then add 'r' to the query, it only matches "foobar".
Remove 'r' and add 'z', it matches "foobaz" again.

Like this, incremental search checks

  • if the runes in the query are included in the text;
  • if the order of the included runes is the same as the query.

But doesn't care about substrings between the runes.

This is the explanation of the default algorithm of inc. You can change the algorithm by implementing inc.Algorithm and passing it to inc.NewWithAlgo.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(query string, body string) bool

Match does naive incremental search.

Types

type Algorithm added in v0.3.0

type Algorithm interface {
	// AppendCands appends candidates to the engine.
	AppendCands([]*Candidate)
	// DeleteCands deletes candidates from the engine.
	DeleteCands()

	// GetQuery returns the current query.
	GetQuery() []rune
	// AddQuery adds a rune to the query.
	AddQuery(rune)
	// RmQuery removes a rune from the query.
	RmQuery()
	// DelQuery deletes (clears) the query.
	DelQuery()
}

Algorithm is the interface for the algorithm used by the engine.

Algorithm can be non transitive. So, you can implement a algorithm that calculates with whole query every time.

type Candidate

type Candidate struct {
	// Ptr is a arbitrary pointer.
	// If you want to Engine to return a pointer to some object as a search result, you can use this field.
	Ptr any
	// Text is a string to be searched.
	Text []rune
	// Matched is true if the candidate is matched.
	Matched bool
	// KeyRunes is a list of key runes.
	// It is intended to be used to highlight matched runes.
	//
	// Even if Matched is true, len(KeyRunes) != len(query) may be true.
	// It depends on the algorithm you use.
	// (Some algorithms may not use KeyRunes at all.)
	KeyRunes []KeyRune
}

Candidate is a candidate for incremental search.

func NewCandidate added in v0.3.0

func NewCandidate(text []rune, ptr any) *Candidate

NewCandidate Makes a new Candidate.

func Strs2Cands

func Strs2Cands(ts []string) []Candidate

Strs2Cands converts a list of strings to a list of Candidates.

Ptr of each Candidate will be set to nil for decreasing memory usage.

func (Candidate) String added in v0.3.0

func (c Candidate) String() string

type Engine

type Engine struct {
	Algorithm
	// contains filtered or unexported fields
}

Engine is a engine for incremental search.

func New

func New(query string, cands []Candidate) *Engine

New returns a new Engine.

It uses the default algorithm which is naive incremental search.

func NewWithAlgo added in v0.4.0

func NewWithAlgo(query string, cands []Candidate, algo Algorithm) *Engine

NewWithAlgo returns a new Engine with a custom algorithm.

func (*Engine) AppendCands added in v0.4.0

func (e *Engine) AppendCands(cands []Candidate)

AppendCands appends candidates to the engine.

It receives candidates as values and converts them to pointers. So, you can't modify the candidates after passing them to AppendCands.

func (*Engine) Candidates added in v0.5.0

func (e *Engine) Candidates() []Candidate

Candidates returns all candidates.

func (*Engine) DeleteCands added in v0.6.0

func (e *Engine) DeleteCands()

DeleteCands deletes all candidates from the engine.

func (*Engine) Matched added in v0.3.0

func (e *Engine) Matched() []Candidate

Matched returns matched candidates.

It returns candidates as values, so you can't modify internal states of the engine.

type KeyRune added in v0.4.0

type KeyRune struct {
	// Pos is the index of the first rune of the runes which are matched.
	Pos uint
	// Len is length of the rune starts with target[Pos].
	Len uint
}

KeyRune represents a important rune in a candidate.Text. It is intended to be used to highlight matched runes.

Use like this:

target[Pos:]
target[Pos:Pos+Len]

Former is the substring from the key rune to the end of the target. Latter is one key runes with proper length.

If multibyte (non-ASCII) characters are not used, you may use only Pos.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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