logo
pub struct RegexSet { /* private fields */ }
Expand description

A set of regular expressions.

A RegexSet encapsulates a set of regular expressions. The regular expressions are compiled according to the rules defined in Section 8 of RFC 4880 modulo two differences. First, the compiler only works on UTF-8 strings (not bytes). Second, ranges in character classes are between UTF-8 characters, not just ASCII characters. Further, by default, strings that don’t pass a sanity check (in particular, include Unicode control characters) never match. This behavior can be customized using RegexSet::disable_sanitizations.

RegexSet implements the semantics of [regular expression]s used in Trust Signatures. In particular, a RegexSet makes it easier to deal with trust signatures that:

  • Contain multiple Regular Expression subpackts,
  • Have no Regular Expression subpackets, and/or
  • Include one or more Regular Expression subpackets that are invalid.

RegexSet compiles each regular expression individually. If there are no regular expressions, the RegexSet matches everything. If a regular expression is invalid, RegexSet treats it as if it doesn’t match anything. Thus, if all regular expressions are invalid, the RegexSet matches nothing (not everything!).

See the module-level documentation for more details.

Implementations

Parses and compiles the regular expressions.

Invalid regular expressions do not cause this to fail. See RegexSet’s top-level documentation for details.

By default, strings that don’t pass a sanity check (in particular, include Unicode control characters) never match. This behavior can be customized using RegexSet::disable_sanitizations.

Examples
use sequoia_openpgp as openpgp;
use openpgp::regex::RegexSet;

// Extract the regex and compile it.
let res = &[
    "<[^>]+[@.]example\\.org>$",
    // Invalid.
    "[..",
];

let res = RegexSet::new(res)?;

assert!(res.is_match("Alice <alice@example.org>"));
assert!(! res.is_match("Bob <bob@example.com>"));

Parses and compiles the regular expressions.

The regular expressions are first converted to UTF-8 strings. Byte sequences that are not valid UTF-8 strings are considered to be invalid regular expressions. Invalid regular expressions do not cause this to fail. See RegexSet’s top-level documentation for details.

By default, strings that don’t pass a sanity check (in particular, include Unicode control characters) never match. This behavior can be customized using RegexSet::disable_sanitizations.

Examples
use sequoia_openpgp as openpgp;
use openpgp::regex::RegexSet;

// A valid and an invalid UTF-8 byte sequence.  The invalid
// sequence doesn't match anything.  But, that doesn't impact
// the other regular expressions.
let res: &[ &[u8] ] = &[
    &b"<[^>]+[@.]example\\.org>$"[..],
    // Invalid UTF-8.
    &b"\xC3\x28"[..],
];
assert!(std::str::from_utf8(res[0]).is_ok());
assert!(std::str::from_utf8(res[1]).is_err());

let re_set = RegexSet::from_bytes(res.into_iter())?;

assert!(re_set.is_match("Alice <alice@example.org>"));
assert!(! re_set.is_match("Bob <bob@example.com>"));

// If we only have invalid UTF-8 strings, then nothing
// matches.
let res: &[ &[u8] ] = &[
    // Invalid UTF-8.
    &b"\xC3\x28"[..],
];
assert!(std::str::from_utf8(res[0]).is_err());

let re_set = RegexSet::from_bytes(res.into_iter())?;

assert!(! re_set.is_match("Alice <alice@example.org>"));
assert!(! re_set.is_match("Bob <bob@example.com>"));


// But, if we have no regular expressions, everything matches.
let res: &[ &[u8] ] = &[];
let re_set = RegexSet::from_bytes(res.into_iter())?;

assert!(re_set.is_match("Alice <alice@example.org>"));
assert!(re_set.is_match("Bob <bob@example.com>"));

Creates a RegexSet from the regular expressions stored in a trust signature.

This method is a convenience function, which extracts any regular expressions from a Trust Signature and wraps them in a RegexSet.

If the signature is not a valid trust signature (its type is GenericCertification, PersonaCertification, CasualCertification, or PositiveCertification, and the Trust Signature subpacket is present), this returns an error.

By default, strings that don’t pass a sanity check (in particular, include Unicode control characters) never match. This behavior can be customized using RegexSet::disable_sanitizations.

Examples
use sequoia_openpgp as openpgp;
use openpgp::regex::RegexSet;

// certification is a trust signature, which contains two regular
// expressions: one that matches all mail addresses for 'example.org'
// and another that matches all mail addresses for 'example.com'.
let certification: &Signature = // ...;

// Extract the regex and compile it.
let res = RegexSet::from_signature(certification)?;

// Some positive examples.
assert!(res.is_match("Alice <alice@example.org>"));
assert!(res.is_match("Bob <bob@example.com>"));

// Wrong domain.
assert!(! res.is_match("Carol <carol@acme.com>"));

// The standard regex, "<[^>]+[@.]example\\.org>$" only matches
// email addresses wrapped in <>.
assert!(! res.is_match("dave@example.com"));

// And, it is case sensitive.
assert!(res.is_match("Ellen <ellen@example.com>"));
assert!(! res.is_match("Ellen <ellen@EXAMPLE.COM>"));

Returns a RegexSet that matches everything.

Note: sanitizations are still enabled. So, to really match everything, you still need to call RegexSet::disable_sanitizations.

This can be used to optimize the evaluation of scoping rules along a path: if a RegexSet matches everything, then it doesn’t further contrain the path.

Returns whether a RegexSet matches everything.

Normally, this only returns true if the RegexSet was created using RegexSet::everything. RegexSet::new, RegexSet::from_bytes, RegexSet::from_signature do detect some regular expressions that match everything (e.g., if no regular expressions are supplied). But, they do not guarantee that a RegexSet containing a regular expression like .?, which does in fact match everything, is detected as matching everything.

Examples
use sequoia_openpgp as openpgp;
use openpgp::regex::RegexSet;

assert!(RegexSet::everything()?.matches_everything());
let empty: &[ &str ] = &[];
assert!(RegexSet::new(empty)?.matches_everything());

// A regular expression that matches everything.  But
// `RegexSet` returns false, because it can't detect it.
let res: &[ &str ] = &[
    &".?"[..],
];
let re_set = RegexSet::new(res.into_iter())?;
assert!(! re_set.matches_everything());

Controls whether strings with control characters are allowed.

If false (the default), i.e., sanity checks are enabled, and the string doesn’t pass the sanity check (in particular, it contains a Unicode control character according to char::is_control, including newlines and an embedded NUL byte), this returns false.

Returns whether the regular expression set matches the string.

If sanity checks are enabled (the default) and the string doesn’t pass the sanity check (in particular, it contains a Unicode control character according to char::is_control, including newlines and an embedded NUL byte), this returns false.

If the RegexSet contains one or more regular expressions, this method returns whether at least one of the regular expressions matches. Invalid regular expressions never match.

If the RegexSet does not contain any regular expressions (valid or otherwise), this method returns true.

Examples
use sequoia_openpgp as openpgp;
use openpgp::regex::RegexSet;

// A regular expression that matches anything.  (Note: this is
// equivalent to providing no regular expressions.)
let res: &[ &str ] = &[
    &""[..],
];
let re_set = RegexSet::new(res.into_iter())?;

assert!(re_set.is_match("Alice Lovelace <alice@example.org>"));

// If a User ID has an embedded control character, it doesn't
// match.
assert!(! re_set.is_match("Alice <alice@example.org>\0"));

Returns whether the regular expression matches the User ID.

If the User ID is not a valid UTF-8 string, this returns false.

If sanity checks are enabled (the default) and the string doesn’t pass the sanity check (in particular, it contains a Unicode control character according to char::is_control, including newlines and an embedded NUL byte), this returns false.

If the RegexSet contains one or more regular expressions, this method returns whether at least one of the regular expressions matches. Invalid regular expressions never match.

If the RegexSet does not contain any regular expressions (valid or otherwise), this method returns true.

Examples
use sequoia_openpgp as openpgp;
use openpgp::packet::UserID;
use openpgp::regex::RegexSet;

// A regular expression that matches anything.  (Note: this is
// equivalent to providing no regular expressions.)
let res: &[ &str ] = &[
    "",
];
let re_set = RegexSet::new(res.into_iter())?;

assert!(re_set.matches_userid(
    &UserID::from(&b"Alice Lovelace <alice@example.org>"[..])));

// If a User ID is not valid UTF-8, it never matches.
assert!(! re_set.matches_userid(
    &UserID::from(&b"Alice \xC3\x28 Lovelace <alice@example.org>"[..])));

// If a User ID has an embedded control character, it doesn't
// match.
assert!(! re_set.matches_userid(
    &UserID::from(&b"Alice <alice@example.org>\0"[..])));

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

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.

Should always be Self

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.