Struct rand_regex::Regex

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

A random distribution which generates strings matching the specified regex.

Implementations§

source§

impl Regex

source

pub const fn encoding(&self) -> Encoding

Obtains the narrowest string encoding this regex can produce.

source

pub const fn is_ascii(&self) -> bool

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

pub const fn is_utf8(&self) -> bool

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)
    .utf8(false)
    .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)
    .utf8(false)
    .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);
source

pub const fn capacity(&self) -> usize

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
source

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

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

source

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

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§

source§

impl Clone for Regex

source§

fn clone(&self) -> Regex

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Regex

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Regex

source§

fn default() -> Self

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), "");
source§

impl Distribution<EncodedString> for Regex

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> EncodedString

Generate a random value of T, using rng as the source of randomness.
source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

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

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Distribution<Result<String, FromUtf8Error>> for Regex

source§

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

Samples a random string satisfying the regex.

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

source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

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

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Distribution<String> for Regex

source§

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

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.

source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

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

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Distribution<Vec<u8>> for Regex

source§

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

Samples a random byte string satisfying the regex.

source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

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

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.