synthexp

package module
v0.0.0-...-8cf08cc Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2019 License: MIT Imports: 3 Imported by: 0

README

synthexp

This package helps you generate strings that match your regular expression.

installation

go get -u github.com/noxer/synthexp

api

Synthesizing the string is a two step process, first you need to compile the regex.

syn, err := synthexp.Compile("Hello (World|Earth|the (dear|awesome) User)\\. Here is some randomness [\\w]{3,8}")
if err != nil {
    fmt.Printf("Could not compile: %s\n", err)
}

Now you can use syn to generate as many matching strings as you want...

str := syn.SynthString()
fmt.Println(str)

Printing those strings gave me the following output.

Hello dear User. Here is some randomness O1iIJ
Hello Earth. Here is some randomness rj3vR
Hello World. Here is some randomness SqO
Hello dear User. Here is some randomness fvM
Hello World. Here is some randomness eHIdQn
Hello World. Here is some randomness tb8
Hello Earth. Here is some randomness xzaD
Hello awesome User. Here is some randomness HNU
Hello World. Here is some randomness qr2HN3S
Hello Earth. Here is some randomness oKL

It can be useful for testing to have control over the captures in a regex. This can be provided by passing *string's to the method (or []byte/[]rune for SynthBytes and Synth). To skip captures and have the library generate random values you can pass nil. The provided values doesn't need to match the regex.

str := syn.SynthString(synthexp.Str("Terra"))
fmt.Println(str)
Hello Terra. Here is some randomness Xf1
Hello Terra. Here is some randomness aC_FwmW
Hello Terra. Here is some randomness WX0
Hello Terra. Here is some randomness Qyp
Hello Terra. Here is some randomness Inf1y
Hello Terra. Here is some randomness 8G0oG
Hello Terra. Here is some randomness fIL
Hello Terra. Here is some randomness VKZ
Hello Terra. Here is some randomness 24d
Hello Terra. Here is some randomness lLN
str := syn.SynthString(nil, synthexp.Str("glorious"))
fmt.Println(str)
Hello World. Here is some randomness Y55
Hello Earth. Here is some randomness dej
Hello World. Here is some randomness dpKI
Hello glorious User. Here is some randomness qF8g
Hello Earth. Here is some randomness 1Ucr
Hello glorious User. Here is some randomness FmhfX_
Hello glorious User. Here is some randomness KVcyhG
Hello World. Here is some randomness nX6a
Hello World. Here is some randomness 8DB
Hello World. Here is some randomness p3y1

limits

The library currently can't reliably generate strings for regular expressions containing ^, $, \b and \B.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// WordRunes defines the characters that can be used in words (as defined by regex).
	WordRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	// NonWordRunes defines the characters that can't be used in words (as defined by regex).
	NonWordRunes = []rune(" ,.-;:_!\"§$%&\\/()=?`´#'+*}][{\n")
	// Alphabet defines the characters used for arbitrary characters, must end with a newline.
	Alphabet = append(WordRunes, NonWordRunes...)
)

Functions

func Str

func Str(str string) *string

Str returns a pointer to the string.

Types

type Synthexp

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

Synthexp offers functionality to generate strings from a regex.

func Compile

func Compile(expr string) (*Synthexp, error)

Compile parses the expression and prepared synthesis.

func (*Synthexp) Synth

func (se *Synthexp) Synth(caps ...[]rune) []rune

Synth synthesises a random []rune that is matched by expr. The parameters in caps allow you to provide fixed values for captures within the regexp. They are not checked against the expression which can lead to non-matching strings. You can provide nil for captures that should be filled by synthexp.

func (*Synthexp) SynthBytes

func (se *Synthexp) SynthBytes(caps ...[]byte) []byte

SynthBytes synthesizes a random string that is matched by expr. The parameters in caps allow you to provide fixed values for captures within the regexp. They are not checked against the expression which can lead to non-matching strings. You can provide nil for captures that should be filled by synthexp.

func (*Synthexp) SynthString

func (se *Synthexp) SynthString(caps ...*string) string

SynthString synthesizes a random string that is matched by expr. The parameters in caps allow you to provide fixed values for captures within the regexp. They are not checked against the expression which can lead to non-matching strings. You can provide nil for captures that should be filled by synthexp.

Jump to

Keyboard shortcuts

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