Skip to main content

CharacterClass

Trait CharacterClass 

Source
pub trait CharacterClass: Sized {
    // Required methods
    fn new_from_range_u32<R: RangeBounds<u32>>(range: R) -> Option<Self>;
    fn new_from_range_char<R: RangeBounds<char>>(range: R) -> Self;
    fn get_cardinality(&self) -> u32;
    fn to_regex(&self) -> String;
}
Expand description

The char set operations a RangeSet<Char> gains.

This trait is implemented for RangeSet<Char> and is not meant to be implemented elsewhere. Import it to call the methods below on a set of char.

§Example

use regex_charclass::{AnyRange, Char, CharacterClass, RangeSet};

let range1 = RangeSet::new_from_range_char('a'..='z');
assert_eq!(26, range1.get_cardinality());
assert_eq!("[a-z]", range1.to_regex());

let range2 = RangeSet::new_from_ranges(&[
    AnyRange::from(Char::new('0')..=Char::new('9')),
    AnyRange::from(Char::new('A')..=Char::new('F')),
    AnyRange::from(Char::new('a')..=Char::new('f')),
]);
assert_eq!("\\p{ASCII_Hex_Digit}", range2.to_regex());

let range2_complement = range2.complement();
assert_eq!("\\P{ASCII_Hex_Digit}", range2_complement.to_regex());

assert_eq!(".", range2.union(&range2_complement).to_regex());
assert_eq!("[]", range2.intersection(&range2_complement).to_regex());

assert_eq!("[g-z]", range1.difference(&range2).to_regex());

Required Methods§

Source

fn new_from_range_u32<R: RangeBounds<u32>>(range: R) -> Option<Self>

Create a new instance from the given range of codepoints, or return None if a bound is not a char.

A range holding no codepoint, such as 0..0, gives the empty set.

Source

fn new_from_range_char<R: RangeBounds<char>>(range: R) -> Self

Create a new instance from the given range of char.

A range holding no char, such as 'a'..'a', gives the empty set.

Source

fn get_cardinality(&self) -> u32

Return the number of char values contained, at most 1,112,064.

Source

fn to_regex(&self) -> String

Return the set as a regular expression character class.

See the crate documentation for the forms this returns and the flags they assume.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§