pattern

package module
v0.0.0-...-d07f8be Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: MIT Imports: 2 Imported by: 0

README

pattern

Go library to generate string patterns.

Example

import (
	"fmt"

	"github.com/sollniss/pattern"
)

func main() {
	gen := pattern.New(
		pattern.Repeat(2, 4,
			pattern.Repeat(5, 5, pattern.OneOfByte([]byte("1234567890"))),
			pattern.Literal("-"),
			pattern.Repeat(2, 4, pattern.OneOfRune([]rune("xyz"))),
			pattern.Literal("-"),
		),
		pattern.Potentially(0.3, pattern.OneOfString([]string{"AAAA", "BBBB", "CCCC", "DDDD"})),
		pattern.Literal("-"),
		pattern.Shuffle(pattern.Literal("a"), pattern.Literal("b"), pattern.Literal("c")),
		pattern.Sequence(1, 999, 4),
	)
	for i := 0; i < 10; i++ {
		fmt.Println(gen.String())
	}
}

Outputs:

58525-zzz-30662-yxx-71228-xz-21148-xyz--bca0001
22813-yzyx-74510-yx-40801-yzyz--cab0002
61736-yx-76951-yxxz--cab0003
79908-zxx-96321-zxz-71442-yz-DDDD-acb0004
97571-xz-87210-xy--cab0005
11168-zzz-34216-xy-92184-zz-13246-zxy--abc0006
76919-yx-13975-xx-34534-yyz--cab0007
99940-yzz-31787-yyx--acb0008
13386-zz-60143-yzy-47725-zzxx-CCCC-cab0009
53765-zzyx-87695-zyxz-74415-yxyz--cab0010

Usage

The package exposes an interface Part which all generator fuctions implement. The generator returned by New also implements this interface, which allows combination of multiple generators.

type Part interface {
	// Append appends the Part to the output pattern.
	Append([]byte) []byte
}

Functions

Group(p ...Part) Part

Group returns a Part that wraps p into a single Part.

Repeat(min uint32, max uint32, p ...Part) Part

Repeat returns a Part that repeats p between min and max times randomly.

Potentially(c float64, p Part) Part

Potentially returns a Part that will include p with probability c.

Literal(s string) Part

Literal returns a Part that will always output s.

OneOf(p ...Part) Part

OneOf returns a Part that selects one of p randomly in each iteration. The package also provides the convenience functions OneOfString, OneOfByte and OneOfRune.

Shuffle(p ...Part) Part

Shuffle returns a Part that randomly rearranges p in each iteration.

Sequence(start uint64, max uint64, width int) Part

Sequence returns a Part that will on each iteration increment a number from start to max and display the number zero-padded to width. Sequence is thread safe.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(p ...Part) *gen

New returns a new pattern generator. The generator implements the Part interface, which means it can be used as a Part of another pattern.

Types

type Part

type Part interface {
	// Append appends the Part to the output pattern.
	Append([]byte) []byte
}

Part is a part of a pattern.

func Group

func Group(p ...Part) Part

Group returns a Part that wraps p into a single Part.

func Literal

func Literal(s string) Part

Literal returns a Part that will always output s.

func OneOf

func OneOf(p ...Part) Part

OneOf returns a Part that selects one of p randomly in each iteration.

func OneOfByte

func OneOfByte(b []byte) Part

OneOfByte returns a Part that will select one of b randomly in each iteration.

func OneOfRune

func OneOfRune(r []rune) Part

OneOfRune returns a Part that will select one of r randomly in each iteration. The length of the alphabet must be less than 2^32.

func OneOfString

func OneOfString(s []string) Part

OneOfString returns a Part that will output one of s randomly in each iteration.

func Potentially

func Potentially(c float64, p Part) Part

Potentially returns a Part that will include p with probability c.

Panics if c is < 0.

func Repeat

func Repeat(min uint32, max uint32, p ...Part) Part

Repeat returns a Part that repeats p between min and max times randomly. If min == max, the Part will be repeated exactly max times in each iteration.

func Sequence

func Sequence(start uint64, max uint64, width int) Part

Sequence returns a Part that will on each iteration increment a number from start to max. The number will be zero-padded to width. The output number will reset to start when max is reached.

func Shuffle

func Shuffle(p ...Part) Part

Shuffle returns a Part that randomly rearranges p in each iteration. Uses the Fisher-Yates shuffle to generate permutations.

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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