radix

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: AGPL-3.0 Imports: 0 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Radix Circle CI GoDoc Coverage Status

Package glob provides a trie(also known as prefix-tree) that supports wildcard character '*'.

	func TestTree(t *testing.T) {
		patterns := []struct {
			s string
			i interface{}
		}{
			{"*abcd*ef*", 1},
			{"*.google.com", 2},
			{"http://example.com/books/*", 3},
			{"*://example.com/movies", 4},
			{`http://example.com/\*`, 5},
			{`http://example.com/*`, 6},
			{"你好*世界*", 7},
			{`foo\`, 8},
			{`b\ar`, 9},
		}
		data := []struct {
			s string
			v interface{}
		}{
			{"abcdef", 1},
			{"abcdefef", 1},
			{"abcabcdefgef", 1},
			{"google.com", nil},
			{"www.google.com", 2},
			{"http://example.com/books/", 3},
			{"http://example.com/", 6},
			{"http://example.com/*", 5},
			{"你好世界", 7},
			{"你你好世界", nil},
			{"你好世界世界界界", 7},
			{"你好,世界", 7},
			{"你好,世界。", 7},
			{`foo\`, nil},
			{`foo`, 8},
			{`b\ar`, nil},
			{`bar`, 9},
		}

		tr := &Trie{}
		for _, p := range patterns {
			tr.Add(p.s, p.i)
		}

		for _, data := range data {
			v, ok := tr.Lookup(data.s)
			if data.v == nil {
				assert.False(t, ok)
				assert.Nil(t, v)
			} else {
				assert.True(t, ok)
				assert.Equal(t, data.v, v)
			}
		}

	}

Documentation

Overview

Package glob provides a trie(also known as prefix-tree) that supports wildcard *.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(pattern, s string) bool

Match tests whether s matches pattern.

Types

type Pattern

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

func Compile

func Compile(patterns ...string) *Pattern

Compile compiles several alternative patterns into one.

func (*Pattern) Match

func (p *Pattern) Match(s string) bool

Match tests whether s matches any patterns in p.

type PatternTrie

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

Trie stores a value for each pattern.

func NewPatternTrie

func NewPatternTrie() *PatternTrie

NewPatternTrie returns a new pattern trie.

func (*PatternTrie) Add

func (t *PatternTrie) Add(pattern string, v interface{}) (ov interface{}, has bool)

Add inserts pattern into trie. If there is an old value for this pattern, old value will be returned and 'has' is set to true.

func (*PatternTrie) Lookup

func (t *PatternTrie) Lookup(s string) (v interface{}, ok bool)

Lookup searchs pattern matching s most precisely and returns value associated with it. If not found, ok will be set to false.

type Trie

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

func NewTrie

func NewTrie(dump bool) *Trie

NewTrie creates a new trie.

func (*Trie) Add

func (t *Trie) Add(s string, v interface{}) (ov interface{}, has bool)

Add inserts a key-value pair into trie. If there is an old value for the key, old value will be returned and 'has' will be true.

func (*Trie) Lookup

func (t *Trie) Lookup(s string) (v interface{}, ok bool)

Lookup searchs the trie to find an exact match and returns the associated value. If not found, ok will be false.

Jump to

Keyboard shortcuts

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