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
impl Regex
sourcepub const fn encoding(&self) -> Encoding
pub const fn encoding(&self) -> Encoding
Obtains the narrowest string encoding this regex can produce.
sourcepub const fn is_ascii(&self) -> bool
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);
sourcepub const fn is_utf8(&self) -> bool
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);
sourcepub const fn capacity(&self) -> usize
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
sourcepub fn compile(pattern: &str, max_repeat: u32) -> Result<Self, Error>
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
).
sourcepub fn with_hir(hir: Hir, max_repeat: u32) -> Result<Self, Error>
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 Distribution<EncodedString> for Regex
impl Distribution<EncodedString> for Regex
source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> EncodedString
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> EncodedString
T
, using rng
as the source of randomness.source§fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
T
, using rng
as
the source of randomness. Read moresource§impl Distribution<Result<String, FromUtf8Error>> for Regex
impl Distribution<Result<String, FromUtf8Error>> for Regex
source§fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Result<String, FromUtf8Error>
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>
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
T
, using rng
as
the source of randomness. Read more