Struct rand_regex::Regex[][src]

pub struct Regex { /* fields omitted */ }

A random distribution which generates strings matching the specified regex.

Implementations

impl Regex[src]

pub const fn encoding(&self) -> Encoding[src]

Obtains the narrowest string encoding this regex can produce.

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

Checks if the regex can only produce ASCII strings.

Examples

let ascii_gen = rand_regex::Regex::compile("[0-9]+", 100).unwrap();
assert_eq!(ascii_gen.is_ascii(), true);
let non_ascii_gen = rand_regex::Regex::compile(r"\d+", 100).unwrap();
assert_eq!(non_ascii_gen.is_ascii(), false);

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

Checks if the regex can only produce valid Unicode strings.

Due to complexity of regex pattern, this method may have false negative (returning false but still always produce valid UTF-8)

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

Errors

Returns an error if the pattern is not valid regex, or contains anchors (^, $, \A, \z) or word boundary assertions (\b, \B).

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.

Errors

Returns an error if the Hir object contains anchors (^, $, \A, \z) or word boundary assertions (\b, \B).

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<EncodedString> for Regex[src]

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, Global>> 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

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>,