Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

ahocorasick.gno

// Package ahocorasick implements Aho-Corasick multi-pattern search as a pure,
// reusable package.
//
// Searching for k patterns by running a single-pattern scan k times costs
// O(k*n): every extra word in the dictionary is another full pass over the
// text. Aho-Corasick builds one automaton from the whole set, a trie of the
// patterns plus a failure link on every node pointing at the longest proper
// suffix that is also a prefix of some pattern, and then reads the text once.
// Building the trie and the failure links costs O(total pattern length) and
// scanning costs O(n + matches), whatever k is, and the text cursor never moves
// backwards.
//
// One deliberate departure from the paper: each node stores the full list of
// patterns matching there, merged from its failure target at build time, rather
// than a link to be walked at every offset. That makes a reported match O(1)
// instead of a walk up the failure chain, and it costs build memory in exactly
// one shape, a set whose patterns nest ("a", "aa", "aaa", ...), where the
// merged lists are quadratic in k rather than linear. MaxPatterns is what
// bounds that, and nothing else does.
//
// That last property is why it is worth having on chain: the worst case equals
// the best case, so a dictionary and a text chosen by an attacker cost the same
// as a friendly pair. A naive k-pass scan is an input away from quadratic.
//
// It is [p/moul/x/daily/kmp](/p/moul/x/daily/kmp/v0) generalised: KMP's failure
// table is this automaton for a one-pattern set.
//
// Operates on BYTES, not runes. gno strings are UTF-8 and a match offset is a
// byte offset, which is the right unit for slicing. UTF-8 is self-synchronizing,
// so a pattern that is itself valid UTF-8 can only match on a rune boundary of
// valid UTF-8 text; a pattern that is a bare continuation byte can and will
// match inside a rune, and slicing on that offset splits it.
//
// A live demo of this package is at
// [r/moul/x/daily/ahocorasickdemo](/r/moul/x/daily/ahocorasickdemo/v0).
package ahocorasick

// Bounds on what New will build, so an on-chain caller cannot be handed an
// automaton whose size it did not choose. At most MaxPatterns*MaxPatternLen+1
// nodes exist, and the build is linear in the total pattern length.
const (
	MaxPatterns   = 256
	MaxPatternLen = 256
)

// Match is one occurrence of one pattern. Start and End are byte offsets into
// the text, half-open, so text[Start:End] is the matched run. Index is the
// pattern's slot in the slice given to New, which is how a caller maps a match
// back to whatever it stored alongside the pattern.
type Match struct {
	Start   int
	End     int
	Index   int
	Pattern string
}

// edge is one labelled transition. A node keeps its edges sorted by label so a
// lookup is a binary search over the alphabet actually used, not a 256-entry
// row per node: a dictionary of words touches a few dozen distinct bytes and
// paying 1 KiB a node for the rest is how this structure gets expensive.
type edge struct {
	label byte
	next  int
}

type node struct {
	edges []edge
	fail  int
	out   []int // pattern slots matching here, longest first
}

// Matcher is a built automaton. It is immutable once New returns, so one
// Matcher can serve any number of scans.
type Matcher struct {
	patterns []string
	nodes    []node
}

// New builds the automaton for patterns. Empty patterns are dropped: the empty
// string matches at every offset, which is a different question from the one
// this package answers.
//
// Returns nil when there is nothing to build (no pattern survives the drop) or
// when the set exceeds the bounds above. A nil Matcher answers every method,
// so a caller may hand the result straight to a scan without a check.
func New(patterns []string) *Matcher {
	accepted := make([]string, 0, len(patterns))
	for _, p := range patterns {
		if p == "" {
			continue
		}
		if len(p) > MaxPatternLen {
			return nil
		}
		accepted = append(accepted, p)
	}
	if len(accepted) == 0 || len(accepted) > MaxPatterns {
		return nil
	}

	m := &Matcher{patterns: accepted, nodes: []node{{fail: 0}}}
	for i, p := range accepted {
		cur := 0
		for j := 0; j < len(p); j++ {
			next, ok := m.child(cur, p[j])
			if !ok {
				m.nodes = append(m.nodes, node{fail: 0})
				next = len(m.nodes) - 1
				m.addEdge(cur, p[j], next)
			}
			cur = next
		}
		// Same depth, so same length: ordering by slot keeps a duplicated
		// pattern reporting in the caller's order.
		m.nodes[cur].out = append(m.nodes[cur].out, i)
	}
	m.link()
	return m
}

// link computes every failure link, then merges each node's outputs with its
// failure target's. The merge is what makes a scan O(1) per reported match
// instead of a walk up the failure chain at every offset, and the breadth-first
// order is what makes it correct: a node's failure target is always shallower,
// so it has already been merged.
//
// It is also where the memory goes: a nested set makes the merged lists
// quadratic in the number of patterns. See the package doc.
func (m *Matcher) link() {
	queue := make([]int, 0, len(m.nodes))
	for _, e := range m.nodes[0].edges {
		m.nodes[e.next].fail = 0
		queue = append(queue, e.next)
	}
	for len(queue) > 0 {
		u := queue[0]
		queue = queue[1:]
		for _, e := range m.nodes[u].edges {
			v := e.next
			f := m.nodes[u].fail
			for {
				if w, ok := m.child(f, e.label); ok {
					m.nodes[v].fail = w
					break
				}
				if f == 0 {
					m.nodes[v].fail = 0
					break
				}
				f = m.nodes[f].fail
			}
			inherited := m.nodes[m.nodes[v].fail].out
			if len(inherited) > 0 {
				merged := make([]int, 0, len(m.nodes[v].out)+len(inherited))
				merged = append(merged, m.nodes[v].out...)
				merged = append(merged, inherited...)
				m.nodes[v].out = merged
			}
			queue = append(queue, v)
		}
	}
}

// child returns the node reached from n by label, if there is one.
func (m *Matcher) child(n int, label byte) (int, bool) {
	edges := m.nodes[n].edges
	lo, hi := 0, len(edges)
	for lo < hi {
		mid := (lo + hi) / 2
		if edges[mid].label < label {
			lo = mid + 1
		} else {
			hi = mid
		}
	}
	if lo < len(edges) && edges[lo].label == label {
		return edges[lo].next, true
	}
	return 0, false
}

// addEdge inserts a transition, keeping the edge list sorted by label.
func (m *Matcher) addEdge(n int, label byte, next int) {
	edges := m.nodes[n].edges
	pos := len(edges)
	for i, e := range edges {
		if e.label > label {
			pos = i
			break
		}
	}
	edges = append(edges, edge{})
	copy(edges[pos+1:], edges[pos:])
	edges[pos] = edge{label: label, next: next}
	m.nodes[n].edges = edges
}

// step advances the automaton one byte, following failure links until the byte
// is consumed or the root refuses it. This is the whole scan: the text index
// only ever moves forward.
func (m *Matcher) step(cur int, c byte) int {
	for {
		if w, ok := m.child(cur, c); ok {
			return w
		}
		if cur == 0 {
			return 0
		}
		cur = m.nodes[cur].fail
	}
}

// FindAll returns every occurrence of every pattern, including overlapping and
// nested ones, ordered by end offset. At one end offset the longest match comes
// first, because the scan reports the node it reached before the shorter
// suffixes hanging off its failure chain, so a caller taking the first match at
// each offset gets the greedy one.
func (m *Matcher) FindAll(text string) []Match {
	if m == nil {
		return nil
	}
	var out []Match
	cur := 0
	for i := 0; i < len(text); i++ {
		cur = m.step(cur, text[i])
		for _, slot := range m.nodes[cur].out {
			p := m.patterns[slot]
			out = append(out, Match{Start: i + 1 - len(p), End: i + 1, Index: slot, Pattern: p})
		}
	}
	return out
}

// FindFirst returns the first match FindAll would return, and stops the scan
// there. The one to reach for when the question is "does this text contain
// anything from the list, and where".
func (m *Matcher) FindFirst(text string) (Match, bool) {
	if m == nil {
		return Match{}, false
	}
	cur := 0
	for i := 0; i < len(text); i++ {
		cur = m.step(cur, text[i])
		if out := m.nodes[cur].out; len(out) > 0 {
			p := m.patterns[out[0]]
			return Match{Start: i + 1 - len(p), End: i + 1, Index: out[0], Pattern: p}, true
		}
	}
	return Match{}, false
}

// Contains reports whether any pattern occurs in text.
func (m *Matcher) Contains(text string) bool {
	_, ok := m.FindFirst(text)
	return ok
}

// Patterns returns the accepted patterns, in the caller's order, as a copy.
func (m *Matcher) Patterns() []string {
	if m == nil {
		return nil
	}
	out := make([]string, len(m.patterns))
	copy(out, m.patterns)
	return out
}

// Nodes returns the size of the automaton: one node per distinct pattern
// prefix, plus the root. The number a caller sizing a dictionary wants, since
// it is what the build actually costs.
func (m *Matcher) Nodes() int {
	if m == nil {
		return 0
	}
	return len(m.nodes)
}

ahocorasick_test.gno

package ahocorasick

import (
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// fmtMatches renders a match slice as "start:end:pattern" joined by spaces, so
// a failing case prints the whole scan instead of the first difference.
func fmtMatches(ms []Match) string {
	parts := make([]string, 0, len(ms))
	for _, m := range ms {
		parts = append(parts, itoa(m.Start)+":"+itoa(m.End)+":"+m.Pattern)
	}
	return strings.Join(parts, " ")
}

func itoa(n int) string {
	if n == 0 {
		return "0"
	}
	var b []byte
	for n > 0 {
		b = append([]byte{byte('0' + n%10)}, b...)
		n /= 10
	}
	return string(b)
}

func TestFindAll(t *testing.T) {
	cases := []struct {
		name     string
		patterns []string
		text     string
		want     string
	}{
		{
			// The textbook automaton from the 1975 paper.
			name:     "ushers",
			patterns: []string{"he", "she", "his", "hers"},
			text:     "ushers",
			want:     "1:4:she 2:4:he 2:6:hers",
		},
		{
			// Every suffix of the longest pattern is also a pattern, so one
			// position must report four matches through the output links.
			name:     "nested suffixes",
			patterns: []string{"abcd", "bcd", "cd", "d"},
			text:     "abcd",
			want:     "0:4:abcd 1:4:bcd 2:4:cd 3:4:d",
		},
		{
			name:     "overlapping",
			patterns: []string{"a", "ab", "bab", "bc", "bca", "c", "caa"},
			text:     "abccab",
			want:     "0:1:a 0:2:ab 1:3:bc 2:3:c 3:4:c 4:5:a 4:6:ab",
		},
		{
			name:     "no match",
			patterns: []string{"xyz", "qq"},
			text:     "the quick brown fox",
			want:     "",
		},
		{
			name:     "empty text",
			patterns: []string{"a"},
			text:     "",
			want:     "",
		},
		{
			// A pattern longer than the text can never match, and must not
			// walk the automaton off its end trying.
			name:     "pattern longer than text",
			patterns: []string{"abcdef"},
			text:     "abc",
			want:     "",
		},
		{
			name:     "repeats",
			patterns: []string{"aa"},
			text:     "aaaa",
			want:     "0:2:aa 1:3:aa 2:4:aa",
		},
		{
			// The empty pattern matches everywhere and nowhere; it is dropped
			// rather than reported at every offset.
			name:     "empty pattern dropped",
			patterns: []string{"", "b"},
			text:     "abc",
			want:     "1:2:b",
		},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := fmtMatches(New(tc.patterns).FindAll(tc.text))
			uassert.Equal(t, tc.want, got)
		})
	}
}

// At one end offset the longest match comes first, because the scan reports
// the node it reached before walking the output links to its shorter
// suffixes. A caller taking the first match per offset gets the greedy one.
func TestFindAllOrdersLongestFirstAtAnEndOffset(t *testing.T) {
	got := New([]string{"he", "she"}).FindAll("she")
	uassert.Equal(t, "0:3:she 1:3:he", fmtMatches(got))
}

// The index is the caller's slot in the pattern slice, not the automaton's
// node number: that is what lets a caller map a match back to whatever it
// stored alongside the pattern.
func TestMatchIndexIsTheCallersSlot(t *testing.T) {
	ms := New([]string{"zz", "he", "qq"}).FindAll("he")
	uassert.Equal(t, 1, len(ms))
	uassert.Equal(t, 1, ms[0].Index)
	uassert.Equal(t, "he", ms[0].Pattern)
}

// A duplicated pattern is two slots, so it reports twice. Deduplicating here
// would silently drop the second caller's payload.
func TestDuplicatePatternsReportOnceperSlot(t *testing.T) {
	ms := New([]string{"ab", "ab"}).FindAll("ab")
	uassert.Equal(t, 2, len(ms))
	uassert.Equal(t, 0, ms[0].Index)
	uassert.Equal(t, 1, ms[1].Index)
}

func TestFindFirst(t *testing.T) {
	m, ok := New([]string{"brown", "fox"}).FindFirst("the quick brown fox")
	uassert.True(t, ok)
	uassert.Equal(t, "brown", m.Pattern)
	uassert.Equal(t, 10, m.Start)

	_, ok = New([]string{"cat"}).FindFirst("the quick brown fox")
	uassert.False(t, ok)
}

// FindFirst stops at the first match, so it must agree with FindAll's head
// rather than with some other traversal order.
func TestFindFirstAgreesWithFindAll(t *testing.T) {
	m := New([]string{"he", "she", "hers"})
	all := m.FindAll("ushers")
	first, ok := m.FindFirst("ushers")
	uassert.True(t, ok)
	uassert.Equal(t, all[0].Start, first.Start)
	uassert.Equal(t, all[0].Pattern, first.Pattern)
}

func TestContains(t *testing.T) {
	m := New([]string{"spam", "eggs"})
	uassert.True(t, m.Contains("green eggs and ham"))
	uassert.False(t, m.Contains("green ham"))
}

// Matching is over BYTES. UTF-8 is self-synchronizing, so a pattern that is
// itself valid UTF-8 can only match on a rune boundary of valid UTF-8 text.
func TestValidUTF8PatternMatchesOnARuneBoundary(t *testing.T) {
	ms := New([]string{"é"}).FindAll("café")
	uassert.Equal(t, "3:5:é", fmtMatches(ms))
}

// The flip side of the same fact, and the reason the doc says bytes: a
// pattern that is a bare continuation byte DOES match inside a rune. A caller
// slicing on that offset splits the rune.
func TestContinuationBytePatternMatchesInsideARune(t *testing.T) {
	ms := New([]string{"\xa9"}).FindAll("café")
	uassert.Equal(t, 1, len(ms))
	uassert.Equal(t, 4, ms[0].Start)
}

// Refusals. The bounds exist so an on-chain caller cannot be handed an
// unbounded build; a refusal is nil, and nil still answers every query.
func TestNewRefusesWhatItCannotBound(t *testing.T) {
	uassert.True(t, New(nil) == nil)
	uassert.True(t, New([]string{}) == nil)
	uassert.True(t, New([]string{"", ""}) == nil)

	tooMany := make([]string, MaxPatterns+1)
	for i := range tooMany {
		tooMany[i] = "p" + itoa(i)
	}
	uassert.True(t, New(tooMany) == nil)

	uassert.True(t, New([]string{strings.Repeat("a", MaxPatternLen+1)}) == nil)
	uassert.True(t, New([]string{strings.Repeat("a", MaxPatternLen)}) != nil)
}

// A nil matcher is what a refused build returns, so every method has to
// survive it: a demo that renders whatever New gave it must not panic.
func TestNilMatcherAnswersEverything(t *testing.T) {
	var m *Matcher
	uassert.Equal(t, 0, len(m.FindAll("anything")))
	uassert.False(t, m.Contains("anything"))
	_, ok := m.FindFirst("anything")
	uassert.False(t, ok)
	uassert.Equal(t, 0, len(m.Patterns()))
	uassert.Equal(t, 0, m.Nodes())
}

// Patterns returns the accepted set, empties dropped, in the caller's order,
// and the returned slice is a copy: mutating it must not corrupt the
// automaton's own view of what it matched.
func TestPatternsIsACopyOfTheAcceptedSet(t *testing.T) {
	m := New([]string{"he", "", "she"})
	got := m.Patterns()
	uassert.Equal(t, "he she", strings.Join(got, " "))
	got[0] = "clobbered"
	uassert.Equal(t, "he she", strings.Join(m.Patterns(), " "))
}

// The automaton is one node per distinct pattern prefix plus the root, which
// is what makes the build linear in the total pattern length rather than in
// the product of the set.
func TestNodesCountsDistinctPrefixesPlusTheRoot(t *testing.T) {
	// he, she, his, hers: prefixes h, he, her, hers, s, sh, she, hi, his = 9.
	uassert.Equal(t, 10, New([]string{"he", "she", "his", "hers"}).Nodes())
	// A single pattern is its own prefix chain.
	uassert.Equal(t, 4, New([]string{"abc"}).Nodes())
}

// The guarantee worth having over a naive scan: the text cursor never moves
// backwards, so a pathological set cannot be made to cost more than one pass.
// 64 patterns that all share a prefix, over a text of the same letter.
func TestPathologicalSetStaysOnePass(t *testing.T) {
	patterns := make([]string, 0, 64)
	for i := 1; i <= 64; i++ {
		patterns = append(patterns, strings.Repeat("a", i))
	}
	ms := New(patterns).FindAll(strings.Repeat("a", 64))
	// Every (start, length) pair with start+length <= 64: 64*65/2.
	uassert.Equal(t, 2080, len(ms))
}

// The merged output lists are what the package doc calls its one departure
// from the paper, so the shape that costs the most is pinned rather than
// asserted in prose. A nested set of k patterns stores 1+2+...+k entries,
// quadratic in k, where a dictionary of distinct words stores one per node.
func TestMergedOutputsAreQuadraticOnANestedSet(t *testing.T) {
	nested := make([]string, 0, 64)
	for i := 1; i <= 64; i++ {
		nested = append(nested, strings.Repeat("a", i))
	}
	uassert.Equal(t, 2080, totalOutputs(New(nested)))

	// Four distinct words, no nesting: one entry per terminal node.
	uassert.Equal(t, 4, totalOutputs(New([]string{"fox", "lazy", "over", "quick"})))

	// he/she/his/hers nests once, "he" inside "she", so one node carries two.
	uassert.Equal(t, 5, totalOutputs(New([]string{"he", "she", "his", "hers"})))
}

func totalOutputs(m *Matcher) int {
	n := 0
	for _, nd := range m.nodes {
		n += len(nd.out)
	}
	return n
}

gnomod.toml

module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/ahocorasick/v0"
gno = "0.9"

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 751591
  max_deposit = "9000000ugnot"