Skip to main content

Width

Struct Width 

Source
pub struct Width {
    pub min: usize,
    pub max: Option<usize>,
}
Expand description

The range of character widths a pattern can match.

  • min — fewest characters consumed; always finite.
  • max — most characters consumed; None means unbounded (*, +, {n,}).

Fields§

§min: usize§max: Option<usize>

Implementations§

Source§

impl Width

Source

pub fn fixed(n: usize) -> Self

Fixed width: min == max.

Source

pub fn unbounded(min: usize) -> Self

Unbounded: at least min characters, no upper limit.

Source

pub fn is_fixed(&self) -> bool

true when the pattern always matches the same number of characters.

Examples found in repository?
examples/width.rs (line 60)
25fn main() {
26    println!("=== Fixed-width patterns ===");
27    report("abc");
28    report(r"\d{4}-\d{2}-\d{2}");           // ISO date → 10
29    report(r"#[0-9a-fA-F]{6}");             // CSS hex colour → 7
30    report(r"[A-Z]{2}[0-9]{4}");            // plate → 6
31
32    println!("\n=== Variable but bounded patterns ===");
33    report(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); // IPv4 → 7..=15
34    report(r"https?://");                    // 7..=8
35    report(r"[a-zA-Z]{2,8}");               // 2..=8
36    report(r"colou?r");                     // 5..=6
37
38    println!("\n=== Unbounded patterns ===");
39    report(r"\w+");                          // 1..∞
40    report(r".*");                           // 0..∞
41    report(r"\S+@\S+\.\S+");               // 5..∞
42    report(r"(?:ab)+");                     // 2..∞
43
44    println!("\n=== Anchors and lookarounds are zero-width ===");
45    report(r"^hello$");                     // anchors → still 5
46    report(r"foo(?=bar)");                  // lookahead → 3
47    report(r"(?<=\d)px");                   // lookbehind → 2
48    report(r"\bword\b");                    // \b → 4
49
50    println!("\n=== Alternation ===");
51    report(r"cat|dog");                     // exactly 3
52    report(r"a|bb|ccc");                    // 1..=3
53    report(r"yes|no");                      // 2..=3
54    report(r"x|\d+");                       // 1..∞
55
56    // ── using pattern_width convenience function ───────────────────────────
57    println!("\n=== pattern_width() convenience wrapper ===");
58    let w = pattern_width(r"a{2,5}").unwrap();
59    println!("  a{{2,5}} => {w}");
60    println!("    is_fixed:     {}", w.is_fixed());
61    println!("    is_nullable:  {}", w.is_nullable());
62    println!("    is_unbounded: {}", w.is_unbounded());
63
64    // ── calling methods on a sub-node ──────────────────────────────────────
65    println!("\n=== Inspecting sub-nodes ===");
66    let ast = parse(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
67    println!("  full pattern  min={} max={:?}", ast.min_width(), ast.max_width());
68
69    // Walk the top-level Concat children manually
70    if let re_parser::ast::Regex::Concat(nodes) = &ast {
71        for (i, node) in nodes.iter().enumerate() {
72            println!(
73                "  child[{i}]  min={}  max={:?}  node={node:?}",
74                node.min_width(),
75                node.max_width(),
76            );
77        }
78    }
79}
Source

pub fn is_nullable(&self) -> bool

true when the pattern can match the empty string.

Examples found in repository?
examples/width.rs (line 61)
25fn main() {
26    println!("=== Fixed-width patterns ===");
27    report("abc");
28    report(r"\d{4}-\d{2}-\d{2}");           // ISO date → 10
29    report(r"#[0-9a-fA-F]{6}");             // CSS hex colour → 7
30    report(r"[A-Z]{2}[0-9]{4}");            // plate → 6
31
32    println!("\n=== Variable but bounded patterns ===");
33    report(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); // IPv4 → 7..=15
34    report(r"https?://");                    // 7..=8
35    report(r"[a-zA-Z]{2,8}");               // 2..=8
36    report(r"colou?r");                     // 5..=6
37
38    println!("\n=== Unbounded patterns ===");
39    report(r"\w+");                          // 1..∞
40    report(r".*");                           // 0..∞
41    report(r"\S+@\S+\.\S+");               // 5..∞
42    report(r"(?:ab)+");                     // 2..∞
43
44    println!("\n=== Anchors and lookarounds are zero-width ===");
45    report(r"^hello$");                     // anchors → still 5
46    report(r"foo(?=bar)");                  // lookahead → 3
47    report(r"(?<=\d)px");                   // lookbehind → 2
48    report(r"\bword\b");                    // \b → 4
49
50    println!("\n=== Alternation ===");
51    report(r"cat|dog");                     // exactly 3
52    report(r"a|bb|ccc");                    // 1..=3
53    report(r"yes|no");                      // 2..=3
54    report(r"x|\d+");                       // 1..∞
55
56    // ── using pattern_width convenience function ───────────────────────────
57    println!("\n=== pattern_width() convenience wrapper ===");
58    let w = pattern_width(r"a{2,5}").unwrap();
59    println!("  a{{2,5}} => {w}");
60    println!("    is_fixed:     {}", w.is_fixed());
61    println!("    is_nullable:  {}", w.is_nullable());
62    println!("    is_unbounded: {}", w.is_unbounded());
63
64    // ── calling methods on a sub-node ──────────────────────────────────────
65    println!("\n=== Inspecting sub-nodes ===");
66    let ast = parse(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
67    println!("  full pattern  min={} max={:?}", ast.min_width(), ast.max_width());
68
69    // Walk the top-level Concat children manually
70    if let re_parser::ast::Regex::Concat(nodes) = &ast {
71        for (i, node) in nodes.iter().enumerate() {
72            println!(
73                "  child[{i}]  min={}  max={:?}  node={node:?}",
74                node.min_width(),
75                node.max_width(),
76            );
77        }
78    }
79}
Source

pub fn is_unbounded(&self) -> bool

true when there is no upper bound on the match length.

Examples found in repository?
examples/width.rs (line 62)
25fn main() {
26    println!("=== Fixed-width patterns ===");
27    report("abc");
28    report(r"\d{4}-\d{2}-\d{2}");           // ISO date → 10
29    report(r"#[0-9a-fA-F]{6}");             // CSS hex colour → 7
30    report(r"[A-Z]{2}[0-9]{4}");            // plate → 6
31
32    println!("\n=== Variable but bounded patterns ===");
33    report(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"); // IPv4 → 7..=15
34    report(r"https?://");                    // 7..=8
35    report(r"[a-zA-Z]{2,8}");               // 2..=8
36    report(r"colou?r");                     // 5..=6
37
38    println!("\n=== Unbounded patterns ===");
39    report(r"\w+");                          // 1..∞
40    report(r".*");                           // 0..∞
41    report(r"\S+@\S+\.\S+");               // 5..∞
42    report(r"(?:ab)+");                     // 2..∞
43
44    println!("\n=== Anchors and lookarounds are zero-width ===");
45    report(r"^hello$");                     // anchors → still 5
46    report(r"foo(?=bar)");                  // lookahead → 3
47    report(r"(?<=\d)px");                   // lookbehind → 2
48    report(r"\bword\b");                    // \b → 4
49
50    println!("\n=== Alternation ===");
51    report(r"cat|dog");                     // exactly 3
52    report(r"a|bb|ccc");                    // 1..=3
53    report(r"yes|no");                      // 2..=3
54    report(r"x|\d+");                       // 1..∞
55
56    // ── using pattern_width convenience function ───────────────────────────
57    println!("\n=== pattern_width() convenience wrapper ===");
58    let w = pattern_width(r"a{2,5}").unwrap();
59    println!("  a{{2,5}} => {w}");
60    println!("    is_fixed:     {}", w.is_fixed());
61    println!("    is_nullable:  {}", w.is_nullable());
62    println!("    is_unbounded: {}", w.is_unbounded());
63
64    // ── calling methods on a sub-node ──────────────────────────────────────
65    println!("\n=== Inspecting sub-nodes ===");
66    let ast = parse(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
67    println!("  full pattern  min={} max={:?}", ast.min_width(), ast.max_width());
68
69    // Walk the top-level Concat children manually
70    if let re_parser::ast::Regex::Concat(nodes) = &ast {
71        for (i, node) in nodes.iter().enumerate() {
72            println!(
73                "  child[{i}]  min={}  max={:?}  node={node:?}",
74                node.min_width(),
75                node.max_width(),
76            );
77        }
78    }
79}

Trait Implementations§

Source§

impl Clone for Width

Source§

fn clone(&self) -> Width

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Width

Source§

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

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

impl Display for Width

Source§

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

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

impl Eq for Width

Source§

impl PartialEq for Width

Source§

fn eq(&self, other: &Width) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Width

Auto Trait Implementations§

§

impl Freeze for Width

§

impl RefUnwindSafe for Width

§

impl Send for Width

§

impl Sync for Width

§

impl Unpin for Width

§

impl UnsafeUnpin for Width

§

impl UnwindSafe for Width

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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

Source§

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.