Playground
ahocorasickdemo.gno
// Package ahocorasickdemo is a small gnoweb demo of the
// [p/moul/x/daily/ahocorasick](/p/moul/x/daily/ahocorasick/v0) library: it runs
// one automaton over a text once and shows the overlapping matches a k-pass
// scan would have to pay k passes for.
//
// It contains no search logic of its own. Stateless, so Render is
// deterministic.
package ahocorasickdemo
import (
"strconv"
"strings"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/ahocorasick/v0"
)
// Fixed inputs, never chain state: a demo whose output moves with the block
// height cannot be pinned by an example test.
var (
textbookPatterns = []string{"he", "she", "his", "hers"}
textbookText = "ushers"
dictPatterns = []string{"the", "he", "fox", "over", "lazy"}
sentence = "the quick brown fox jumps over the lazy dog"
textbook = ahocorasick.New(textbookPatterns)
dict = ahocorasick.New(dictPatterns)
)
// Render renders the demo for gnoweb.
//
// Render("") / Render("/") -> the textbook automaton and the sentence scan
// Render("/<text>") -> scan that text with the same dictionary
func Render(path string) string {
var b strings.Builder
b.WriteString("# Aho-Corasick\n\n")
b.WriteString("One automaton for the whole dictionary, one pass over the text, ")
b.WriteString("demoing the [`p/moul/x/daily/ahocorasick`](/p/moul/x/daily/ahocorasick/v0) library.\n\n")
if arg := parseArg(path); arg != "" {
b.WriteString(renderScan(arg))
return b.String()
}
b.WriteString("## The 1975 example\n\n")
b.WriteString("Patterns " + list(textbookPatterns) + " over " + md.InlineCode(textbookText) + ". ")
b.WriteString("Three of the four overlap, and `hers` ends where nothing else starts.\n\n")
b.WriteString(matchTable(textbook, textbookText))
b.WriteString("\n## One pass over a sentence\n\n")
b.WriteString("Patterns " + list(dictPatterns) + " over " + md.InlineCode(sentence) + ".\n\n")
b.WriteString(matchTable(dict, sentence))
b.WriteString("\n")
b.WriteString(stats(dict, sentence))
b.WriteString("\n> Scan your own text: append it to the path, for instance ")
b.WriteString("[`/thermos`](/r/moul/x/daily/ahocorasickdemo/v0:/thermos), ")
b.WriteString("where `the` and `he` overlap on the same three letters.\n")
return b.String()
}
// renderScan scans the visitor's text with the sentence dictionary.
//
// The argument is the visitor's text and it is shown inside a code span, so it
// goes through md.InlineCode and not ui.Cell: a cell escaper backslashes a
// backtick, and a backslash does not close a backtick inside a span. Only a
// fence wider than anything in the argument does, which is what md.InlineCode
// picks. The table itself only ever holds offsets and dictionary words, never
// a slice of the argument.
func renderScan(arg string) string {
var b strings.Builder
b.WriteString("## ")
b.WriteString(md.InlineCode(arg))
b.WriteString("\n\nPatterns " + list(dictPatterns) + ".\n\n")
ms := dict.FindAll(arg)
if len(ms) == 0 {
b.WriteString("No pattern occurs in it.\n")
return b.String()
}
b.WriteString(matchTable(dict, arg))
b.WriteString("\n")
b.WriteString(stats(dict, arg))
return b.String()
}
// matchTable renders every match as offsets plus the pattern that matched.
// It never echoes a slice of the scanned text: the pattern is already the
// matched bytes, and it comes from the fixed dictionary rather than from the
// caller.
func matchTable(m *ahocorasick.Matcher, text string) string {
ms := m.FindAll(text)
t := ui.NewTable("start", "end", "pattern")
for _, match := range ms {
t.Row(
"`"+strconv.Itoa(match.Start)+"`",
"`"+strconv.Itoa(match.End)+"`",
"`"+match.Pattern+"`",
)
}
return t.String()
}
// stats says what the pass actually cost, which is the whole claim: the text
// is read once whatever the dictionary holds.
func stats(m *ahocorasick.Matcher, text string) string {
return "`" + strconv.Itoa(len(m.Patterns())) + "` patterns, `" +
strconv.Itoa(m.Nodes()) + "` automaton nodes, `" +
strconv.Itoa(len(text)) + "` bytes read once, `" +
strconv.Itoa(len(m.FindAll(text))) + "` matches. " +
"Running a single-pattern scan per word would read those bytes `" +
strconv.Itoa(len(m.Patterns())) + "` times instead.\n"
}
// list renders a pattern set as code spans. The patterns are this file's own
// constants, never the caller's.
func list(patterns []string) string {
parts := make([]string, 0, len(patterns))
for _, p := range patterns {
parts = append(parts, "`"+p+"`")
}
return strings.Join(parts, ", ")
}
// parseArg takes the one routing slash off and nothing else. It does not cut
// at a later slash and it does not trim: a page that offers to scan what the
// caller typed either scans that string or says it found nothing, and never
// quietly substitutes a shorter or tidier one.
func parseArg(path string) string {
return strings.TrimPrefix(path, "/")
}
gnomod.toml
module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/ahocorasickdemo/v0" gno = "0.9" private = true [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 755778 max_deposit = "6000000ugnot"
render_example_test.gno
package ahocorasickdemo
// ExampleRender pins the realm's root Render output as a testable example.
// The `ushers` rows are the textbook automaton's published answer, so this
// example doubles as a conformance test for the library.
func ExampleRender() {
print(Render(""))
// Output:
// # Aho-Corasick
//
// One automaton for the whole dictionary, one pass over the text, demoing the [`p/moul/x/daily/ahocorasick`](/p/moul/x/daily/ahocorasick/v0) library.
//
// ## The 1975 example
//
// Patterns `he`, `she`, `his`, `hers` over `ushers`. Three of the four overlap, and `hers` ends where nothing else starts.
//
// | start | end | pattern |
// | --- | --- | --- |
// | `1` | `4` | `she` |
// | `2` | `4` | `he` |
// | `2` | `6` | `hers` |
//
// ## One pass over a sentence
//
// Patterns `the`, `he`, `fox`, `over`, `lazy` over `the quick brown fox jumps over the lazy dog`.
//
// | start | end | pattern |
// | --- | --- | --- |
// | `0` | `3` | `the` |
// | `1` | `3` | `he` |
// | `16` | `19` | `fox` |
// | `26` | `30` | `over` |
// | `31` | `34` | `the` |
// | `32` | `34` | `he` |
// | `35` | `39` | `lazy` |
//
// `5` patterns, `17` automaton nodes, `43` bytes read once, `7` matches. Running a single-pattern scan per word would read those bytes `5` times instead.
//
// > Scan your own text: append it to the path, for instance [`/thermos`](/r/moul/x/daily/ahocorasickdemo/v0:/thermos), where `the` and `he` overlap on the same three letters.
}
// ExampleRenderScan pins the argument path: the visitor's text is scanned with
// the same dictionary, and the table reports offsets and dictionary words only.
func ExampleRenderScan() {
print(Render("/thermos"))
// Output:
// # Aho-Corasick
//
// One automaton for the whole dictionary, one pass over the text, demoing the [`p/moul/x/daily/ahocorasick`](/p/moul/x/daily/ahocorasick/v0) library.
//
// ## `thermos`
//
// Patterns `the`, `he`, `fox`, `over`, `lazy`.
//
// | start | end | pattern |
// | --- | --- | --- |
// | `0` | `3` | `the` |
// | `1` | `3` | `he` |
//
// `5` patterns, `17` automaton nodes, `7` bytes read once, `2` matches. Running a single-pattern scan per word would read those bytes `5` times instead.
}
scan_test.gno
package ahocorasickdemo
import (
"strings"
"testing"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
"gno.land/p/nt/uassert/v0"
)
// The path is the visitor's text and it is echoed inside a code span, so a
// backtick in it must not close that span and let a link through. A backslash
// does not escape a backtick inside a span: only a wider fence does, which is
// what md.InlineCode picks.
func TestRenderArgCannotCloseItsCodeSpan(t *testing.T) {
arg := "x` [claim](https://evil.example) `"
out := Render("/" + arg)
uassert.True(t, strings.Contains(out, md.InlineCode(arg)), out)
uassert.False(t, strings.Contains(out, "`x\\` [claim](https://evil.example) \\``"), out)
}
// The match table reports offsets and dictionary words, never a slice of the
// scanned text, so the argument reaches the page exactly once: in the heading
// that echoes it. A pipe in it cannot open a column that is not there.
func TestRenderEchoesTheArgumentOnlyOnce(t *testing.T) {
out := Render("/the|evil")
uassert.Equal(t, 1, strings.Count(out, "evil"))
uassert.True(t, strings.Contains(out, "| `0` | `3` | `the` |"), out)
}
// A text with nothing from the dictionary gets a sentence, not an empty table:
// an empty table reads as a broken page rather than as an answer.
func TestRenderSaysWhenNothingMatched(t *testing.T) {
out := Render("/zzz")
uassert.True(t, strings.Contains(out, "No pattern occurs in it."), out)
uassert.False(t, strings.Contains(out, "| start | end | pattern |"), out)
}
// Whitespace is not trimmed: trimming would report offsets into a string the
// visitor did not submit. The leading space shifts every offset by one.
func TestRenderDoesNotTrim(t *testing.T) {
out := Render("/ the")
uassert.True(t, strings.Contains(out, "| `1` | `4` | `the` |"), out)
}
// The realm holds no state, so two renders of the same path agree. This is
// what makes the pinned example meaningful rather than a snapshot of whichever
// test ran before it.
func TestRenderIsDeterministic(t *testing.T) {
uassert.Equal(t, Render(""), Render(""))
uassert.Equal(t, Render("/thermos"), Render("/thermos"))
}