pub fn to_str(&self, buf: &mut String, precedence: u8) {
        match *self {
            Expr::Empty => (),
            Expr::Any { newline } => buf.push_str(if newline { "(?s:.)" } else { "." }),
            Expr::Literal { ref val, casei } => {
                if casei {
                    buf.push_str("(?i:");
                }
                push_quoted(buf, val);
                if casei {
                    buf.push_str(")");
                }
            }
            Expr::StartText => buf.push('^'),
            Expr::EndText => buf.push('$'),
            Expr::StartLine => buf.push_str("(?m:^)"),
            Expr::EndLine => buf.push_str("(?m:$)"),
            Expr::Concat(ref children) => {
                if precedence > 1 {
                    buf.push_str("(?:");
                }
                for child in children {
                    child.to_str(buf, 2);
                }
                if precedence > 1 {
                    buf.push(')')
                }
            }
            Expr::Alt(ref children) => {
                if precedence > 0 {
                    buf.push_str("(?:");
                }

                let is_empty = |e: &Expr| match e {
                    Expr::Empty => true,
                    _ => false,
                };

                let contains_empty = children.iter().any(is_empty);
                if contains_empty {
                    buf.push_str("(?:");
                }
                for (i, child) in children.iter().filter(|&c| !is_empty(c)).enumerate() {
                    if i != 0 {
                        buf.push('|');
                    }
                    child.to_str(buf, 1);
                }
                if contains_empty {
                    // regex fails with `(a|b|)`, so transform to `((?:a|b)?)`
                    buf.push_str(")?");
                }

                if precedence > 0 {
                    buf.push(')');
                }
            }
            Expr::Group(ref child) => {
                buf.push('(');
                child.to_str(buf, 0);
                buf.push(')');
            }
            Expr::Repeat {
                ref child,
                lo,
                hi,
                greedy,
            } => {
                if precedence > 2 {
                    buf.push_str("(?:");
                }
                child.to_str(buf, 3);
                match (lo, hi) {
                    (0, 1) => buf.push('?'),
                    (0, usize::MAX) => buf.push('*'),
                    (1, usize::MAX) => buf.push('+'),
                    (lo, hi) => {
                        buf.push('{');
                        push_usize(buf, lo);
                        if lo != hi {
                            buf.push(',');
                            if hi != usize::MAX {
                                push_usize(buf, hi);
                            }
                        }
                        buf.push('}');
                    }
                }
                if !greedy {
                    buf.push('?');
                }
                if precedence > 2 {
                    buf.push(')');
                }
            }
            Expr::Delegate {
                ref inner, casei, ..
            } => {
                // at the moment, delegate nodes are just atoms
                if casei {
                    buf.push_str("(?i:");
                }
                buf.push_str(inner);
                if casei {
                    buf.push_str(")");
                }
            }
            _ => panic!("attempting to format hard expr"),
        }
    }
/// Convert expression to a regex string in the regex crate's syntax.
///
/// # Panics
///
/// Panics for expressions that are hard, i.e. can not be handled by the regex crate.
Convert expression to a regex string in the regex crate's syntax.
Convert expression to a regex string in the regex crate ' s syntax .
pub fn to_str(&self, buf: &mut String, precedence: u8) {
        match *self {
            Expr::Empty => (),
            Expr::Any { newline } => buf.push_str(if newline { "(?s:.)" } else { "." }),
            Expr::Literal { ref val, casei } => {
                if casei {
                    buf.push_str("(?i:");
                }
                push_quoted(buf, val);
                if casei {
                    buf.push_str(")");
                }
            }
            Expr::StartText => buf.push('^'),
            Expr::EndText => buf.push('$'),
            Expr::StartLine => buf.push_str("(?m:^)"),
            Expr::EndLine => buf.push_str("(?m:$)"),
            Expr::Concat(ref children) => {
                if precedence > 1 {
                    buf.push_str("(?:");
                }
                for child in children {
                    child.to_str(buf, 2);
                }
                if precedence > 1 {
                    buf.push(')')
                }
            }
            Expr::Alt(ref children) => {
                if precedence > 0 {
                    buf.push_str("(?:");
                }
                let is_empty = |e: &Expr| match e {
                    Expr::Empty => true,
                    _ => false,
                };
                let contains_empty = children.iter().any(is_empty);
                if contains_empty {
                    buf.push_str("(?:");
                }
                for (i, child) in children.iter().filter(|&c| !is_empty(c)).enumerate() {
                    if i != 0 {
                        buf.push('|');
                    }
                    child.to_str(buf, 1);
                }
                if contains_empty {
                    // regex fails with `(a|b|)`, so transform to `((?:a|b)?)`
                    buf.push_str(")?");
                }
                if precedence > 0 {
                    buf.push(')');
                }
            }
            Expr::Group(ref child) => {
                buf.push('(');
                child.to_str(buf, 0);
                buf.push(')');
            }
            Expr::Repeat {
                ref child,
                lo,
                hi,
                greedy,
            } => {
                if precedence > 2 {
                    buf.push_str("(?:");
                }
                child.to_str(buf, 3);
                match (lo, hi) {
                    (0, 1) => buf.push('?'),
                    (0, usize::MAX) => buf.push('*'),
                    (1, usize::MAX) => buf.push('+'),
                    (lo, hi) => {
                        buf.push('{');
                        push_usize(buf, lo);
                        if lo != hi {
                            buf.push(',');
                            if hi != usize::MAX {
                                push_usize(buf, hi);
                            }
                        }
                        buf.push('}');
                    }
                }
                if !greedy {
                    buf.push('?');
                }
                if precedence > 2 {
                    buf.push(')');
                }
            }
            Expr::Delegate {
                ref inner, casei, ..
            } => {
                // at the moment, delegate nodes are just atoms
                if casei {
                    buf.push_str("(?i:");
                }
                buf.push_str(inner);
                if casei {
                    buf.push_str(")");
                }
            }
            _ => panic!("attempting to format hard expr"),
        }
    }
pub fn to_str ( & self , buf : & mut String , precedence : u8 ) { match * self { Expr :: Empty => ( ) , Expr :: Any { newline } => buf . push_str ( if newline { " " } else { " " } ) , Expr :: Literal { ref val , casei } => { if casei { buf . push_str ( " " ) ; } push_quoted ( buf , val ) ; if casei { buf . push_str ( " " ) ; } } Expr :: StartText => buf . push ( '^' ) , Expr :: EndText => buf . push ( '$' ) , Expr :: StartLine => buf . push_str ( " " ) , Expr :: EndLine => buf . push_str ( " " ) , Expr :: Concat ( ref children ) => { if precedence > 1 { buf . push_str ( " " ) ; } for child in children { child . to_str ( buf , 2 ) ; } if precedence > 1 { buf . push ( ')' ) } } Expr :: Alt ( ref children ) => { if precedence > 0 { buf . push_str ( " " ) ; } let is_empty = | e : & Expr | match e { Expr :: Empty => true , _ => false , } ; let contains_empty = children . iter ( ) . any ( is_empty ) ; if contains_empty { buf . push_str ( " " ) ; } for ( i , child ) in children . iter ( ) . filter ( | & c | ! is_empty ( c ) ) . enumerate ( ) { if i != 0 { buf . push ( '|' ) ; } child . to_str ( buf , 1 ) ; } if contains_empty { // regex fails with `(a|b|)`, so transform to `((?:a|b)?)` buf . push_str ( " " ) ; } if precedence > 0 { buf . push ( ')' ) ; } } Expr :: Group ( ref child ) => { buf . push ( '(' ) ; child . to_str ( buf , 0 ) ; buf . push ( ')' ) ; } Expr :: Repeat { ref child , lo , hi , greedy , } => { if precedence > 2 { buf . push_str ( " " ) ; } child . to_str ( buf , 3 ) ; match ( lo , hi ) { ( 0 , 1 ) => buf . push ( '?' ) , ( 0 , usize :: MAX ) => buf . push ( '*' ) , ( 1 , usize :: MAX ) => buf . push ( '+' ) , ( lo , hi ) => { buf . push ( '{' ) ; push_usize ( buf , lo ) ; if lo != hi { buf . push ( ',' ) ; if hi != usize :: MAX { push_usize ( buf , hi ) ; } } buf . push ( '}' ) ; } } if ! greedy { buf . push ( '?' ) ; } if precedence > 2 { buf . push ( ')' ) ; } } Expr :: Delegate { ref inner , casei , .. } => { // at the moment, delegate nodes are just atoms if casei { buf . push_str ( " " ) ; } buf . push_str ( inner ) ; if casei { buf . push_str ( " " ) ; } } _ => panic ! ( " " ) , } }
Convert expression to a regex string in the regex crate's syntax.
Convert expression to a regex string in the regex crate ' s syntax .
&self
buf
&mut String
precedence
u8
&self
buf
&mut String
precedence
u8
panics
Panics for expressions that are hard, i.e. can not be handled by the regex crate.
Panics for expressions that are hard i . e . can not be handled by the regex crate .