Struct rand_regex::Regex

source ·
pub struct Regex { /* private fields */ }
Expand description

A random distribution which generates strings matching the specified regex.

Implementations

Checks if the regex can only produce valid Unicode strings.

Examples
extern crate rand_regex;
extern crate regex_syntax;

let utf8_hir = regex_syntax::ParserBuilder::new()
    .unicode(false)
    .allow_invalid_utf8(true)
    .build()
    .parse(r"[\x00-\x7f]")
    .unwrap();
let utf8_gen = rand_regex::Regex::with_hir(utf8_hir, 100).unwrap();
assert_eq!(utf8_gen.is_utf8(), true);

let non_utf8_hir = regex_syntax::ParserBuilder::new()
    .unicode(false)
    .allow_invalid_utf8(true)
    .build()
    .parse(r"[\x00-\xff]")
    .unwrap();
let non_utf8_gen = rand_regex::Regex::with_hir(non_utf8_hir, 100).unwrap();
assert_eq!(non_utf8_gen.is_utf8(), false);

Returns the maximum length the string this regex can generate.

Examples
extern crate rand_regex;

let gen = rand_regex::Regex::compile(r"\d{4}-\d{2}-\d{2}", 100).unwrap();
assert_eq!(gen.capacity(), 34);
// each `\d` can occupy 4 bytes

Compiles a regex pattern for string generation.

If you need to supply additional flags to the pattern, please use Regex::with_hir() instead.

The max_repeat parameter gives the maximum extra repeat counts the x*, x+ and x{n,} operators will become, e.g.

extern crate rand_regex;

let gen = rand_regex::Regex::compile("a{4,}", 100).unwrap();
// this will generate a string between 4 to 104 characters long.
assert_eq!(gen.capacity(), 104);

Compiles a parsed regex pattern for string generation.

The Hir object can be obtained using regex_syntax::ParserBuilder.

The max_repeat parameter gives the maximum extra repeat counts the x*, x+ and x{n,} operators will become.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Creates an empty regex which generates empty strings.

Examples
extern crate rand_regex;
extern crate rand;
use rand::Rng;

let gen = rand_regex::Regex::default();
assert_eq!(rand::thread_rng().sample::<String, _>(&gen), "");

Samples a random string satisfying the regex.

Panics

If the regex produced some non-UTF-8 byte sequence, this method will panic. You may want to check is_utf8() to ensure the regex will only generate valid Unicode strings, or sample a Vec<u8> and manually check for UTF-8 validity.

Create an iterator that generates random values of T, using rng as the source of randomness. Read more

Samples a random byte string satisfying the regex.

Create an iterator that generates random values of T, using rng as the source of randomness. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.