ac

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: BSD-3-Clause Imports: 1 Imported by: 0

README ¶

ac

GoDoc Build Status

Golang implementation of Aho-Corasick for rapid substring matching on either byte strings or ASCII strings.

This is based on the excellent library cloudflare/ahocorasick (BSD License). The fork/changes were needed for a specific application usages that are incomptabile with the original library. Some other minor optimizations around memory and setup were also done.

🚨 NOTICE 🚨

Effective May 17th 2021 the default branch will change from master to main. Run the following commands to update a local clone:

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

Examples

  • FindAllString
m := ac.MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
matches := m.FindAllString("The Man Of Steel: Superman")
fmt.Println(matches)

Output:

[Superman uperman perman erman]
  • MatchString
m := ac.MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
contains := m.MatchString("The Man Of Steel: Superman")
fmt.Println(contains)

Output:

true

ac/acascii for pure ASCII matching

The ac/acascii package assumes the dictionary is all ASCII characters (1-127) without NULL bytes. This results in during setup:

  • 50% less memory allocations
  • 50% less memory users
  • 50% less CPU time

as compared to the plain ac package.

IN PROGRESS

  • Support for ASCII case-insensitive matching.

Documentation ¶

Overview ¶

Package ac provides an implementation of the Aho-Corasick string matching algorithm. Throughout this code []byte is referred to as a blice.

http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm

Copyright (c) 2013 CloudFlare, Inc.

Originally from https://github.com/cloudflare/ahocorasick

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Matcher ¶

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

Matcher contains a list of blices to match against

func Compile ¶

func Compile(dictionary [][]byte) (*Matcher, error)

Compile creates a new Matcher using a list of []byte

func CompileString ¶

func CompileString(dictionary []string) (*Matcher, error)

CompileString creates a new Matcher used to match against a set of strings (this is a helper to make initialization easy)

func MustCompile ¶

func MustCompile(dictionary [][]byte) *Matcher

MustCompile returns a Matcher or panics

func MustCompileString ¶

func MustCompileString(dictionary []string) *Matcher

MustCompileString returns a Matcher or panics

func (*Matcher) FindAll ¶

func (m *Matcher) FindAll(in []byte) [][]byte

FindAll searches in for blices and returns all the blices found in the original dictionary

func (*Matcher) FindAllString ¶

func (m *Matcher) FindAllString(in string) []string

FindAllString searches in for blices and returns all the blices (as strings) found as in the original dictionary

Example ¶
m := MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
matches := m.FindAllString("The Man Of Steel: Superman")
fmt.Println(matches)
Output:

[Superman uperman perman erman]

func (*Matcher) Match ¶

func (m *Matcher) Match(in []byte) bool

Match returns true if the input slice contains any subslices

func (*Matcher) MatchString ¶

func (m *Matcher) MatchString(in string) bool

MatchString returns true if the input slice contains any subslices

Example ¶
m := MustCompileString([]string{"Superman", "uperman", "perman", "erman"})
contains := m.MatchString("The Man Of Steel: Superman")
fmt.Println(contains)
Output:

true

Directories ¶

Path Synopsis
Package ac provides an implementation of the Aho-Corasick string matching algorithm.
Package ac provides an implementation of the Aho-Corasick string matching algorithm.

Jump to

Keyboard shortcuts

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