[][src]Struct rand_regex::Regex

pub struct Regex { /* fields omitted */ }

A random distribution which generates strings matching the specified regex.

Methods

impl Regex[src]

pub const fn is_utf8(&self) -> bool[src]

Checks if the regex can only produce valid Unicode strings.

Examples

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);

pub const fn capacity(&self) -> usize[src]

Returns the maximum length the string this regex can generate.

Examples

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

pub fn compile(pattern: &str, max_repeat: u32) -> Result<Self, Error>[src]

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.

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);

pub fn with_hir(hir: Hir, max_repeat: u32) -> Result<Self, Error>[src]

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

impl Clone for Regex[src]

impl Debug for Regex[src]

impl Default for Regex[src]

fn default() -> Self[src]

Creates an empty regex which generates empty strings.

Examples

use rand::Rng;

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

impl Distribution<Result<String, FromUtf8Error>> for Regex[src]

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Result<String, FromUtf8Error>[src]

Samples a random string satisfying the regex.

The the sampled bytes sequence is not valid UTF-8, the sampling result is an Err value.

impl Distribution<String> for Regex[src]

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> String[src]

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 Result<String, FromUtf8Error> and manually handle the error.

impl Distribution<Vec<u8>> for Regex[src]

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<u8>[src]

Samples a random byte string satisfying the regex.

Auto Trait Implementations

impl RefUnwindSafe for Regex

impl Send for Regex

impl Sync for Regex

impl Unpin for Regex

impl UnwindSafe for Regex

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,