Documentation
¶
Overview ¶
Package randregex generates pseudo-random strings that match regular expressions.
It is intended for test data, identifiers, fixtures, property-style checks, and other workflows where a regexp is a compact description of valid sample strings. Patterns are parsed with regexp/syntax using syntax.Perl.
Generator values are immutable and safe for concurrent use. The Generate and Append methods use the default pseudo-random source. GenerateWithRand and AppendWithRand let callers provide any Rand implementation, including a seeded *math/rand/v2.Rand for reproducible output. If a Rand is shared across goroutines, the Rand implementation must provide its own synchronization.
Compile parses, validates, and converts patterns into an immutable internal representation using DefaultMaxRepeat for unbounded repetitions such as a*, a+, and a{3,}. Use CompileMaxRepeat or FromRegexpMaxRepeat when a different unbounded-repeat policy is needed. Use MustCompile for package-level generators when invalid patterns should be treated as programmer errors.
Generated strings are not cryptographic secrets unless callers provide a Rand implementation backed by an appropriate cryptographic source, such as CryptoRand.
Character generation is ASCII-first. Literal Unicode characters are emitted as literals, but dot and negated or very broad character classes sample from printable ASCII, from space through tilde. The predefined Perl classes \d, \w, and \s use conventional ASCII definitions. Full Unicode character-class sampling and regex features unsupported by Go's regexp engine, such as lookaround and backreferences, are out of scope.
Index ¶
- Constants
- Variables
- type Generator
- func Compile(pattern string) (*Generator, error)
- func CompileMaxRepeat(pattern string, maxRepeat int) (*Generator, error)
- func FromRegexp(re *syntax.Regexp) (*Generator, error)
- func FromRegexpMaxRepeat(re *syntax.Regexp, maxRepeat int) (*Generator, error)
- func MustCompile(pattern string) *Generator
- func MustCompileMaxRepeat(pattern string, maxRepeat int) *Generator
- type Rand
Examples ¶
Constants ¶
const DefaultMaxRepeat = 32
DefaultMaxRepeat is the recommended upper bound for unbounded repetitions.
It is used by Compile, MustCompile, and FromRegexp for patterns such as a*, a+, and a{3,}. Use the MaxRepeat variants to choose a different bound.
Variables ¶
var CryptoRand = &cryptoRand{}
CryptoRand is a Rand value backed by crypto/rand.Reader.
Pass it to GenerateWithRand or AppendWithRand when generated strings are used as secrets or other security-sensitive identifiers. CryptoRand is safe for concurrent use. It panics if crypto/rand.Reader fails or if IntN is called with n <= 0.
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is an immutable compiled regular-expression string generator.
Generator values are safe for concurrent use. Generation methods using an explicit Rand require the caller's Rand to be safe when shared concurrently.
func Compile ¶
Compile parses pattern using regexp/syntax.Perl and compiles it into a Generator using DefaultMaxRepeat.
Example ¶
package main
import (
"fmt"
"github.com/ryanfowler/randregex"
)
func main() {
g, err := randregex.Compile(`[a-z]{8}\d{2}`)
if err != nil {
panic(err)
}
fmt.Println(len(g.Generate()))
}
Output: 10
func CompileMaxRepeat ¶
CompileMaxRepeat parses pattern using regexp/syntax.Perl and compiles it into a Generator.
maxRepeat controls the maximum used for unbounded repetitions. It must be non-negative. For a{n,}, the upper bound is maxRepeat when maxRepeat > n; otherwise generation emits exactly n repetitions.
Example ¶
package main
import (
"fmt"
"github.com/ryanfowler/randregex"
)
func main() {
g, err := randregex.CompileMaxRepeat(`a{4,}`, 4)
if err != nil {
panic(err)
}
fmt.Println(g.Generate())
}
Output: aaaa
func FromRegexp ¶
FromRegexp compiles re into a Generator without mutating re, using DefaultMaxRepeat. It returns an error when re is nil.
func FromRegexpMaxRepeat ¶
FromRegexpMaxRepeat compiles re into a Generator without mutating re. It returns an error when re is nil.
maxRepeat controls unbounded repetitions as described by CompileMaxRepeat. Passing an already simplified regexp is supported, but regexp/syntax.Simplify may rewrite counted unbounded repetitions such as a{3,} into forms that no longer preserve the original minimum for randregex's maxRepeat policy.
func MustCompile ¶
MustCompile is like Compile but panics if pattern cannot be compiled.
func MustCompileMaxRepeat ¶
MustCompileMaxRepeat is like CompileMaxRepeat but panics if pattern cannot be compiled.
func (*Generator) Append ¶
Append appends a pseudo-random string matching the compiled regexp to dst and returns the extended buffer.
Example ¶
package main
import (
"fmt"
"github.com/ryanfowler/randregex"
)
func main() {
g := randregex.MustCompile(`[a-zA-Z0-9_-]{24}`)
buf := make([]byte, 0, 64)
buf = g.Append(buf)
fmt.Println(len(buf))
}
Output: 24
func (*Generator) AppendWithRand ¶
AppendWithRand appends a generated string matching the compiled regexp to dst using r, and returns the extended buffer.
This is the lowest-allocation public API. If dst has sufficient capacity, it does not allocate for common ASCII patterns. r must be non-nil and must return values in [0, n) from IntN(n). If r is shared concurrently, it must provide its own synchronization.
func (*Generator) Generate ¶
Generate generates a pseudo-random string matching the compiled regexp.
func (*Generator) GenerateWithRand ¶
GenerateWithRand generates a string matching the compiled regexp using r.
r must be non-nil and must return values in [0, n) from IntN(n). If r is shared concurrently, it must provide its own synchronization.
Example ¶
package main
import (
"fmt"
"math/rand/v2"
"github.com/ryanfowler/randregex"
)
func main() {
r := rand.New(rand.NewPCG(1, 2))
g := randregex.MustCompile(`[a-z]{8}`)
fmt.Println(g.GenerateWithRand(r))
}
Output: uquugbml
type Rand ¶
Rand is the random-number interface used by Generator.
It is satisfied by *math/rand/v2.Rand. The package-level CryptoRand value provides a cryptographic implementation. Implementations must return a value in [0, n), and may panic when n <= 0. If a Rand is shared concurrently, the Rand implementation is responsible for synchronization.